Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions contents/jarvis_march/code/coconut/jarvis_march.coco
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
data point(x=0, y=0):
def __str__(self):
return f'({self.x}, {self.y})'

# Is the turn counter-clockwise?
def counter_clockwise(p1 is point, p2 is point, p3 is point) =
(p3.y - p1.y) * (p2.x - p1.x) >= (p2.y - p1.y) * (p3.x - p1.x)


def jarvis_march(gift: point[]) -> point[]:
point_on_hull = min(gift) # The leftmost point in the gift
hull = [point_on_hull] # It is guaranteed it will be on the hull.

while True:
# Candidate for the next point in the hull
endpoint = gift[0]
for p in gift:
if (endpoint == point_on_hull
or not counter_clockwise(p, hull[-1], endpoint)):
endpoint = p

point_on_hull = endpoint

# Check if the gift is completely covered.
if hull[0] == endpoint:
return hull
hull.append(point_on_hull)


if __name__ == '__main__':
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 -> point(*t)) |> list
hull = jarvis_march(test_gift)

print("[#] The points in the hull are:")
for point in hull:
print(point)
2 changes: 2 additions & 0 deletions contents/jarvis_march/jarvis_march.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Since this algorithm, there have been many other algorithms that have advanced t
[import, lang:"v"](code/v/jarvis.v)
{% sample lang="rust" %}
[import, lang:"rust"](code/rust/jarvis_march.rs)
{% sample lang="coco" %}
[import, lang:"coconut"](code/coconut/jarvis_march.coco)
{% endmethod %}

<script>
Expand Down