BerGeo/h2/util.py

87 lines
2.1 KiB
Python

import random
from collections import namedtuple
from enum import Enum, auto
from typing import Set
from math import cos, sin, sqrt, pi
import matplotlib.pyplot as plt
import numpy as np
Point = namedtuple('Point', 'x y')
Vector = namedtuple('Vector', 'x y')
def gen_point(lower: int = 0, upper: int = 10) -> Point:
a = random.uniform(lower, upper)
b = random.uniform(lower, upper)
#x_i = random.uniform(lower, upper)
#p_i = Point(x_i, a * x_i + b)
return Point(a, b)
def display(points: Set[Point], hull: Set[Point]):
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 gen_circular_point(lower : int = 0, upper: int = 10, radius: int = 5) -> Point:
a = random.uniform(lower, upper) * 2 * pi
r = radius * sqrt(random.uniform(lower, upper))
x = r * cos(a)
y = r * sin(a)
return Point(x,y)
def gen_weird_point(lower : int = 0, upper: int = 10) -> Point:
x = random.uniform(lower, upper)
y = x**2
if x < 0:
return Point(random.uniform(x, -x), y)
return Point(random.uniform(-x, x), y)
def gen_triangular_point(left : Point, right : Point, top : Point):
r1 = random.uniform(0,1)
r2 = random.uniform(0,1)
return Point((1 - sqrt(r1)) * left.x + (sqrt(r1) * (1 - r2)) * right.x + (sqrt(r1) * r2) * top.x,
(1 - sqrt(r1)) * left.y + (sqrt(r1) * (1 - r2)) * right.y + (sqrt(r1) * r2) * top.y)
def display_line_only(points: Set[Point], slope: float, intercept: float, line_points: Set[Point]):
x = [point.x for point in points]
y = [point.y for point in points]
plt.scatter(x, y)
# Plot a line from slope and intercept
axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = intercept + slope * x_vals
for point in line_points:
plt.plot(point.x, point.y, 'go')
plt.plot(x_vals, y_vals, '--')
plt.show()
class Side(Enum):
ON = auto()
ABOVE = auto()
BELOW = auto()