Author Topic: Circle Intersect Line  (Read 6004 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Circle Intersect Line
« on: January 29, 2020, 09:25:33 pm »
Changed title to more general topic of Circle Intersect Line, see best answer

This is actually a demo to find the point on a line the shortest distance from a given point off the line.
Code: QB64: [Select]
  1. _TITLE "Draw Circle Tangent to Line at Given Pt" 'B+ 2020-01-29
  2. ' see Find point on line perpendicular to line at another point for how I worked this out
  3. ' This merely demo's the 2 subs I will need for Geonetric Construction
  4.  
  5. ' let p1 = mx(1), my(1) = 1 point of the line
  6. ' let p2 = mx(2), my(2) = 2nd point on line (or segment)
  7. ' let p3 = mx(3), my(3) = origin to circle to make tangent to line = shortest dist to line from that point
  8.  
  9. ' x, y finds the line perpendicular to line formed by p1 and p2 from p3 to x, y
  10.  
  11. CONST xmax = 800, ymax = 600
  12. SCREEN _NEWIMAGE(xmax, ymax, 32)
  13. _SCREENMOVE 300, 40
  14.  
  15.     CLS
  16.     PRINT "2 clicks to form a line, a 3rd click for circle origin:"
  17.     WHILE pi < 3 'get 3 mouse clicks
  18.         _PRINTSTRING (20, 20), SPACE$(25)
  19.         _PRINTSTRING (20, 20), "Need 3 clicks, have" + STR$(pi)
  20.         WHILE _MOUSEINPUT: WEND
  21.         IF _MOUSEBUTTON(1) AND oldMouse = 0 THEN 'new mouse down
  22.             pi = pi + 1
  23.             mx(pi) = _MOUSEX: my(pi) = _MOUSEY
  24.             CIRCLE (mx(pi), my(pi)), 2
  25.             IF pi = 2 THEN 'draw first line segment then line
  26.                 drawLine mx(1), my(1), mx(2), my(2), &HFF000099
  27.                 LINE (mx(1), my(1))-(mx(2), my(2))
  28.             END IF
  29.         END IF
  30.         oldMouse = _MOUSEBUTTON(1)
  31.         _DISPLAY
  32.         _LIMIT 60
  33.     WEND
  34.     _PRINTSTRING (20, 20), SPACE$(25)
  35.     circleTangentXY mx(1), my(1), mx(2), my(2), mx(3), my(3), x, y
  36.     CIRCLE (x, y), 2, &HFFFFFF00 'yellow circle should be on line perpendicular to 3rd click point
  37.     LINE (mx(3), my(3))-(x, y), &HFFFFFF00
  38.     CIRCLE (mx(3), my(3)), SQR((mx(3) - x) ^ 2 + (my(3) - y) ^ 2), &HFFFFFF00
  39.     _DISPLAY
  40.     _DELAY 5
  41.     pi = 0 'point index
  42.  
  43. SUB circleTangentXY (X1, Y1, X2, Y2, xC, yC, findXperp, findYperp)
  44.     'p1 and p2 form a line, with slop and y intersect y0
  45.     'xC, yC is a circle origin
  46.     'we find X, Y such that line x, y to xC, yC is perpendicular to p1, p2 line that is radius of tangent circle
  47.     slope = (Y2 - Y1) / (X2 - X1)
  48.     y0 = slope * (0 - X1) + Y1
  49.     A = slope ^ 2 + 1
  50.     B = 2 * (slope * y0 - slope * yC - xC)
  51.     findXperp = -B / (2 * A)
  52.     findYperp = slope * findXperp + y0
  53.  
  54. SUB drawLine (x1, y1, x2, y2, K AS _UNSIGNED LONG)
  55.     slope = (y2 - y1) / (x2 - x1)
  56.     y0 = slope * (0 - x1) + y1
  57.     LINE (0, y0)-(_WIDTH, slope * _WIDTH + y0), &HFF0000FF
  58.  
  59.  
Draw Circle Tangent to Line.PNG
* Draw Circle Tangent to Line.PNG (Filesize: 10.58 KB, Dimensions: 805x620, Views: 522)
« Last Edit: February 01, 2020, 11:00:14 am by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Draw circle tangent to line at given point origin
« Reply #1 on: January 29, 2020, 09:52:04 pm »
Ah, applied geometry. A personal fav.

I was able to make it fail instantly though, cause I knew what special case to look for - if your first two points are on the same vertical line, the circle doesn't show when you click for the third point. (The calculation becomes trivial in this case so who even cares though right?!)

Challenge: Do it for ellipses next!

... or *any* curve. Actually please beat me to it because this sounds interesting - trivial in fact if you use my technique to go from PSET to DRAW. Hm, I might have to do it now... (by now I mean later)
« Last Edit: January 29, 2020, 11:00:04 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Draw circle tangent to line at given point origin
« Reply #2 on: January 29, 2020, 11:16:18 pm »
Oh Thanks STxAxTIC, division by 0 for verticals, I see.

Here is the fix:
Code: QB64: [Select]
  1. _TITLE "Draw Circle Tangent to Line at Given Pt" 'B+ 2020-01-29
  2. ' see Find point on line perpendicular to line at another point for how I worked this out
  3. ' This merely demo's the 2 subs I will need for Geonetric Construction
  4.  
  5. ' let p1 = mx(1), my(1) = 1 point of the line
  6. ' let p2 = mx(2), my(2) = 2nd point on line (or segment)
  7. ' let p3 = mx(3), my(3) = origin to circle to make tangent to line = shortest dist to line from that point
  8.  
  9. ' x, y finds the line perpendicular to line formed by p1 and p2 from p3 to x, y
  10.  
  11. CONST xmax = 800, ymax = 600
  12. SCREEN _NEWIMAGE(xmax, ymax, 32)
  13. _SCREENMOVE 300, 40
  14.  
  15.     CLS
  16.     lc = lc + 1
  17.     IF lc = 1 THEN
  18.         mx(1) = 400: my(1) = 200: mx(2) = 400: my(2) = 205: mx(3) = 500: my(3) = 300
  19.         drawLine mx(1), my(1), mx(2), my(2), &HFF000099
  20.         circleTangentXY mx(1), my(1), mx(2), my(2), mx(3), my(3), x, y
  21.         CIRCLE (x, y), 2, &HFFFFFF00 'yellow circle should be on line perpendicular to 3rd click point
  22.         LINE (mx(3), my(3))-(x, y), &HFFFFFF00
  23.         CIRCLE (mx(3), my(3)), SQR((mx(3) - x) ^ 2 + (my(3) - y) ^ 2), &HFFFFFF00
  24.         _DISPLAY
  25.         _DELAY 5
  26.         CLS
  27.     END IF
  28.     PRINT "2 clicks to form a line, a 3rd click for circle origin:"
  29.     WHILE pi < 3 'get 3 mouse clicks
  30.         _PRINTSTRING (20, 20), SPACE$(25)
  31.         _PRINTSTRING (20, 20), "Need 3 clicks, have" + STR$(pi)
  32.         WHILE _MOUSEINPUT: WEND
  33.         IF _MOUSEBUTTON(1) AND oldMouse = 0 THEN 'new mouse down
  34.             pi = pi + 1
  35.             mx(pi) = _MOUSEX: my(pi) = _MOUSEY
  36.             CIRCLE (mx(pi), my(pi)), 2
  37.             IF pi = 2 THEN 'draw first line segment then line
  38.                 drawLine mx(1), my(1), mx(2), my(2), &HFF000099
  39.                 LINE (mx(1), my(1))-(mx(2), my(2))
  40.             END IF
  41.         END IF
  42.         oldMouse = _MOUSEBUTTON(1)
  43.         _DISPLAY
  44.         _LIMIT 60
  45.     WEND
  46.     _PRINTSTRING (20, 20), SPACE$(25)
  47.     circleTangentXY mx(1), my(1), mx(2), my(2), mx(3), my(3), x, y
  48.     CIRCLE (x, y), 2, &HFFFFFF00 'yellow circle should be on line perpendicular to 3rd click point
  49.     LINE (mx(3), my(3))-(x, y), &HFFFFFF00
  50.     CIRCLE (mx(3), my(3)), SQR((mx(3) - x) ^ 2 + (my(3) - y) ^ 2), &HFFFFFF00
  51.     _DISPLAY
  52.     _DELAY 5
  53.     pi = 0 'point index
  54.  
  55. SUB circleTangentXY (X1, Y1, X2, Y2, xC, yC, findXperp, findYperp)
  56.     'p1 and p2 form a line, with slop and y intersect y0
  57.     'xC, yC is a circle origin
  58.     'we find X, Y such that line x, y to xC, yC is perpendicular to p1, p2 line that is radius of tangent circle
  59.     IF X2 <> X1 THEN
  60.         slope = (Y2 - Y1) / (X2 - X1)
  61.         y0 = slope * (0 - X1) + Y1
  62.         A = slope ^ 2 + 1
  63.         B = 2 * (slope * y0 - slope * yC - xC)
  64.         findXperp = -B / (2 * A)
  65.         findYperp = slope * findXperp + y0
  66.     ELSE
  67.         findXperp = X1
  68.         findYperp = yC
  69.     END IF
  70.  
  71. SUB drawLine (x1, y1, x2, y2, K AS _UNSIGNED LONG)
  72.     IF x2 <> x1 THEN
  73.         slope = (y2 - y1) / (x2 - x1)
  74.         y0 = slope * (0 - x1) + y1
  75.         LINE (0, y0)-(_WIDTH, slope * _WIDTH + y0), K
  76.     ELSE
  77.         LINE (x1, 0)-(x1, _HEIGHT), K
  78.     END IF
  79.  
  80.  

I see I forgot to set color at K also.
Test vertical fix.PNG
* Test vertical fix.PNG (Filesize: 7.88 KB, Dimensions: 804x621, Views: 514)
« Last Edit: January 29, 2020, 11:18:00 pm by bplus »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Draw circle tangent to line at given point origin
« Reply #3 on: January 30, 2020, 01:04:48 pm »
Nice. I think I'll file this one away as a possible basis for an orbital insertion routine.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Circle Intersect Line
« Reply #4 on: February 01, 2020, 10:58:46 am »
In my haste to do the Triangle Dissection, I did not finish the complete code version of Circle Intersect Line with 3 possible outcomes:
1. no intersect with radius
2. intersect Circle tangent to Line, 1 point
3. intersect 2 places on radius, 2 points located

Here is the more complete coverage of cases:
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))
  116.  
  117.  
