From 518f0e55efb9bd6bf186f876fdc5c61b60175a4a Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 15 Nov 2020 12:24:53 +0400 Subject: [PATCH] Add generate_polygon, rename Rect -> AxisAlignedBB --- geom/demos/aabb.py | 8 ++++---- geom/demos/sat.py | 12 ++++-------- geom/geom.py | 21 ++++++++++++++++----- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/geom/demos/aabb.py b/geom/demos/aabb.py index a836637..2ac8c15 100644 --- a/geom/demos/aabb.py +++ b/geom/demos/aabb.py @@ -1,5 +1,5 @@ from pygame import draw -from ..geom import Rect +from ..geom import AxisAlignedBB from .. import WIDTH, HEIGHT, get_cursor_pos from ..colors import * from ..math import * @@ -10,13 +10,13 @@ class AABBDistance: title = "AABB Distance" def render(self, surface): - rect_a = Rect( + rect_a = AxisAlignedBB( -(WIDTH / 8), -(HEIGHT / 8), WIDTH / 4, HEIGHT / 4, ) - rect_b = Rect( + rect_b = AxisAlignedBB( *get_cursor_pos(), WIDTH / 5, HEIGHT / 5 @@ -52,7 +52,7 @@ class PointAABBDistance: title = "Point-AABB Distance" def render(self, surface): - rect_a = Rect( + rect_a = AxisAlignedBB( -(WIDTH / 8), -(HEIGHT / 8), WIDTH / 4, diff --git a/geom/demos/sat.py b/geom/demos/sat.py index 8d5cf11..c2f8bfd 100644 --- a/geom/demos/sat.py +++ b/geom/demos/sat.py @@ -1,4 +1,4 @@ -from ..geom import Rect, Polygon +from ..geom import Polygon, generate_polygon from .. import WIDTH, HEIGHT, get_cursor_pos, get_inputs from ..colors import * from ..math import * @@ -10,12 +10,7 @@ class SeparatingAxisTheorem: title = "Separating Axis Theorem" def __init__(self): - self.shape1 = Polygon(0, 0, [ - (0, 60), - (60, 0), - (0, -60), - (-60, 0) - ]) + self.shape1 = generate_polygon(0, 0, sides=4, radius=60) self.shape2 = Polygon(*get_cursor_pos(), [ (60, 60), @@ -83,7 +78,8 @@ class SeparatingAxisTheorem: overlap = max(min2 - max1, min1 - max2) overlaps.append(overlap) - self.draw_axis(surface, normal, overlap < 0, min1, max1, min2, max2) + self.draw_axis(surface, normal, overlap < 0, + min1, max1, min2, max2) colliding = all(overlap < 0 for overlap in overlaps) if colliding: diff --git a/geom/geom.py b/geom/geom.py index 7a53dd9..4ffa730 100644 --- a/geom/geom.py +++ b/geom/geom.py @@ -1,9 +1,10 @@ from .math import * from .draw import * import numpy as np +from math import pi, sin, cos -class Rect: +class AxisAlignedBB: def __init__(self, x, y, width, height): self.x = x @@ -24,8 +25,8 @@ class Rect: 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.cx = self.x + self.halfwidth + self.cy = self.y + self.halfheight self.vertices = [ (self.x, self.y), @@ -57,14 +58,14 @@ class Rect: class Polygon: - def __init__(self, x, y, vertices, origin_x=0, origin_y=0, rotation=0): + def __init__(self, x, y, vertices, rotation=0, origin_x=0, origin_y=0): self.x = x self.y = y self.vertices = vertices + self.rotation = rotation self.length = len(vertices) self.origin_x = origin_x self.origin_y = origin_y - self.rotation = rotation self.update_transformation() self.update() @@ -127,3 +128,13 @@ class Polygon: def draw(self, surface, colour, width): polygon(surface, colour, self.translated_vertices, width) pixel(surface, colour, self.x, self.y) + + +def generate_polygon(x, y, rotation=0, sides=3, radius=15): + theta = (2 * pi) / sides + return Polygon(x, y, [ + scale(cos((2 * pi) - i * theta), + sin((2 * pi) - i * theta), + radius) + for i in range(sides) + ], rotation)