-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraham.py
49 lines (33 loc) · 998 Bytes
/
Graham.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from points import *
def check_orientation(temp):
while len(temp)>=3 and orientation(temp[-3], temp[-2], temp[-1]) !=1:
temp.pop(-2)
def Graham_s_Scan(points):
p = []
Lupper = []
Llowwer = []
convex_hull = []
if len(points)<= 3:
print("Not enough Points Given\n")
return points
# sort the list of points
p = sorted(points, key = lambda k: [k.x, k.y])
# construct Lupper
Lupper.append(p[0])
Lupper.append(p[1])
for i in range(2, len(p)):
Lupper.append(p[i])
check_orientation(Lupper)
for i in range(len(Lupper)):
convex_hull.append(Lupper[i])
# construct Llower
Llowwer.append(p[-1])
Llowwer.append(p[-2])
for i in range(len(p)-3, -1, -1):
Llowwer.append(p[i])
check_orientation(Llowwer)
Llowwer.pop(0)
Llowwer.pop(-1)
for i in range(len(Llowwer)):
convex_hull.append(Llowwer[i])
return convex_hull