fixed sidedness
This commit is contained in:
parent
69037ec771
commit
c363bb3901
|
@ -14,10 +14,13 @@ class Side(Enum):
|
||||||
|
|
||||||
def sidedness(p1, p2, p3, eps=0.0000001):
|
def sidedness(p1, p2, p3, eps=0.0000001):
|
||||||
|
|
||||||
y = p1.y * (p3.x - p2.x)
|
# Find line from p1 to p2, ask where p3 is in relation to this
|
||||||
x = p1.x
|
|
||||||
a = (p3.y - p2.y)
|
y = p3.y * (p2.x - p1.x)
|
||||||
b = p3.y * (p3.x - p2.x) - a * p3.x
|
x = p3.x
|
||||||
|
|
||||||
|
a = (p2.y - p1.y)
|
||||||
|
b = p2.y * (p2.x - p1.x) - a * p2.x
|
||||||
|
|
||||||
if y - eps < a * x + b < y + eps:
|
if y - eps < a * x + b < y + eps:
|
||||||
return Side.ON
|
return Side.ON
|
||||||
|
@ -47,13 +50,11 @@ def genPoint():
|
||||||
return p_i
|
return p_i
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def graham_scan(points):
|
def graham_scan(points):
|
||||||
|
|
||||||
# A funky issue where both a and b become negative in the sidedness test causes us to have to use
|
# A funky issue where both a and b become negative in the sidedness test causes us to have to use
|
||||||
# Side.ABOVE for both tests, regardless of UH or LH.
|
# Side.ABOVE for both tests, regardless of UH or LH.
|
||||||
|
|
||||||
|
|
||||||
sorted_points = sorted(points, key=lambda p: p.x)
|
sorted_points = sorted(points, key=lambda p: p.x)
|
||||||
|
|
||||||
UH = sorted_points[:2]
|
UH = sorted_points[:2]
|
||||||
|
@ -63,6 +64,7 @@ def graham_scan(points):
|
||||||
while len(UH) > 1 and (sidedness(UH[-2], UH[-1], s) != Side.ABOVE):
|
while len(UH) > 1 and (sidedness(UH[-2], UH[-1], s) != Side.ABOVE):
|
||||||
del UH[-1]
|
del UH[-1]
|
||||||
UH.append(s)
|
UH.append(s)
|
||||||
|
|
||||||
reversed_list = list(reversed(sorted_points))
|
reversed_list = list(reversed(sorted_points))
|
||||||
reversed_list.append(UH[0])
|
reversed_list.append(UH[0])
|
||||||
LH = reversed_list[:2]
|
LH = reversed_list[:2]
|
||||||
|
@ -75,6 +77,7 @@ def graham_scan(points):
|
||||||
|
|
||||||
return UH, LH
|
return UH, LH
|
||||||
|
|
||||||
|
|
||||||
p = [genPoint() for _ in range(30)]
|
p = [genPoint() for _ in range(30)]
|
||||||
UH, LH = graham_scan(p)
|
UH, LH = graham_scan(p)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user