_TITLE "Sub PointOnLinePerp2Point" 'b+ 2020-02-25 from ' Find point on line perpendicular to line at another point" 'B+ 2019-12-15
' let p1 = mx(1), my(1) = 1 point of the line
' let p2 = mx(2), my(2) = 2nd point on line (or segment)
' let p3 = mx(3), my(3)
'See circle tangent to line for best sub routines worked out here
CONST xmax
= 800, ymax
= 600
WHILE pi
< 3 'get 3 mouse clicks pi = pi + 1
IF pi
= 2 THEN 'draw first line segment then line slopeYintersect mx(1), my(1), mx(2), my(2), m, Y0 ' Y0 otherwise know as y Intersect
LINE (0, Y0
)-(xmax
, m
* xmax
+ Y0
), &HFF0000FF LINE (mx
(1), my
(1))-(mx
(2), my
(2))
'p = mx(3): q = my(3) ': r = 800 ' SQR(xmax ^ 2 + ymax ^ 2) ' this is varaiables for max circle that can be clicked on screen r is rediculously big
''from inserting eq of line into eq of circle where line intersects circle
'' https://math.stackexchange.com/questions/228841/how-do-i-calculate-the-intersections-of-a-straight-line-and-a-circle
'A = m ^ 2 + 1
'B = 2 * (m * Y0 - m * q - p)
''C = q ^ 2 - r ^ 2 + p ^ 2 - 2 * Y0 * q + Y0 ^ 2 <<< don't need
''because r is rediculously large the line must intersect at two points likely beyond the screen limits
''but the middle of those two points is exactly the point we want
''D = B ^ 2 - 4 * A * C
'IF D < 0 THEN
' LOCATE 5, 1
' PRINT "m, y0"; m; ","; Y0
' PRINT "(p, q)"; p; ","; q
' PRINT "A:"; A
' PRINT "B:"; B
' 'PRINT "C:"; C
' 'PRINT "D:"; D
' BEEP
'ELSE
' 'here is our point!
' x = -B / (2 * A)
' y = m * x + Y0
' PRINT "answer (x, y) = "; x, y
' CIRCLE (x, y), 2, &HFFFFFF00 'yellow circle should be on line perprendicular to 3rd click point
' LINE (p, q)-(x, y), &HFFFFFF00
'END IF
'test new sub
PointOnLinePerp2Point mx(1), my(1), mx(2), my(2), mx(3), my(3), Rx, Ry
PRINT "answer (x, y) = "; Rx
, Ry
CIRCLE (Rx
, Ry
), 2, &HFFFFFF00 'yellow circle should be on line perprendicular to 3rd click point LINE (mx
(3), my
(3))-(Rx
, Ry
), &HFFFFFF00
INPUT "press enter to continue, any + enter to quit "; q$
pi = 0 'point index
SUB slopeYintersect
(X1
, Y1
, X2
, Y2
, slope
, Yintercept
) ' fix for when x1 = x2 slope = X1
Yintercept = Y2
slope = (Y2 - Y1) / (X2 - X1)
Yintercept = slope * (0 - X1) + Y1
SUB PointOnLinePerp2Point
(Lx1
, Ly1
, Lx2
, Ly2
, Px
, Py
, Rx
, Ry
) '
'this sub needs SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept) ' fix for when x1 = x2
'
'Lx1, Ly1, Lx2, Ly2 the two points that make a line
'Px, Py is point off the line
'Rx, Ry Return Point is the Point on the line perpendicular to Px, Py
slopeYintersect Lx1, Ly1, Lx2, Ly2, m, Y0
A = m ^ 2 + 1
B = 2 * (m * Y0 - m * Py - Px)
Rx = -B / (2 * A)
Ry = m * Rx + Y0