diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index e513e8a8d..50199d0d4 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -38,3 +38,5 @@ lulucca12
GuyPozner
+William Boyles +
diff --git a/chapters/computational_geometry/gift_wrapping/jarvis_march/code/py/jarvisMarch.py b/chapters/computational_geometry/gift_wrapping/jarvis_march/code/py/jarvisMarch.py new file mode 100644 index 000000000..781f598a1 --- /dev/null +++ b/chapters/computational_geometry/gift_wrapping/jarvis_march/code/py/jarvisMarch.py @@ -0,0 +1,38 @@ +# Is the turn counter clockwise? +def CCW(p1, p2, p3): + return (p3[1]-p1[1])*(p2[0]-p1[0]) >= (p2[1]-p1[1])*(p3[0]-p1[0]) + + +def jarvisMarch(gift): + n = len(gift) #Number of points in list + pointOnHull = min(gift) #leftmost point in gift + hull = [pointOnHull] #leftmost point guaranteed to be in hull + + while True: + endpoint = gift[0] #Candidate for next point in hull + for j in range(1,n): + if endpoint==pointOnHull or not CCW(gift[j],hull[-1],endpoint): + endpoint = gift[j] + + pointOnHull = endpoint + + #Check if we have completely wrapped gift + if hull[0]==endpoint: + break + else: + hull.append(pointOnHull) + + return hull + + +def main(): + testGift = [(5, 7), (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)] + hull = jarvisMarch(testGift) + + print("The points in the hull are:") + for point in hull: + print(point) + +main() diff --git a/chapters/computational_geometry/gift_wrapping/jarvis_march/jarvis_march.md b/chapters/computational_geometry/gift_wrapping/jarvis_march/jarvis_march.md index dae06205c..62cf086db 100644 --- a/chapters/computational_geometry/gift_wrapping/jarvis_march/jarvis_march.md +++ b/chapters/computational_geometry/gift_wrapping/jarvis_march/jarvis_march.md @@ -41,6 +41,9 @@ Program.cs {% sample lang="js" %} ### JavaScript [import, lang:"javascript"](code/javascript/jarvis-march.js) +{% sample lang="py" %} +### Python +[import, lang:"python"](code/py/jarvisMarch.py) {% endmethod %}