Refactors

- Move all pygame draw calls into draw module
- Scrap indvidual shape modules for single 'geom' module
- Center of screen is now 0,0
This commit is contained in:
2020-11-14 21:15:47 +04:00
parent 699acd1602
commit c313b3e8ba
8 changed files with 129 additions and 53 deletions

View File

@ -1,9 +1,9 @@
from pygame import draw, mouse
from .. import text
from ..geom.rect import Rect
from .. import WIDTH, HEIGHT, text
from pygame import draw
from ..geom import Rect
from .. import WIDTH, HEIGHT, get_cursor_pos
from ..colors import *
from ..math import *
from ..draw import *
class AABBDistance:
@ -11,13 +11,13 @@ class AABBDistance:
def render(self, surface):
rect_a = Rect(
WIDTH / 2 - (WIDTH / 8),
HEIGHT / 2 - (HEIGHT / 8),
-(WIDTH / 8),
-(HEIGHT / 8),
WIDTH / 4,
HEIGHT / 4,
)
rect_b = Rect(
*mouse.get_pos(),
*get_cursor_pos(),
WIDTH / 5,
HEIGHT / 5
)
@ -41,10 +41,10 @@ class AABBDistance:
else:
distance = length(gap_x, gap_y)
text.draw(surface, (10, HEIGHT - 20), WHITE, f"Distance: {distance}")
text_screen(surface, WHITE, (10, HEIGHT - 20), f"Distance: {distance}")
rect_a.draw(surface, YELLOW, 4)
rect_b.draw(surface, WHITE, 4)
draw.line(surface, RED if distance < 0 else GREEN,
line(surface, RED if distance < 0 else GREEN,
rect_a.get_center(), rect_b.get_center(), 2)
class PointAABBDistance:
@ -53,12 +53,12 @@ class PointAABBDistance:
def render(self, surface):
rect_a = Rect(
WIDTH / 2 - (WIDTH / 8),
HEIGHT / 2 - (HEIGHT / 8),
-(WIDTH / 8),
-(HEIGHT / 8),
WIDTH / 4,
HEIGHT / 4,
)
cx, cy = mouse.get_pos()
cx, cy = get_cursor_pos()
# cx, cy = point
dist_x = abs(cx - rect_a.cx)
@ -79,7 +79,7 @@ class PointAABBDistance:
else:
distance = length(gap_x, gap_y)
text.draw(surface, (10, HEIGHT - 20), WHITE, f"Distance: {distance}")
text_screen(surface, WHITE, (10, HEIGHT - 20), f"Distance: {distance}")
rect_a.draw(surface, YELLOW, 4)
draw.line(surface, RED if distance < 0 else GREEN,
line(surface, RED if distance < 0 else GREEN,
rect_a.get_center(), (cx, cy), 2)

View File

@ -1,19 +1,19 @@
from pygame import draw, mouse
from .. import HEIGHT, WIDTH
from .. import HEIGHT, WIDTH, get_cursor_pos
from ..colors import *
from ..math import *
from ..draw import *
class VecProj:
title = "Vector Projection"
def render(self, surface):
x1 = WIDTH / 4
y1 = HEIGHT / 2 + 100
x2 = 0.75 * WIDTH
y2 = HEIGHT / 2
x1 = -(WIDTH / 4)
y1 = 100
x2 = WIDTH / 4
y2 = 0
cx, cy = mouse.get_pos()
cx, cy = get_cursor_pos()
lx, ly = x2 - x1, y2 - y1 # line vector
llen = length(lx, ly)
@ -21,9 +21,9 @@ class VecProj:
lcx, lcy = cx - x1, cy - y1 # line start - cursor vector
proj = dot(lcx, lcy, lx, ly) / llen
nearest_point = add(x1, y1, *scale(lnx, lny, proj))
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)
line(surface, GREEN, (cx, cy), nearest_point, 5)
line(surface, WHITE, (cx, cy), (x1, y1), 5)
line(surface, YELLOW, (x1, y1), (x2, y2), 5)
line(surface, RED, (x1, y1), nearest_point, 5)