« Last Edit: February 01, 2020, 03:16:17 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Circle Intersect Line
« Reply #5 on: February 01, 2020, 03:19:46 pm »
OK the vertical line case has been added to above code and tested with a plug in set but still want to wrap this up into a SUB for a toolbox routine. Thank you math.stackexchange for having formulas worked out and correct.

Marked as best answer by bplus on March 06, 2020, 09:04:17 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Circle Intersect Line
« Reply #6 on: March 06, 2020, 02:00:10 pm »
@STxAxTIC

Oh man! Had I known you were interested in Circle Intersect Line for samples I would have added this update I did to "fix" Hypnotic Polygon Orbits https://www.qb64.org/forum/index.php?topic=2234.0

From last Best Answer (reply #4), I did this, to put code into sub for useful tool:

Code: QB64: [Select]
  1. _TITLE "Sub PointOnLinePerp2Point" 'b+ 2020-02-25 from
  2. ' Find point on line perpendicular to line at another point" 'B+ 2019-12-15
  3. ' let p1 = mx(1), my(1) = 1 point of the line
  4. ' let p2 = mx(2), my(2) = 2nd point on line (or segment)
  5. ' let p3 = mx(3), my(3)
  6. 'See circle tangent to line for best sub routines worked out here
  7.  
  8. CONST xmax = 800, ymax = 600
  9. SCREEN _NEWIMAGE(xmax, ymax, 32)
  10. _SCREENMOVE 300, 40
  11.  
  12.     CLS
  13.     WHILE pi < 3 'get 3 mouse clicks
  14.         _PRINTSTRING (5, 5), SPACE$(20)
  15.         _PRINTSTRING (5, 5), "Need 3 clicks, have" + STR$(pi)
  16.         WHILE _MOUSEINPUT: WEND
  17.         IF _MOUSEBUTTON(1) AND oldMouse = 0 THEN 'new mouse down
  18.             pi = pi + 1
  19.             mx(pi) = _MOUSEX: my(pi) = _MOUSEY
  20.             CIRCLE (mx(pi), my(pi)), 2
  21.             IF pi = 2 THEN 'draw first line segment then line
  22.                 slopeYintersect mx(1), my(1), mx(2), my(2), m, Y0 ' Y0 otherwise know as y Intersect
  23.                 LINE (0, Y0)-(xmax, m * xmax + Y0), &HFF0000FF
  24.                 LINE (mx(1), my(1))-(mx(2), my(2))
  25.             END IF
  26.         END IF
  27.         oldMouse = _MOUSEBUTTON(1)
  28.         _DISPLAY
  29.         _LIMIT 60
  30.     WEND
  31.  
  32.     '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
  33.     ''from inserting eq of line into eq of circle where line intersects circle
  34.     '' https://math.stackexchange.com/questions/228841/how-do-i-calculate-the-intersections-of-a-straight-line-and-a-circle
  35.     'A = m ^ 2 + 1
  36.     'B = 2 * (m * Y0 - m * q - p)
  37.     ''C = q ^ 2 - r ^ 2 + p ^ 2 - 2 * Y0 * q + Y0 ^ 2   <<< don't need
  38.     ''because r is rediculously large the line must intersect at two points likely beyond the screen limits
  39.     ''but the middle of those two points is exactly the point we want
  40.     ''D = B ^ 2 - 4 * A * C
  41.     'IF D < 0 THEN
  42.     '    LOCATE 5, 1
  43.     '    PRINT "m, y0"; m; ","; Y0
  44.     '    PRINT "(p, q)"; p; ","; q
  45.     '    PRINT "A:"; A
  46.     '    PRINT "B:"; B
  47.     '    'PRINT "C:"; C
  48.     '    'PRINT "D:"; D
  49.  
  50.     '    BEEP
  51.     'ELSE
  52.     '    'here is our point!
  53.     '    x = -B / (2 * A)
  54.     '    y = m * x + Y0
  55.     '    PRINT "answer (x, y) = "; x, y
  56.     '    CIRCLE (x, y), 2, &HFFFFFF00 'yellow circle should be on line perprendicular to 3rd click point
  57.     '    LINE (p, q)-(x, y), &HFFFFFF00
  58.     'END IF
  59.  
  60.  
  61.     'test new sub
  62.     PointOnLinePerp2Point mx(1), my(1), mx(2), my(2), mx(3), my(3), Rx, Ry
  63.     PRINT "answer (x, y) = "; Rx, Ry
  64.     CIRCLE (Rx, Ry), 2, &HFFFFFF00 'yellow circle should be on line perprendicular to 3rd click point
  65.     LINE (mx(3), my(3))-(Rx, Ry), &HFFFFFF00
  66.  
  67.     _DISPLAY
  68.     INPUT "press enter to continue, any + enter to quit "; q$
  69.     IF LEN(q$) THEN SYSTEM
  70.     pi = 0 'point index
  71.  
  72. SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept) ' fix for when x1 = x2
  73.     IF X1 = X2 THEN
  74.         slope = X1
  75.         Yintercept = Y2
  76.     ELSE
  77.         slope = (Y2 - Y1) / (X2 - X1)
  78.         Yintercept = slope * (0 - X1) + Y1
  79.     END IF
  80.  
  81. SUB PointOnLinePerp2Point (Lx1, Ly1, Lx2, Ly2, Px, Py, Rx, Ry)
  82.     '
  83.     'this sub needs  SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept) ' fix for when x1 = x2
  84.     '
  85.     'Lx1, Ly1, Lx2, Ly2     the two points that make a line
  86.     'Px, Py is point off the line
  87.     'Rx, Ry Return Point is the Point on the line perpendicular to Px, Py
  88.     slopeYintersect Lx1, Ly1, Lx2, Ly2, m, Y0
  89.     A = m ^ 2 + 1
  90.     B = 2 * (m * Y0 - m * Py - Px)
  91.     Rx = -B / (2 * A)
  92.     Ry = m * Rx + Y0
  93.  
  94.  

