diff --git a/ch7/polygon.py b/ch7/polygon.py index 54cf62f..2db6ddd 100644 --- a/ch7/polygon.py +++ b/ch7/polygon.py @@ -16,10 +16,7 @@ def __init__(self, x, y): # constructor self.x = x self.y = y - def __sub__(self, b): #tovec(a,b)==b-a - return point(self.x-b.x, self.y-b.y) - def __lt__(self, b): #self &P) { # double ans = 0.0; @@ -73,7 +80,7 @@ def cross(a, b): return a.x*b.y-a.y*b.x # return fabs(ans)/2.0; # } -def ccw(p, q, r): return (cross(q-p, r-p) > 0) +def ccw(p, q, r): return (cross(toVec(p,q),toVec(p,r)) > 0) # note python i used class opperators for tovec (Agis Daniels) # // note: to accept collinear points, we have to change the `> 0' # // returns true if point r is on the left side of line pq @@ -181,8 +188,8 @@ def CH_Andrew(ps): def f(B): #f is a mapping of the two loops since its dup code for p in P: while len(H)>B and not ccw(H[-2], H[-1], p): H.pop() - H.append(p) - H.pop() + H.append(p) + H.pop() f(1); P=P[::-1]; f(len(H)+1); return H #4 line low, rev, up, ret #c++ implementation below # vector CH_Andrew(vector &Pts) { // overall O(n log n)