Add generate_polygon, rename Rect -> AxisAlignedBB

This commit is contained in:
Matt Low 2020-11-15 12:24:53 +04:00
parent 726ef4458d
commit 518f0e55ef
3 changed files with 24 additions and 17 deletions

View File

@ -1,5 +1,5 @@
from pygame import draw from pygame import draw
from ..geom import Rect from ..geom import AxisAlignedBB
from .. import WIDTH, HEIGHT, get_cursor_pos from .. import WIDTH, HEIGHT, get_cursor_pos
from ..colors import * from ..colors import *
from ..math import * from ..math import *
@ -10,13 +10,13 @@ class AABBDistance:
title = "AABB Distance" title = "AABB Distance"
def render(self, surface): def render(self, surface):
rect_a = Rect( rect_a = AxisAlignedBB(
-(WIDTH / 8), -(WIDTH / 8),
-(HEIGHT / 8), -(HEIGHT / 8),
WIDTH / 4, WIDTH / 4,
HEIGHT / 4, HEIGHT / 4,
) )
rect_b = Rect( rect_b = AxisAlignedBB(
*get_cursor_pos(), *get_cursor_pos(),
WIDTH / 5, WIDTH / 5,
HEIGHT / 5 HEIGHT / 5
@ -52,7 +52,7 @@ class PointAABBDistance:
title = "Point-AABB Distance" title = "Point-AABB Distance"
def render(self, surface): def render(self, surface):
rect_a = Rect( rect_a = AxisAlignedBB(
-(WIDTH / 8), -(WIDTH / 8),
-(HEIGHT / 8), -(HEIGHT / 8),
WIDTH / 4, WIDTH / 4,

View File

@ -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 .. import WIDTH, HEIGHT, get_cursor_pos, get_inputs
from ..colors import * from ..colors import *
from ..math import * from ..math import *
@ -10,12 +10,7 @@ class SeparatingAxisTheorem:
title = "Separating Axis Theorem" title = "Separating Axis Theorem"
def __init__(self): def __init__(self):
self.shape1 = Polygon(0, 0, [ self.shape1 = generate_polygon(0, 0, sides=4, radius=60)
(0, 60),
(60, 0),
(0, -60),
(-60, 0)
])
self.shape2 = Polygon(*get_cursor_pos(), [ self.shape2 = Polygon(*get_cursor_pos(), [
(60, 60), (60, 60),
@ -83,7 +78,8 @@ class SeparatingAxisTheorem:
overlap = max(min2 - max1, min1 - max2) overlap = max(min2 - max1, min1 - max2)
overlaps.append(overlap) 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) colliding = all(overlap < 0 for overlap in overlaps)
if colliding: if colliding:

View File

@ -1,9 +1,10 @@
from .math import * from .math import *
from .draw import * from .draw import *
import numpy as np import numpy as np
from math import pi, sin, cos
class Rect: class AxisAlignedBB:
def __init__(self, x, y, width, height): def __init__(self, x, y, width, height):
self.x = x self.x = x
@ -24,8 +25,8 @@ class Rect:
self.halfwidth = self.width / 2 self.halfwidth = self.width / 2
self.halfheight = self.height / 2 self.halfheight = self.height / 2
self.cx = self.x + self.width / 2 self.cx = self.x + self.halfwidth
self.cy = self.y + self.height / 2 self.cy = self.y + self.halfheight
self.vertices = [ self.vertices = [
(self.x, self.y), (self.x, self.y),
@ -57,14 +58,14 @@ class Rect:
class Polygon: 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.x = x
self.y = y self.y = y
self.vertices = vertices self.vertices = vertices
self.rotation = rotation
self.length = len(vertices) self.length = len(vertices)
self.origin_x = origin_x self.origin_x = origin_x
self.origin_y = origin_y self.origin_y = origin_y
self.rotation = rotation
self.update_transformation() self.update_transformation()
self.update() self.update()
@ -127,3 +128,13 @@ class Polygon:
def draw(self, surface, colour, width): def draw(self, surface, colour, width):
polygon(surface, colour, self.translated_vertices, width) polygon(surface, colour, self.translated_vertices, width)
pixel(surface, colour, self.x, self.y) 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)