@@ -46,6 +46,14 @@ def ort(self, pt1 , pt2, r):
46
46
else :
47
47
return CCW
48
48
49
+ def cross (self , pt1 , pt2 , r ):
50
+ v = ((pt2 .get_x ()- pt1 .get_x ())* (r .get_y ()- pt2 .get_y ())
51
+ - (pt2 .get_y ()- pt1 .get_y ())* (r .get_x ()- pt2 .get_x ()))
52
+ if v <= 0 :
53
+ return CW
54
+ else :
55
+ return CCW
56
+
49
57
def display_hull (self , hull_points ):
50
58
if hull_points == []:
51
59
print ("No points on the hull list to display" )
@@ -113,25 +121,26 @@ def incremental_convex_hull(self):
113
121
# culculating lower hull #
114
122
for idx in range (N ):
115
123
#print idx
116
- while len (lowerHull ) >= 2 and \
117
- (self .ort (lowerHull [- 2 ], lowerHull [- 1 ], self .points [idx ]) == CCW or \
118
- self .ort (lowerHull [- 2 ], lowerHull [- 1 ], self .points [idx ]) == COLINEAR ):
124
+ while len (lowerHull ) >= 2 and self .cross (lowerHull [- 2 ], lowerHull [- 1 ], self .points [idx ]) == CW :
119
125
lowerHull .pop ()
120
126
lowerHull .append (self .points [idx ])
121
127
122
128
# culculating upper hull #
123
129
self .points .reverse ()
124
130
#self.print_plane()
125
131
for idx in range (N ):
126
- while len (upperHull ) >= 2 and \
127
- (self .ort (lowerHull [- 2 ], lowerHull [- 1 ], self .points [idx ]) == CCW or \
128
- self .ort (lowerHull [- 2 ], lowerHull [- 1 ], self .points [idx ]) == COLINEAR ):
132
+ while len (upperHull ) >= 2 and self .cross (lowerHull [- 2 ], lowerHull [- 1 ], self .points [idx ]) == CW :
129
133
upperHull .pop ()
130
134
upperHull .append (self .points [idx ])
131
135
132
136
#pop common point
133
137
lowerHull .pop ()
134
138
hull = lowerHull + upperHull
139
+ for point in lowerHull :
140
+ point .print_point ()
141
+ print "\n "
142
+ for point in upperHull :
143
+ point .print_point ()
135
144
self .display_hull (lowerHull )
136
145
self .display_hull (upperHull )
137
146
self .display_hull (hull )
0 commit comments