geom-demo/geom/demos/vec_proj.py

33 lines
900 B
Python
Raw Normal View History

from .. import HEIGHT, WIDTH, get_cursor_pos
2020-11-13 07:46:31 -07:00
from ..colors import *
from ..math import *
from ..draw import *
2020-11-13 07:46:31 -07:00
class VecProj:
title = "Vector Projection"
def handle_key_down(self, key):
pass
2020-11-13 07:46:31 -07:00
def render(self, surface):
x1 = -(WIDTH / 4)
y1 = 100
x2 = WIDTH / 4
y2 = 0
2020-11-13 07:46:31 -07:00
cx, cy = get_cursor_pos()
2020-11-13 07:46:31 -07:00
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))
2020-11-13 07:46:31 -07:00
line(surface, GREEN, (cx, cy), nearest_point, 2)
line(surface, WHITE, (cx, cy), (x1, y1), 2)
line(surface, YELLOW, (x1, y1), (x2, y2), 2)
line(surface, RED, (x1, y1), nearest_point, 4)