### Author Topic: Circle Intersecting Line by bplus  (Read 1826 times)

0 Members and 1 Guest are viewing this topic.

#### The Librarian

• Moderator
• Newbie
• Posts: 40
##### Circle Intersecting Line by bplus
« on: March 05, 2020, 11:35:49 PM »
Circle Intersecting Line

Author: @bplus
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=2132.0
Version: 2020
Tags: [2d], [geometry], [intersections]

Description:
This is an interactive (mouse-driven) demo that calculates the intersection of any line with any circle.

Source Code:
Code: QB64: [Select]
1. _TITLE "Circle Intersect Line" ' b+ 2020-01-31 develop
2. ' Find point on line perpendicular to line at another point" 'B+ 2019-12-15
3. ' further for a Line and Circle Intersect, making full use of the information from the link below.
4.
5. CONST xmax = 800, ymax = 600
6. SCREEN _NEWIMAGE(xmax, ymax, 32)
7. _SCREENMOVE 300, 40
8.
9.     IF testTangent = 0 THEN 'test plug in set of border conditions not easy to click
10.         PRINT "First set here is a plug in test set for vertical lines."
11.         mx(1) = 200: my(1) = 100: mx(2) = 200: my(2) = 400 'line  x = 200
12.         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
13.         FOR i = 1 TO 4
14.             CIRCLE (mx(i), my(i)), 2
15.         IF mx(1) <> mx(2) THEN
16.             slopeYintersect mx(1), my(1), mx(2), my(2), m, Y0 ' Y0 otherwise know as y Intersect
17.             LINE (0, Y0)-(xmax, m * xmax + Y0), &HFF0000FF
18.             LINE (mx(1), my(1))-(mx(2), my(2))
19.             LINE (mx(1), 0)-(mx(1), ymax), &HFF0000FF
20.             LINE (mx(1), my(1))-(mx(2), my(2))
21.         testTangent = 1
22.         PRINT "First 2 clicks will form a line, 3rd the circle origin and 4th the circle radius:"
23.         WHILE pi < 4 'get 4 mouse clicks
24.             _PRINTSTRING (20, 20), SPACE\$(20)
25.             _PRINTSTRING (20, 20), "Need 4 clicks, have" + STR\$(pi)
26.             IF _MOUSEBUTTON(1) AND oldMouse = 0 THEN 'new mouse down
27.                 pi = pi + 1
28.                 mx(pi) = _MOUSEX: my(pi) = _MOUSEY
29.                 CIRCLE (mx(pi), my(pi)), 2
30.                 IF pi = 2 THEN 'draw first line segment then line
31.                     IF mx(1) <> mx(2) THEN
32.                         slopeYintersect mx(1), my(1), mx(2), my(2), m, Y0 ' Y0 otherwise know as y Intersect
33.                         LINE (0, Y0)-(xmax, m * xmax + Y0), &HFF0000FF
34.                         LINE (mx(1), my(1))-(mx(2), my(2))
35.                         LINE (mx(1), 0)-(mx(1), ymax), &HFF0000FF
36.                         LINE (mx(1), my(1))-(mx(2), my(2))
37.             oldMouse = _MOUSEBUTTON(1)
38.             _LIMIT 60
39.     p = mx(3): q = my(3)
40.     r = SQR((mx(3) - mx(4)) ^ 2 + (my(3) - my(4)) ^ 2)
41.     CIRCLE (p, q), r, &HFFFF0000
42.     IF mx(1) = mx(2) THEN 'line is vertical so if r =
43.         IF r = ABS(mx(1) - mx(3)) THEN ' one point tangent intersect
44.             PRINT "Tangent point is "; TS\$(mx(1)); ", "; TS\$(my(3))
45.             CIRCLE (mx(1), my(3)), 2, &HFFFFFF00
46.             CIRCLE (mx(1), my(3)), 4, &HFFFFFF00
47.         ELSEIF r < ABS(mx(1) - mx(3)) THEN 'no intersect
48.             PRINT "No intersect, radius too small."
49.         ELSE '2 point intersect
50.             ydist = SQR(r ^ 2 - (mx(1) - mx(3)) ^ 2)
51.             y1 = my(3) + ydist
52.             y2 = my(3) - ydist
53.             PRINT "2 Point intersect (x1, y1) = "; TS\$(mx(1)); ", "; TS\$(y1); "  (x2, y2) = "; TS\$(mx(1)); ", "; TS\$(y2)
54.             CIRCLE (mx(1), y1), 2, &HFFFFFF00 'marking intersect points yellow
55.             CIRCLE (mx(1), y2), 2, &HFFFFFF00
56.             CIRCLE (mx(1), y1), 4, &HFFFFFF00 'marking intersect points yellow
57.             CIRCLE (mx(1), y2), 4, &HFFFFFF00
58.
59.         'OK the calculations!
60.         'from inserting eq ofline into eq of circle where line intersects circle see reference
61.         ' https://math.stackexchange.com/questions/228841/how-do-i-calculate-the-intersections-of-a-straight-line-and-a-circle
62.         A = m ^ 2 + 1
63.         B = 2 * (m * Y0 - m * q - p)
64.         C = q ^ 2 - r ^ 2 + p ^ 2 - 2 * Y0 * q + Y0 ^ 2
65.         D = B ^ 2 - 4 * A * C 'telling part of Quadratic formula = 0 then circle is tangent  or > 0 then 2 intersect points
66.         IF D < 0 THEN ' no intersection
67.             PRINT "m, y0 "; TS\$(m); ", "; TS\$(Y0)
68.             PRINT "(p, q) "; TS\$(p); ", "; TS\$(q)
69.             PRINT "A: "; TS\$(A)
70.             PRINT "B: "; TS\$(B)
71.             PRINT "C: "; TS\$(C)
72.             PRINT "D: "; TS\$(D); " negative so no intersect."
73.         ELSEIF D = 0 THEN ' one point tangent
74.             x1 = (-B + SQR(D)) / (2 * A)
75.             y1 = m * x1 + Y0
76.             PRINT "Tangent Point Intersect (x1, y1) = "; TS\$(x1); ", "; TS\$(y1)
77.             CIRCLE (x1, y1), 2, &HFFFFFF00 'yellow circle should be on line perprendicular to 3rd click point
78.             CIRCLE (x1, y1), 4, &HFFFFFF00 'yellow circle should be on line perprendicular to 3rd click point
79.             '2 points
80.             x1 = (-B + SQR(D)) / (2 * A): y1 = m * x1 + Y0
81.             x2 = (-B - SQR(D)) / (2 * A): y2 = m * x2 + Y0
82.             PRINT "2 Point intersect (x1, y1) = "; TS\$(x1); ", "; TS\$(y1); "  (x2, y2) = "; TS\$(x2); ", "; TS\$(y2)
83.             CIRCLE (x1, y1), 2, &HFFFFFF00 'marking intersect points yellow
84.             CIRCLE (x2, y2), 2, &HFFFFFF00
85.             CIRCLE (x1, y1), 4, &HFFFFFF00 'marking intersect points yellow
86.             CIRCLE (x2, y2), 4, &HFFFFFF00
87.     INPUT "press enter to continue, any + enter to quit "; q\$
88.     IF LEN(q\$) THEN SYSTEM
89.     pi = 0 'point index
90.
91. SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept) ' fix for when x1 = x2
92.     slope = (Y2 - Y1) / (X2 - X1)
93.     Yintercept = slope * (0 - X1) + Y1
94.
95. FUNCTION TS\$ (n)
96.     TS\$ = _TRIM\$(STR\$(n))

« Last Edit: September 25, 2021, 08:01:04 AM by Junior Librarian »