The great fixiroo

This commit is contained in:
Alexander Munch-Hansen 2018-09-18 11:54:11 +02:00
parent 7ad637fe35
commit 4f7c4a5699
1 changed files with 10 additions and 13 deletions

View File

@ -2,7 +2,7 @@
import random
from collections import namedtuple
import matplotlib.pyplot as plt
from math import atan2, degrees, tau, pi
from math import atan2, degrees, tau, pi, acos, sqrt
Point = namedtuple('Point', 'x y')
Vector = namedtuple('Vector', 'x y')
@ -18,21 +18,14 @@ def gen_point():
return p_i
def calc_angle(v1: Vector) -> float:
# x = atan2(v1.y, v1.x)
theta_deg = atan2(-v1.y, -v1.x) / pi * 180 + 180
lel = (90 - theta_deg) % 360
# degrees = (atan2(-v1.y, -v1.x)) + 180
return lel
def calc_angle(v: Vector, upside_down=False) -> float:
return (90 - degrees(atan2(v.y, v.x)) - 180*upside_down) % 360
def calc_vector(p1: Point, p2: Point) -> Vector:
return Vector((p2.x - p1.x), (p2.y - p1.y))
def display(points, hull):
x = [point.x for point in points]
y = [point.y for point in points]
@ -48,11 +41,15 @@ def display(points, hull):
def rapper(points: set):
min_pt = min(points)
max_pt = max(points)
hull = [min_pt]
upside_down = False
while True:
hull.append(min(points - {hull[-1]},
key=lambda p: calc_angle(calc_vector(hull[-1], p))))
key=lambda p: calc_angle(calc_vector(hull[-1], p), upside_down)))
if hull[-1].x == max_pt.x:
upside_down = True
display(points, hull)
if hull[-1] == min_pt:
break
@ -60,5 +57,5 @@ def rapper(points: set):
return hull
points = {gen_point() for _ in range(10)}
points = {gen_point() for _ in range(30)}
hull = rapper(points)