From 4f7c4a5699cb37d68f0449de1e1e090cd653b5e6 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Tue, 18 Sep 2018 11:54:11 +0200 Subject: [PATCH] The great fixiroo --- h2/gift_wrapper.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/h2/gift_wrapper.py b/h2/gift_wrapper.py index 8064674..b2a4e54 100644 --- a/h2/gift_wrapper.py +++ b/h2/gift_wrapper.py @@ -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)