_TITLE "Line Intersect Circle Function" ' b+ 2020-03-09 develop
CONST xmax
= 800, ymax
= 600
IF testTangent
= 0 THEN 'test plug in set of border conditions not easy to click PRINT "First set here is a plug in test set for vertical lines." mx(1) = 200: my(1) = 100: mx(2) = 200: my(2) = 400 'line x = 200
mx(3) = 400: my(3) = 300: mx(4) = 150: my(4) = 300 ' circle origin (center 400, 300) then radius test 200 tangent, 150 more than tangent, 250 short
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)) LINE (mx
(1), 0)-(mx
(1), ymax
), &HFF0000FF LINE (mx
(1), my
(1))-(mx
(2), my
(2)) testTangent = 1
PRINT "First 2 clicks will form a line, 3rd the circle origin and 4th the circle radius:" WHILE pi
< 4 'get 4 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)) LINE (mx
(1), 0)-(mx
(1), ymax
), &HFF0000FF LINE (mx
(1), my
(1))-(mx
(2), my
(2))
p = mx(3): q = my(3) 'draw circle
r
= SQR((mx
(3) - mx
(4)) ^ 2 + (my
(3) - my
(4)) ^ 2)
intersect = lineIntersectCircle%(mx(1), my(1), mx(2), my(2), mx(3), my(3), r, ix1, iy1, ix2, iy2)
PRINT "Intersect at tangent point ("; TS$
(ix1
);
", "; TS$
(iy1
);
")" CIRCLE (ix1
, iy1
), 2, &HFFFFFF00 PRINT "2 point intersect ("; TS$
(ix1
);
", "; TS$
(iy1
);
") and ("; TS$
(ix2
);
", "; TS$
(iy2
);
")" CIRCLE (ix1
, iy1
), 2, &HFFFFFF00 CIRCLE (ix2
, iy2
), 2, &HFFFFFF00
'IF mx(1) = mx(2) THEN 'line is vertical so if r =
' IF r = ABS(mx(1) - mx(3)) THEN ' one point tangent intersect
' PRINT "Tangent point is "; TS$(mx(1)); ", "; TS$(my(3))
' CIRCLE (mx(1), my(3)), 2, &HFFFFFF00
' CIRCLE (mx(1), my(3)), 4, &HFFFFFF00
' ELSEIF r < ABS(mx(1) - mx(3)) THEN 'no intersect
' PRINT "No intersect, radius too small."
' ELSE '2 point intersect
' ydist = SQR(r ^ 2 - (mx(1) - mx(3)) ^ 2)
' y1 = my(3) + ydist
' y2 = my(3) - ydist
' PRINT "2 Point intersect (x1, y1) = "; TS$(mx(1)); ", "; TS$(y1); " (x2, y2) = "; TS$(mx(1)); ", "; TS$(y2)
' CIRCLE (mx(1), y1), 2, &HFFFFFF00 'marking intersect points yellow
' CIRCLE (mx(1), y2), 2, &HFFFFFF00
' CIRCLE (mx(1), y1), 4, &HFFFFFF00 'marking intersect points yellow
' CIRCLE (mx(1), y2), 4, &HFFFFFF00
' END IF
'ELSE
' 'OK the calculations!
' 'from inserting eq ofline into eq of circle where line intersects circle see reference
' ' 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
' D = B ^ 2 - 4 * A * C 'telling part of Quadratic formula = 0 then circle is tangent or > 0 then 2 intersect points
' IF D < 0 THEN ' no intersection
' PRINT "m, y0 "; TS$(m); ", "; TS$(Y0)
' PRINT "(p, q) "; TS$(p); ", "; TS$(q)
' PRINT "A: "; TS$(A)
' PRINT "B: "; TS$(B)
' PRINT "C: "; TS$(C)
' PRINT "D: "; TS$(D); " negative so no intersect."
' ELSEIF D = 0 THEN ' one point tangent
' x1 = (-B + SQR(D)) / (2 * A)
' y1 = m * x1 + Y0
' PRINT "Tangent Point Intersect (x1, y1) = "; TS$(x1); ", "; TS$(y1)
' CIRCLE (x1, y1), 2, &HFFFFFF00 'yellow circle should be on line perprendicular to 3rd click point
' CIRCLE (x1, y1), 4, &HFFFFFF00 'yellow circle should be on line perprendicular to 3rd click point
' ELSE
' '2 points
' x1 = (-B + SQR(D)) / (2 * A): y1 = m * x1 + Y0
' x2 = (-B - SQR(D)) / (2 * A): y2 = m * x2 + Y0
' PRINT "2 Point intersect (x1, y1) = "; TS$(x1); ", "; TS$(y1); " (x2, y2) = "; TS$(x2); ", "; TS$(y2)
' CIRCLE (x1, y1), 2, &HFFFFFF00 'marking intersect points yellow
' CIRCLE (x2, y2), 2, &HFFFFFF00
' CIRCLE (x1, y1), 4, &HFFFFFF00 'marking intersect points yellow
' CIRCLE (x2, y2), 4, &HFFFFFF00
' END IF
'END IF
INPUT "press enter to continue, any + enter to quit "; q$
pi = 0 'point index
' return 0 no Intersect, 1 = tangent 1 point touch, 2 = 2 point intersect
FUNCTION lineIntersectCircle%
(lx1
, ly1
, lx2
, ly2
, cx
, cy
, r
, ix1
, iy1
, ix2
, iy2
) slopeYintersect lx1, ly1, lx2, ly2, m, Y0 ' Y0 otherwise know as y Intersect
' 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 * cy - cx)
C = cy ^ 2 - r ^ 2 + cx ^ 2 - 2 * Y0 * cy + Y0 ^ 2
D = B ^ 2 - 4 * A * C 'telling part of Quadratic formula = 0 then circle is tangent or > 0 then 2 intersect points
IF D
< 0 THEN ' no intersection ix1 = -999: iy1 = -999: ix2 = -999: iy2 = -999: lineIntersectCircle% = 0
x1
= (-B
+ SQR(D
)) / (2 * A
) y1 = m * x1 + Y0
ix1 = x1: iy1 = y1: ix2 = -999: iy2 = -999: lineIntersectCircle% = 1
x1
= (-B
+ SQR(D
)) / (2 * A
): y1
= m
* x1
+ Y0
x2
= (-B
- SQR(D
)) / (2 * A
): y2
= m
* x2
+ Y0
ix1 = x1: iy1 = y1: ix2 = x2: iy2 = y2: lineIntersectCircle% = 2
ix1 = lx1: iy1 = cy: ix2 = -999: iy2 = -999: lineIntersectCircle% = 1
ix1 = -999: iy1 = -999: ix2 = -999: iy2 = -999: lineIntersectCircle% = 0
ydist
= SQR(r
^ 2 - (lx1
- cx
) ^ 2) ix1 = lx1: iy1 = cy + ydist: ix2 = lx1: iy2 = cy - ydist: lineIntersectCircle% = 2
SUB slopeYintersect
(X1
, Y1
, X2
, Y2
, slope
, Yintercept
) ' fix for when x1 = x2 slope = (Y2 - Y1) / (X2 - X1)
Yintercept = slope * (0 - X1) + Y1