summaryrefslogtreecommitdiff
path: root/nim/src
diff options
context:
space:
mode:
authorTom Harley2019-04-19 16:11:26 +0100
committerTom Harley2019-04-19 16:11:26 +0100
commit0be2f6f8ce0ec9c17453c59ef9f9d150ac7c3c08 (patch)
tree3b7a58041eb9d264b72c95c44a105a4cca9cd6d0 /nim/src
downloadbezier-0be2f6f8ce0ec9c17453c59ef9f9d150ac7c3c08.tar.gz
bezier-0be2f6f8ce0ec9c17453c59ef9f9d150ac7c3c08.zip

Initial commit

HEADmaster
Diffstat (limited to 'nim/src')
-rw-r--r--nim/src/oggie.nim205
-rw-r--r--nim/src/oggiepkg/submodule.nim1
2 files changed, 206 insertions, 0 deletions
diff --git a/nim/src/oggie.nim b/nim/src/oggie.nim
new file mode 100644
index 0000000..c7346a9
--- /dev/null
+++ b/nim/src/oggie.nim
@@ -0,0 +1,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()
diff --git a/nim/src/oggiepkg/submodule.nim b/nim/src/oggiepkg/submodule.nim
new file mode 100644
index 0000000..7ec3b29
--- /dev/null
+++ b/nim/src/oggiepkg/submodule.nim
@@ -0,0 +1 @@
+proc getWelcomeMessage*(): string = "Hello, World!"