|
2 | 2 |
|
3 | 3 |
|
4 | 4 | #Is the turn counter clockwise?
|
5 |
| -def counter_Clockwise(p1, p2, p3): |
| 5 | +def counter_clockwise(p1, p2, p3): |
6 | 6 | return (p3[1]-p1[1])*(p2[0]-p1[0]) >= (p2[1]-p1[1])*(p3[0]-p1[0])
|
7 | 7 |
|
8 | 8 |
|
9 | 9 | #Find the polar angle of a point relative to a reference point
|
10 |
| -def polar_Angle(ref, point): |
| 10 | +def polar_angle(ref, point): |
11 | 11 | return atan2(point[1]-ref[1],point[0]-ref[0])
|
12 | 12 |
|
13 | 13 |
|
14 |
| -def graham_Scan(gift): |
15 |
| - start = min(gift, key=lambda p: (p[1],p[0])) #Must be in hull |
| 14 | +def graham_scan(gift): |
| 15 | + start = min(gift, key=lambda p: (p[1], p[0])) #Must be in hull |
16 | 16 | gift.remove(start)
|
17 | 17 |
|
18 |
| - S = sorted(gift,key=lambda point: polar_Angle(start,point)) |
19 |
| - hull = [start,S[0],S[1]] |
| 18 | + s = sorted(gift,key=lambda point: polar_angle(start, point)) |
| 19 | + hull = [start,s[0],s[1]] |
20 | 20 |
|
21 | 21 | #Remove points from hull that make the hull concave
|
22 |
| - for pt in S[2:]: |
23 |
| - while not counter_Clockwise(hull[-2],hull[-1],pt): |
| 22 | + for pt in s[2:]: |
| 23 | + while not counter_clockwise(hull[-2], hull[-1], pt): |
24 | 24 | del hull[-1]
|
25 | 25 | hull.append(pt)
|
26 | 26 |
|
27 | 27 | return hull
|
28 | 28 |
|
29 | 29 |
|
30 | 30 | def main():
|
31 |
| - test_Gift = [(-5,2),(5,7),(-6,-12),(-14,-14),(9,9), |
32 |
| - (-1,-1),(-10,11),(-6,15),(-6,-8),(15,-9), |
33 |
| - (7,-7),(-2,-9),(6,-5),(0,14),(2,8)] |
34 |
| - hull = graham_Scan(test_Gift) |
| 31 | + test_gift = [(-5, 2), (5, 7), (-6, -12), (-14, -14), (9, 9), |
| 32 | + (-1, -1), (-10, 11), (-6, 15), (-6, -8), (15, -9), |
| 33 | + (7, -7), (-2, -9), (6, -5), (0, 14), (2, 8)] |
| 34 | + hull = graham_scan(test_gift) |
35 | 35 |
|
36 | 36 | print("The points in the hull are:")
|
37 | 37 | for point in hull:
|
|
0 commit comments