2020-11-14 10:15:47 -07:00
|
|
|
from .math import *
|
|
|
|
from .draw import *
|
2020-11-14 12:23:49 -07:00
|
|
|
import numpy as np
|
|
|
|
|
2020-11-13 07:46:31 -07:00
|
|
|
|
|
|
|
class Rect:
|
|
|
|
|
|
|
|
def __init__(self, x, y, width, height):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.height = height
|
|
|
|
self.width = width
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.min_x = self.x
|
|
|
|
self.min_y = self.y
|
|
|
|
self.max_x = self.x + self.width
|
|
|
|
self.max_y = self.y + self.height
|
|
|
|
|
|
|
|
self.min = (self.min_x, self.min_y)
|
|
|
|
self.max = (self.max_x, self.max_y)
|
|
|
|
|
|
|
|
self.halfwidth = self.width / 2
|
|
|
|
self.halfheight = self.height / 2
|
|
|
|
|
|
|
|
self.cx = self.x + self.width / 2
|
|
|
|
self.cy = self.y + self.height / 2
|
|
|
|
|
|
|
|
self.vertices = [
|
|
|
|
(self.x, self.y),
|
|
|
|
(self.x + self.width, self.y),
|
|
|
|
(self.x + self.width, self.y + self.height),
|
|
|
|
(self.x, self.y + self.height)
|
|
|
|
]
|
|
|
|
self.segments = [
|
|
|
|
(self.vertices[0], self.vertices[1]),
|
|
|
|
(self.vertices[1], self.vertices[2]),
|
|
|
|
(self.vertices[2], self.vertices[3]),
|
|
|
|
(self.vertices[3], self.vertices[0]),
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_center(self):
|
|
|
|
return (self.cx, self.cy)
|
|
|
|
|
|
|
|
def get_center_px(self):
|
|
|
|
return (int(self.cx), int(self.cy))
|
|
|
|
|
|
|
|
def contains(self, x, y):
|
|
|
|
return (x >= self.min_x and x <= self.max_x
|
|
|
|
and y >= self.min_y and y <= self.max_y)
|
|
|
|
|
|
|
|
def draw(self, surface, colour, width):
|
2020-11-14 10:15:47 -07:00
|
|
|
pixel(surface, colour, *self.get_center())
|
|
|
|
rect(surface, colour, self.x, self.y, self.width, self.height, width)
|
|
|
|
|
2020-11-14 12:23:49 -07:00
|
|
|
|
2020-11-14 10:15:47 -07:00
|
|
|
class Polygon:
|
|
|
|
|
2020-11-14 12:23:49 -07:00
|
|
|
def __init__(self, x, y, vertices, origin_x=0, origin_y=0, rotation=0):
|
2020-11-14 10:15:47 -07:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.vertices = vertices
|
2020-11-14 12:23:49 -07:00
|
|
|
self.length = len(vertices)
|
2020-11-14 10:15:47 -07:00
|
|
|
self.origin_x = origin_x
|
|
|
|
self.origin_y = origin_y
|
2020-11-14 12:23:49 -07:00
|
|
|
self.rotation = rotation
|
|
|
|
self.update_transformation()
|
2020-11-14 10:15:47 -07:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.translated_vertices = [
|
2020-11-14 12:23:49 -07:00
|
|
|
(self.x + x, self.y + y)
|
|
|
|
for x, y in self.transformed_vertices
|
|
|
|
]
|
|
|
|
self.segments = [
|
|
|
|
(self.translated_vertices[i],
|
|
|
|
self.translated_vertices[i + 1 if i + 1 < self.length else 0])
|
|
|
|
for i in range(self.length)
|
2020-11-14 10:15:47 -07:00
|
|
|
]
|
2020-11-14 12:23:49 -07:00
|
|
|
self.normals = [
|
|
|
|
normalize(*lnormal(*sub(*v2, *v1)))
|
|
|
|
for v1, v2 in self.segments
|
|
|
|
]
|
|
|
|
|
|
|
|
def update_transformation(self):
|
|
|
|
if self.rotation != 0:
|
|
|
|
rads = math.radians(self.rotation)
|
|
|
|
rot_matrix = np.array([[math.cos(rads), -math.sin(rads)],
|
|
|
|
[math.sin(rads), math.cos(rads)]])
|
|
|
|
|
|
|
|
self.transformed_vertices = [
|
|
|
|
add(*rot_matrix.dot(sub(x, y, self.origin_x, self.origin_y)),
|
|
|
|
self.origin_x, self.origin_y)
|
|
|
|
for x, y in self.vertices
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
self.transformed_vertices = [
|
|
|
|
(self.origin_x + x, self.origin_y + y)
|
|
|
|
for x, y in self.vertices
|
|
|
|
]
|
2020-11-14 10:15:47 -07:00
|
|
|
|
|
|
|
def set_position(self, x, y):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.update()
|
|
|
|
|
2020-11-14 12:23:49 -07:00
|
|
|
def set_rotation(self, rotation):
|
|
|
|
self.rotation = rotation
|
|
|
|
self.update_transformation()
|
|
|
|
|
|
|
|
def rotate(self, degrees):
|
|
|
|
self.set_rotation(self.rotation + degrees)
|
|
|
|
|
2020-11-14 10:15:47 -07:00
|
|
|
def get_normals(self):
|
2020-11-14 12:23:49 -07:00
|
|
|
return self.normals
|
2020-11-14 10:15:47 -07:00
|
|
|
|
|
|
|
def get_translated_vertices(self):
|
|
|
|
return self.translated_vertices
|
|
|
|
|
2020-11-14 12:23:49 -07:00
|
|
|
def get_vertices(self):
|
|
|
|
return self.transformed_vertices
|
|
|
|
|
2020-11-14 10:15:47 -07:00
|
|
|
def get_segments(self):
|
2020-11-14 12:23:49 -07:00
|
|
|
return self.segments
|
2020-11-14 10:15:47 -07:00
|
|
|
|
|
|
|
def draw(self, surface, colour, width):
|
|
|
|
polygon(surface, colour, self.translated_vertices, width)
|
|
|
|
pixel(surface, colour, self.x, self.y)
|