2020-11-14 10:15:47 -07:00
|
|
|
from .. import HEIGHT, WIDTH, get_cursor_pos
|
2020-11-13 07:46:31 -07:00
|
|
|
from ..colors import *
|
|
|
|
from ..math import *
|
2020-11-14 10:15:47 -07:00
|
|
|
from ..draw import *
|
2020-11-13 07:46:31 -07:00
|
|
|
|
|
|
|
class VecProj:
|
|
|
|
|
|
|
|
title = "Vector Projection"
|
|
|
|
|
|
|
|
def render(self, surface):
|
2020-11-14 10:15:47 -07:00
|
|
|
x1 = -(WIDTH / 4)
|
|
|
|
y1 = 100
|
|
|
|
x2 = WIDTH / 4
|
|
|
|
y2 = 0
|
2020-11-13 07:46:31 -07:00
|
|
|
|
2020-11-14 10:15:47 -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
|
2020-11-14 10:15:47 -07:00
|
|
|
nearest_point = add(x1, y1, *scale(lnx, lny, proj))
|
2020-11-13 07:46:31 -07:00
|
|
|
|
2020-11-14 10:15:47 -07:00
|
|
|
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)
|