From 99203c606c2890d4a926442e8fd309e7c2b83f0a Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Thu, 10 Oct 2019 23:06:38 -0400 Subject: [PATCH 1/7] Initial implementation of Graham Scan in Nim --- contents/graham_scan/code/nim/.gitignore | 1 + contents/graham_scan/code/nim/graham.nim | 49 ++++++++++++++++++++++++ contents/graham_scan/graham_scan.md | 6 +++ 3 files changed, 56 insertions(+) create mode 100644 contents/graham_scan/code/nim/.gitignore create mode 100644 contents/graham_scan/code/nim/graham.nim diff --git a/contents/graham_scan/code/nim/.gitignore b/contents/graham_scan/code/nim/.gitignore new file mode 100644 index 000000000..c69e8da45 --- /dev/null +++ b/contents/graham_scan/code/nim/.gitignore @@ -0,0 +1 @@ +graham diff --git a/contents/graham_scan/code/nim/graham.nim b/contents/graham_scan/code/nim/graham.nim new file mode 100644 index 000000000..d185f255e --- /dev/null +++ b/contents/graham_scan/code/nim/graham.nim @@ -0,0 +1,49 @@ +from algorithm import sorted +from math import arctan2 +from sequtils import deduplicate, map +import sugar + +type Point[T: SomeNumber] = tuple[x, y: T] + +proc tup_to_point[T](t: (T, T)): Point[T] = + (x: t[0], y: t[1]) + +proc is_counter_clockwise(p1, p2, p3: Point): bool = + (p3.y - p1.y) * (p2.x - p1.x) >= (p2.y - p1.y) * (p3.x - p1.x) + +proc polar_angle(reference, point: Point): float = + arctan2(float(point.y - reference.y), float(point.x - reference.x)) + +proc flipped_point_cmp(pa, pb: Point): int = + if (pa.y, pa.x) < (pb.y, pb.x): -1 + elif pa == pb: 0 + else: 1 + +proc graham_scan(gift: seq[Point]): seq[Point] = + # TODO make sure input has length >= 3 + let + gift_without_duplicates = sorted(deduplicate(gift), flipped_point_cmp) + start = gift_without_duplicates[0] + candidates = sorted(gift_without_duplicates[1..^1], + proc (pa, pb: Point): int = + if polar_angle(start, pa) < polar_angle(start, pb): -1 + else: 1) + # TODO take the approach outlined in the text where we perform rotations on + # the candidates, rather than add to a new sequence + var hull = @[start, candidates[0], candidates[1]] + for candidate in candidates: + while not is_counter_clockwise(hull[^2], hull[^1], candidate): + discard pop(hull) + add(hull, candidate) + hull + +when isMainModule: + let + test_gift = @[(-5, 2), (5, 7), (-6, -12), (-14, -14), (9, 9), + (-1, -1), (-10, 11), (-6, 15), (-6, -8), (15, -9), + (7, -7), (-2, -9), (6, -5), (0, 14), (2, 8)].map(t => tup_to_point(t)) + hull = graham_scan(test_gift) + echo "Initial gift:" + echo test_gift + echo "Final hull:" + echo hull diff --git a/contents/graham_scan/graham_scan.md b/contents/graham_scan/graham_scan.md index 586d209db..050034ded 100644 --- a/contents/graham_scan/graham_scan.md +++ b/contents/graham_scan/graham_scan.md @@ -32,6 +32,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions [import:18-20, lang="cpp"](code/c++/graham_scan.cpp) {% sample lang="coco" %} [import:4-8, lang="coconut"](code/coconut/graham_scan.coco) +{% sample lang="nim" %} +[import:14-15, lang:"nim"](code/nim/graham.nim) {% endmethod %} If the output of this function is 0, the points are collinear. @@ -66,6 +68,8 @@ In the end, the code should look something like this: [import:26-62, lang="cpp"](code/c++/graham_scan.cpp) {% sample lang="coco" %} [import:17-30, lang="coconut"](code/coconut/graham_scan.coco) +{% sample lang="nim" %} +[import, lang:"nim"](code/nim/graham.nim) {% endmethod %} ### Bibliography @@ -95,6 +99,8 @@ In the end, the code should look something like this: [import, lang="cpp"](code/c++/graham_scan.cpp) {%sample lang="coco" %} [import, lang="coconut"](code/coconut/graham_scan.coco) +{% sample lang="nim" %} +[import, lang:"nim"](code/nim/graham.nim) {% endmethod %}