The two subs from this added in to here
https://www.qb64.org/forum/index.php?topic=2234.msg114838#msg114838

is what fixed the orbits to the polygon lines! (after changing their rates of orbit to 12ths of each other)

IMHO this IS a 2nd useful result of this geometric construction that can be applied in other programs:
Code: QB64: [Select]
  1. SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept) ' fix for when x1 = x2
  2.     IF X1 = X2 THEN
  3.         slope = X1
  4.         Yintercept = Y2
  5.     ELSE
  6.         slope = (Y2 - Y1) / (X2 - X1)
  7.         Yintercept = slope * (0 - X1) + Y1
  8.     END IF
  9.  
  10. SUB PointOnLinePerp2Point (Lx1, Ly1, Lx2, Ly2, Px, Py, Rx, Ry)
  11.     '
  12.     'this sub needs  SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept) ' fix for when x1 = x2
  13.     '
  14.     'Lx1, Ly1, Lx2, Ly2     the two points that make a line
  15.     'Px, Py is point off the line
  16.     'Rx, Ry Return Point is the Point on the line perpendicular to Px, Py
  17.     slopeYintersect Lx1, Ly1, Lx2, Ly2, m, Y0
  18.     A = m ^ 2 + 1
  19.     B = 2 * (m * Y0 - m * Py - Px)
  20.     Rx = -B / (2 * A)
  21.     Ry = m * Rx + Y0
  22.  

And it looks like STxAxTIC already has a sub returning the points of intersect with a line for a circle ellipse.
https://www.qb64.org/forum/index.php?topic=2302.msg115268#msg115268
« Last Edit: March 06, 2020, 04:28:33 pm by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Circle Intersect Line
« Reply #7 on: March 06, 2020, 02:05:30 pm »
Oh cool bplus! For a second I was iffy because it can be seen as redundant to the elliptical case, but your code was so unique I posted both. In that same spirit, feel free to make a new thread with the fix and stuff.
You're not done when it works, you're done when it's right.