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

0 Members and 1 Guest are viewing this topic.

Offline The Librarian

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

CircleIntersectLine.png
* Circle Intersect Line.bas (Filesize: 5.49 KB, Downloads: 387)
« Last Edit: September 25, 2021, 08:01:04 am by Junior Librarian »