Skip to content

Commit 99203c6

Browse files
committed
Initial implementation of Graham Scan in Nim
1 parent 18944ea commit 99203c6

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
graham
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from algorithm import sorted
2+
from math import arctan2
3+
from sequtils import deduplicate, map
4+
import sugar
5+
6+
type Point[T: SomeNumber] = tuple[x, y: T]
7+
8+
proc tup_to_point[T](t: (T, T)): Point[T] =
9+
(x: t[0], y: t[1])
10+
11+
proc is_counter_clockwise(p1, p2, p3: Point): bool =
12+
(p3.y - p1.y) * (p2.x - p1.x) >= (p2.y - p1.y) * (p3.x - p1.x)
13+
14+
proc polar_angle(reference, point: Point): float =
15+
arctan2(float(point.y - reference.y), float(point.x - reference.x))
16+
17+
proc flipped_point_cmp(pa, pb: Point): int =
18+
if (pa.y, pa.x) < (pb.y, pb.x): -1
19+
elif pa == pb: 0
20+
else: 1
21+
22+
proc graham_scan(gift: seq[Point]): seq[Point] =
23+
# TODO make sure input has length >= 3
24+
let
25+
gift_without_duplicates = sorted(deduplicate(gift), flipped_point_cmp)
26+
start = gift_without_duplicates[0]
27+
candidates = sorted(gift_without_duplicates[1..^1],
28+
proc (pa, pb: Point): int =
29+
if polar_angle(start, pa) < polar_angle(start, pb): -1
30+
else: 1)
31+
# TODO take the approach outlined in the text where we perform rotations on
32+
# the candidates, rather than add to a new sequence
33+
var hull = @[start, candidates[0], candidates[1]]
34+
for candidate in candidates:
35+
while not is_counter_clockwise(hull[^2], hull[^1], candidate):
36+
discard pop(hull)
37+
add(hull, candidate)
38+
hull
39+
40+
when isMainModule:
41+
let
42+
test_gift = @[(-5, 2), (5, 7), (-6, -12), (-14, -14), (9, 9),
43+
(-1, -1), (-10, 11), (-6, 15), (-6, -8), (15, -9),
44+
(7, -7), (-2, -9), (6, -5), (0, 14), (2, 8)].map(t => tup_to_point(t))
45+
hull = graham_scan(test_gift)
46+
echo "Initial gift:"
47+
echo test_gift
48+
echo "Final hull:"
49+
echo hull

contents/graham_scan/graham_scan.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
3232
[import:18-20, lang="cpp"](code/c++/graham_scan.cpp)
3333
{% sample lang="coco" %}
3434
[import:4-8, lang="coconut"](code/coconut/graham_scan.coco)
35+
{% sample lang="nim" %}
36+
[import:14-15, lang:"nim"](code/nim/graham.nim)
3537
{% endmethod %}
3638

3739
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:
6668
[import:26-62, lang="cpp"](code/c++/graham_scan.cpp)
6769
{% sample lang="coco" %}
6870
[import:17-30, lang="coconut"](code/coconut/graham_scan.coco)
71+
{% sample lang="nim" %}
72+
[import, lang:"nim"](code/nim/graham.nim)
6973
{% endmethod %}
7074

7175
### Bibliography
@@ -95,6 +99,8 @@ In the end, the code should look something like this:
9599
[import, lang="cpp"](code/c++/graham_scan.cpp)
96100
{%sample lang="coco" %}
97101
[import, lang="coconut"](code/coconut/graham_scan.coco)
102+
{% sample lang="nim" %}
103+
[import, lang:"nim"](code/nim/graham.nim)
98104
{% endmethod %}
99105

100106
<script>

0 commit comments

Comments
 (0)