BerGeo/h2/sidedness.py

58 lines
930 B
Python
Raw Normal View History

2018-09-12 08:38:02 +00:00
import random
from collections import namedtuple
from enum import Enum, auto
Point = namedtuple('Point', ['x', 'y'])
class Side(Enum):
ON = auto()
ABOVE = auto()
BELOW = auto()
def sidedness(p1, p2, p3, eps=0.00000001):
y = p1.y * (p3.x - p2.x)
x = p1.x
a = (p3.y - p2.y)
b = p3.y * (p3.x - p2.x) - a * p3.x
if y - eps < a * x + b < y + eps:
return Side.ON
elif y > a * x + b:
return Side.ABOVE
return Side.BELOW
# test
p1 = Point(4, 4)
p2 = Point(0, 0)
p3 = Point(5, 2)
# print(sidedness(p1, p2, p3))
def genPoint():
a = random.uniform(1, 10)
b = random.uniform(1, 10)
p = []
for i in range(3):
x_i = random.uniform(1, 10)
p_i = Point(x_i, a * x_i + b)
p.append(p_i)
return p
hej = genPoint()
print(hej)
print(sidedness(*hej))
def graham_scan():
3