from .. import HEIGHT, WIDTH, get_cursor_pos from ..colors import * from ..math import * from ..draw import * class VecProj: title = "Vector Projection" def handle_key_down(self, key): pass 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)