@@ -5,33 +5,33 @@ import sugar
5
5
6
6
type Point [T: SomeNumber ] = tuple [x, y: T]
7
7
8
- proc tup_to_point [T](t: (T, T)): Point [T] =
8
+ func tup_to_point [T](t: (T, T)): Point [T] =
9
9
(x: t[0 ], y: t[1 ])
10
10
11
- proc cross_product [T](p1, p2, p3: Point [T]): T =
11
+ func cross_product [T](p1, p2, p3: Point [T]): T =
12
12
# # Form the cross product of three points. If the result is
13
13
# # - zero, the points are collinear.
14
14
# # - positive, the points form a counter-clockwise "left" turn.
15
15
# # - negative, the points form a clockwise "right" turn.
16
16
(p3.y - p1.y) * (p2.x - p1.x) - (p2.y - p1.y) * (p3.x - p1.x)
17
17
18
- proc polar_angle (reference, point: Point ): float =
18
+ func polar_angle (reference, point: Point ): float =
19
19
# # Find the polar angle of a point relative to a reference point
20
20
arctan2 (float (point.y - reference.y), float (point.x - reference.x))
21
21
22
- proc flipped_point_cmp (pa, pb: Point ): int =
22
+ func flipped_point_cmp (pa, pb: Point ): int =
23
23
# # Compare points first by their y-coordinate, then x-coordinate.
24
24
if (pa.y, pa.x) < (pb.y, pb.x): - 1
25
25
elif pa == pb: 0
26
26
else : 1
27
27
28
- proc graham_scan (gift: seq [Point ]): seq [Point ] =
28
+ func graham_scan (gift: seq [Point ]): seq [Point ] =
29
29
assert (gift.len >= 3 )
30
30
var points = sorted (deduplicate (gift), flipped_point_cmp)
31
31
let pivot = points[0 ]
32
32
# Mimic sorting a sliced sequence without copying
33
33
sort (toOpenArray (points, 1 , high (points)),
34
- proc (pa, pb: Point ): int =
34
+ func (pa, pb: Point ): int =
35
35
if polar_angle (pivot, pa) < polar_angle (pivot, pb): - 1
36
36
else : 1 )
37
37
var
0 commit comments