geom-demo/geom/demos/vec_proj.py
Matt Low c313b3e8ba Refactors
- Move all pygame draw calls into draw module
- Scrap indvidual shape modules for single 'geom' module
- Center of screen is now 0,0
2020-11-14 21:15:47 +04:00

30 lines
850 B
Python

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 = 100
x2 = WIDTH / 4
y2 = 0
cx, cy = get_cursor_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
nearest_point = add(x1, y1, *scale(lnx, lny, proj))
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)