Skip to content

Commit b1d367c

Browse files
wmboylesjiegillet
authored andcommitted
Python implementation for Jarvis March (#223)
* Added python3 example for Jarvis March. * Added name to contributors
1 parent 10af60f commit b1d367c

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ lulucca12
3838
<br>
3939
GuyPozner
4040
<br>
41+
William Boyles
42+
<br>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Is the turn counter clockwise?
2+
def CCW(p1, p2, p3):
3+
return (p3[1]-p1[1])*(p2[0]-p1[0]) >= (p2[1]-p1[1])*(p3[0]-p1[0])
4+
5+
6+
def jarvisMarch(gift):
7+
n = len(gift) #Number of points in list
8+
pointOnHull = min(gift) #leftmost point in gift
9+
hull = [pointOnHull] #leftmost point guaranteed to be in hull
10+
11+
while True:
12+
endpoint = gift[0] #Candidate for next point in hull
13+
for j in range(1,n):
14+
if endpoint==pointOnHull or not CCW(gift[j],hull[-1],endpoint):
15+
endpoint = gift[j]
16+
17+
pointOnHull = endpoint
18+
19+
#Check if we have completely wrapped gift
20+
if hull[0]==endpoint:
21+
break
22+
else:
23+
hull.append(pointOnHull)
24+
25+
return hull
26+
27+
28+
def main():
29+
testGift = [(5, 7), (5, 7), (-6, -12), (-14, -14), (9, 9),
30+
(-1, -1), (-10, 11), (-6, 15), (-6, -8), (15, -9),
31+
(7, -7), (-2, -9), (6, -5), (0, 14), (2, 8)]
32+
hull = jarvisMarch(testGift)
33+
34+
print("The points in the hull are:")
35+
for point in hull:
36+
print(point)
37+
38+
main()

chapters/computational_geometry/gift_wrapping/jarvis_march/jarvis_march.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Program.cs
4141
{% sample lang="js" %}
4242
### JavaScript
4343
[import, lang:"javascript"](code/javascript/jarvis-march.js)
44+
{% sample lang="py" %}
45+
### Python
46+
[import, lang:"python"](code/py/jarvisMarch.py)
4447
{% endmethod %}
4548

4649
<script>

0 commit comments

Comments
 (0)