summaryrefslogtreecommitdiff
path: root/nim/src/oggie.nim
blob: c7346a9af1d3857cf1a5c87604a4705498d390ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import strformat
import sequtils
import tables
import random
import math
import sugar

import nimsvg

type point = tuple[ x, y: float ]
type vec   = tuple[ x, y: float ]
type line  = tuple[ a, b: point ]
type colour = string

var draw_line:  seq[(line,  colour)] = @[]
var draw_point: seq[(point, colour)] = @[]

proc get_random_points(n = 10, bounds = (x: 100.0, y: 100.0)): seq[point] =
  result = new_seq[point](n)
  for i in 0..<n:
    result[i] = (rand(bounds.x), rand(bounds.y))

iterator tail[A](s: seq[A]): A =
  var first = true
  for x in s:
    if not first:
      yield x
    else:
      first = false

proc rightmost(points: seq[point]): point =
  result = points[0]
  for p in points.tail():
    if p.x > result.x:
      result = p

proc leftmost(points: seq[point]): point =
  result = points[0]
  for p in points.tail():
    if p.x < result.x:
      result = p

proc vec_to(a, b: point): vec = (b.x - a.x, b.y - a.y)

proc mag(v: vec): float = hypot(v.x, v.y)

proc dot(a, b: vec): float = a.x * b.x + a.y * b.y

proc `ยท`(a, b: vec): float = dot(a, b)

proc angle_to(a, b: vec): float = arccos((a.dot(b)) / (a.mag() * b.mag()))

const up: vec = (0.0, 100.0)

proc displaced(p: point, v: vec): point = (p.x + v.x, p.y + v.y)

proc line_from(v: vec, p: point): line = (p, p.displaced(v))

proc find_lefter(b, p, q: point): point =
  let
    bp = b.vec_to(p)
    bq = b.vec_to(q)
  if up.angle_to(bp) < up.angle_to(bq):
    return p
  else:
    return q

proc find_lefter(e, b, p, q: point): point =
  let
    eb = e.vec_to(b)
    bp = b.vec_to(p)
    bq = b.vec_to(q)
  if eb.angle_to(bp) < eb.angle_to(bq):
    return p
  else:
    return q

proc `==`(a, b: point): bool =
  return a.x == b.x and a.y == b.y

iterator all_but_last[A](s: seq[A]): A =
  for i in 0..<(s.len() - 1):
    yield s[i]

proc gift_wrapping(S: seq[point]): seq[point] = # O(nh)
  var i = 0
  var point_on_hull = leftmost(S)
  var hull = init_table[int, point]()
  while true:
    echo i
    hull[i] = point_on_hull
    var endpoint = S[0]
    for p in S.tail():
      if p == point_on_hull:
        continue
      else:
        if i == 0:
          endpoint = find_lefter(hull[i], endpoint, p)
        else:
          endpoint = find_lefter(hull[i-1], hull[i], endpoint, p)
    i = i + 1
    point_on_hull = endpoint
    if endpoint == hull[0]:
      break
  result = new_seq[point](i)
  for j in 0..<i:
    result[j] = hull[j]

proc distance_to(a, b: point): float = a.vec_to(b).mag()

proc times(x: (float, float), m: float): (float, float) = (x[0] * m, x[1] * m)

proc `+`(a: point, b: (float, float)): point = (a.x + b[0], a.y + b[1])

proc grad(l: line): float =
  let dv = l.a.vec_to(l.b)
  result = dv.y / dv.x

proc perpendicular(l: line, c: point): line =
  let
    grad = l.grad()
    perp = - (1 / grad)
    newp = c + ((1.0, perp).times(100.0))
  result = (c, newp)

proc perpendicular(l: line): line = l.perpendicular(l.a)

proc find_horizontal_intersection(l: line, y = 0.0): point =
  let grad = l.grad()
  return ((l.a.x - ((l.a.y - y) / grad)), y)

proc find_vertical_intersection(l: line, x = 0.0): point =
  let grad = l.grad()
  return (x, l.a.y - (grad * (l.a.x - x)))

proc distance(a, b: point): float =
  sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y))

proc meets(a, b: line): point =
  let
    ma = a.grad()
    mb = b.grad()
    ca = a.find_vertical_intersection().y
    cb = b.find_vertical_intersection().y
    y = (mb * ca - ma * cb) / (mb - ma)
  return a.find_horizontal_intersection(y)

proc project(l: line, p: point, o: point): point =
  let
    drop_line  = l.perpendicular(p)
    drop_point = l.meets(drop_line)
    x = o.distance(drop_point)
    y = drop_point.distance(p)
  return (x, y)

proc project(l: line, p: point): point =
  project(l, p, l.find_horizontal_intersection(0.0))

proc project(l: line, ps: seq[point]): seq[point] =
  ps.map((p) => l.project(p))

proc divide(l: line, ps: seq[point]): (seq[point], seq[point]) =
  result = (@[], @[])
  for p in ps:
    echo p.y, " -> ", l.project(p).y
    if l.project(p).y < 0:
      result[0].add(p)
    else:
      result[1].add(p)

proc quickhull(S: seq[point]): seq[point] = # O(nlogn)
  assert(S.len() >= 2)
  var hull = new_seq[point]()
  let
    l = leftmost(S)
    r = rightmost(S)

proc line_to(a, b: point): line =
  return (a, b)

proc main() =
  let ps = get_random_points(10)
  for p in ps:
    draw_point.add((p, "black"))
  let ch = gift_wrapping(ps)
  for i in 1..<ch.len():
    draw_line.add((ch[i-1].line_to(ch[i]), "red"))

proc draw() =
  build_svg_file("out.svg"):
    svg(width=100, height=100):
      rect(x = 0, y = 0, width = 100, height = 100, fill = "#FFF")
      for d in draw_point:
        let (p, c) = d
        circle(cx = p.x, cy = p.y, r = 1.5, fill = c)
      for d in draw_line:
        let
          (p, c) = d
          (a, b) = p
        line(x1 = a.x, y1 = a.y, x2 = b.x, y2 = b.y, stroke = c,
          `stroke-width` = 1)

randomize()
main()
draw()