Initial commit
This commit is contained in:
85
geom/demos/aabb.py
Normal file
85
geom/demos/aabb.py
Normal file
@ -0,0 +1,85 @@
|
||||
from pygame import draw, mouse
|
||||
from .. import text
|
||||
from ..geom.rect import Rect
|
||||
from .. import WIDTH, HEIGHT, text
|
||||
from ..colors import *
|
||||
from ..math import *
|
||||
|
||||
class AABBDistance:
|
||||
|
||||
title = "AABB Distance"
|
||||
|
||||
def render(self, surface):
|
||||
rect_a = Rect(
|
||||
WIDTH / 2 - (WIDTH / 8),
|
||||
HEIGHT / 2 - (HEIGHT / 8),
|
||||
WIDTH / 4,
|
||||
HEIGHT / 4,
|
||||
)
|
||||
rect_b = Rect(
|
||||
*mouse.get_pos(),
|
||||
WIDTH / 5,
|
||||
HEIGHT / 5
|
||||
)
|
||||
|
||||
# cx, cy = center
|
||||
dist_x = abs(rect_b.cx - rect_a.cx)
|
||||
dist_y = abs(rect_b.cy - rect_a.cy)
|
||||
|
||||
gap_x = dist_x - rect_a.halfwidth - rect_b.halfwidth
|
||||
gap_y = dist_y - rect_a.halfheight - rect_b.halfheight
|
||||
|
||||
distance = 0
|
||||
|
||||
if gap_x < 0 and gap_y < 0:
|
||||
distance = max(gap_x, gap_y)
|
||||
else:
|
||||
if gap_x < 0:
|
||||
distance = gap_y
|
||||
elif gap_y < 0:
|
||||
distance = gap_x
|
||||
else:
|
||||
distance = length(gap_x, gap_y)
|
||||
|
||||
text.draw(surface, (10, HEIGHT - 20), WHITE, f"Distance: {distance}")
|
||||
rect_a.draw(surface, YELLOW, 4)
|
||||
rect_b.draw(surface, WHITE, 4)
|
||||
draw.line(surface, RED if distance < 0 else GREEN,
|
||||
rect_a.get_center(), rect_b.get_center(), 2)
|
||||
|
||||
class PointAABBDistance:
|
||||
|
||||
title = "Point-AABB Distance"
|
||||
|
||||
def render(self, surface):
|
||||
rect_a = Rect(
|
||||
WIDTH / 2 - (WIDTH / 8),
|
||||
HEIGHT / 2 - (HEIGHT / 8),
|
||||
WIDTH / 4,
|
||||
HEIGHT / 4,
|
||||
)
|
||||
cx, cy = mouse.get_pos()
|
||||
|
||||
# cx, cy = point
|
||||
dist_x = abs(cx - rect_a.cx)
|
||||
dist_y = abs(cy - rect_a.cy)
|
||||
|
||||
gap_x = dist_x - rect_a.halfwidth
|
||||
gap_y = dist_y - rect_a.halfheight
|
||||
|
||||
distance = 0
|
||||
|
||||
if gap_x < 0 and gap_y < 0:
|
||||
distance = max(gap_x, gap_y)
|
||||
else:
|
||||
if gap_x < 0:
|
||||
distance = gap_y
|
||||
elif gap_y < 0:
|
||||
distance = gap_x
|
||||
else:
|
||||
distance = length(gap_x, gap_y)
|
||||
|
||||
text.draw(surface, (10, HEIGHT - 20), WHITE, f"Distance: {distance}")
|
||||
rect_a.draw(surface, YELLOW, 4)
|
||||
draw.line(surface, RED if distance < 0 else GREEN,
|
||||
rect_a.get_center(), (cx, cy), 2)
|
29
geom/demos/vec_proj.py
Normal file
29
geom/demos/vec_proj.py
Normal file
@ -0,0 +1,29 @@
|
||||
from pygame import draw, mouse
|
||||
from .. import HEIGHT, WIDTH
|
||||
from ..colors import *
|
||||
from ..math import *
|
||||
|
||||
class VecProj:
|
||||
|
||||
title = "Vector Projection"
|
||||
|
||||
def render(self, surface):
|
||||
x1 = WIDTH / 4
|
||||
y1 = HEIGHT / 2 + 100
|
||||
x2 = 0.75 * WIDTH
|
||||
y2 = HEIGHT / 2
|
||||
|
||||
cx, cy = mouse.get_pos()
|
||||
|
||||
lx, ly = x2 - x1, y2 - y1 # line vector
|
||||
llen = length(lx, ly)
|
||||
lnx, lny = lx / llen, ly / llen # line normal
|
||||
lcx, lcy = cx - x1, cy - y1 # line start - cursor vector
|
||||
|
||||
proj = dot(lcx, lcy, lx, ly) / llen
|
||||
|
||||
draw.line(
|
||||
surface, GREEN, (cx, cy), add(x1, y1, *scale(lnx, lny, proj)), 5
|
||||
)
|
||||
draw.line(surface, WHITE, (cx, cy), (x1, y1), 5)
|
||||
draw.line(surface, YELLOW, (x1, y1), (x2, y2), 5)
|
Reference in New Issue
Block a user