|
| 1 | +from math import atan2 |
| 2 | + |
| 3 | +#Is the turn counter clockwise? |
| 4 | +def CCW(p1, p2, p3): |
| 5 | + return (p3[1]-p1[1])*(p2[0]-p1[0]) >= (p2[1]-p1[1])*(p3[0]-p1[0]) |
| 6 | + |
| 7 | + |
| 8 | +#Find the point with least y-value. If tie, use point with least x-value. |
| 9 | +def minYPoint(points): |
| 10 | + minYPt = points[0] |
| 11 | + |
| 12 | + for point in points[1:]: |
| 13 | + if point[1] < minYPt[1]: |
| 14 | + minYPt = point |
| 15 | + elif point[1] == minYPt[1] and point[0]<minYPt[0]: |
| 16 | + minYPt = point |
| 17 | + |
| 18 | + return minYPt |
| 19 | + |
| 20 | + |
| 21 | +#Find the polar angle of each point in list relative to a reference point |
| 22 | +def polarAngles(ref, points): |
| 23 | + return [atan2(point[1]-ref[0],point[0]-ref[0]) for point in points] |
| 24 | + |
| 25 | + |
| 26 | +#Sort the list of point by their polar angle |
| 27 | +def sortByPolar(ref, points): |
| 28 | + return [x for _,x in sorted(zip(polarAngles(ref,points),points))] |
| 29 | + |
| 30 | + |
| 31 | +def grahamScan(gift): |
| 32 | + start = minYPoint(gift) #Must be in hull |
| 33 | + gift.remove(start) |
| 34 | + |
| 35 | + S = sortByPolar(start,gift) |
| 36 | + hull = [start,S[0],S[1]] |
| 37 | + |
| 38 | + #Remove points from hull that make the hull concave |
| 39 | + for pt in S[2:]: |
| 40 | + while not CCW(hull[-2],hull[-1],pt): |
| 41 | + del hull[-1] |
| 42 | + hull.append(pt) |
| 43 | + |
| 44 | + return hull |
| 45 | + |
| 46 | + |
| 47 | +def main(): |
| 48 | + testGift = [(-5,2),(5,7),(-6,-12),(-14,-14),(9,9), |
| 49 | + (-1,-1),(-10,11),(-6,15),(-6,-8),(15,-9), |
| 50 | + (7,-7),(-2,-9),(6,-5),(0,14),(2,8)] |
| 51 | + hull = grahamScan(testGift) |
| 52 | + |
| 53 | + print("The points in the hull are:") |
| 54 | + for point in hull: |
| 55 | + print(point) |
| 56 | + |
| 57 | +main() |
0 commit comments