58 lines
1.2 KiB
Python
58 lines
1.2 KiB
Python
import random
|
|
from collections import namedtuple
|
|
from enum import Enum, auto
|
|
from typing import Set
|
|
|
|
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 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()
|