BerGeo/h2/gift_wrapper.py

62 lines
1.4 KiB
Python
Raw Normal View History

2018-09-17 15:06:20 +00:00
# Use atan2 instead of acos to calc angle; atan2(x,y) of the point we potentially want to add
import random
from collections import namedtuple
import matplotlib.pyplot as plt
2018-09-18 09:54:11 +00:00
from math import atan2, degrees, tau, pi, acos, sqrt
2018-09-17 15:06:20 +00:00
Point = namedtuple('Point', 'x y')
Vector = namedtuple('Vector', 'x y')
def gen_point():
a = random.uniform(1, 5)
b = random.uniform(1, 5)
x_i = random.uniform(1, 5)
p_i = Point(x_i, a * x_i + b)
return p_i
2018-09-18 09:54:11 +00:00
def calc_angle(v: Vector, upside_down=False) -> float:
return (90 - degrees(atan2(v.y, v.x)) - 180*upside_down) % 360
2018-09-17 15:06:20 +00:00
def calc_vector(p1: Point, p2: Point) -> Vector:
return Vector((p2.x - p1.x), (p2.y - p1.y))
2018-09-18 09:54:11 +00:00
2018-09-17 15:06:20 +00:00
def display(points, hull):
x = [point.x for point in points]
y = [point.y for point in points]
h_x = [point.x for point in hull]
h_y = [point.y for point in hull]
plt.plot(h_x, h_y, 'ro-')
plt.scatter(x, y)
plt.show()
def rapper(points: set):
min_pt = min(points)
2018-09-18 09:54:11 +00:00
max_pt = max(points)
2018-09-17 15:06:20 +00:00
hull = [min_pt]
2018-09-18 09:54:11 +00:00
upside_down = False
2018-09-17 15:06:20 +00:00
while True:
hull.append(min(points - {hull[-1]},
2018-09-18 09:54:11 +00:00
key=lambda p: calc_angle(calc_vector(hull[-1], p), upside_down)))
if hull[-1].x == max_pt.x:
upside_down = True
2018-09-17 15:06:20 +00:00
display(points, hull)
if hull[-1] == min_pt:
break
return hull
2018-09-18 09:54:11 +00:00
points = {gen_point() for _ in range(30)}
2018-09-17 15:06:20 +00:00
hull = rapper(points)