72 lines
1.5 KiB
Python
72 lines
1.5 KiB
Python
# 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
|
|
from math import acos, sqrt
|
|
|
|
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
|
|
|
|
|
|
def calc_angle(v1: Vector, v2: Vector) -> float:
|
|
dot = (v1.x * v2.x) + (v1.y * v2.y)
|
|
len_1 = sqrt(v1.x**2 + v1.y**2)
|
|
len_2 = sqrt(v2.x**2 + v2.y**2)
|
|
|
|
tmp = dot / (len_1 * len_2)
|
|
|
|
# pls
|
|
hmmmmmmm = round(tmp, 6)
|
|
|
|
return acos(hmmmmmmm)
|
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
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)
|
|
hull = [min_pt]
|
|
comp_vec = Vector(0, 1)
|
|
while True:
|
|
hull.append(min(points - {hull[-1]},
|
|
key=lambda p: calc_angle(comp_vec, calc_vector(hull[-1], p))))
|
|
comp_vec = calc_vector(hull[-2], hull[-1])
|
|
|
|
display(points, hull)
|
|
|
|
if hull[-1] == min_pt:
|
|
break
|
|
|
|
return hull
|
|
|
|
|
|
points = {gen_point() for _ in range(30)}
|
|
hull = rapper(points)
|