geom-demo/geom/demos/vec_proj.py
2020-11-13 18:46:31 +04:00

30 lines
809 B
Python

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)