Author Topic: Intersection of two circles  (Read 8559 times)

0 Members and 1 Guest are viewing this topic.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Intersection of two circles
« on: December 11, 2019, 06:30:49 am »
From our lovely Discord chat:

Quote
Zeppelin
I know this comes under complex math, but does anyone know how to calculate the 2 points at which 2 circles intersect

I’m trying to calculate the intersecting points not distance. SMc sent a helpful link, but StxAxTic it would be great if you could explain it further

The link from Mathworld is http://mathworld.wolfram.com/Circle-CircleIntersection.html, but this derivation is severely lacking because it doesn't handle circles in arbitrary positions. Then I realized my old solution had the same problem, so I derived the general case in the last two hours. The notes are three pages long and are a giant ball of vectors / trig. I'll spare the details now, and get down to the distilled result: sample code (a little streamlining and this will go in the toolbox):

Code: QB64: [Select]
  1.  
  2. C1x = -10
  3. C1y = -5
  4. C2x = 20
  5. C2y = 30
  6. r1 = 40
  7. r2 = 50
  8.  
  9. Dx = C1x - C2x
  10. Dy = C1y - C2y
  11.  
  12. E = (Dx ^ 2 + Dy ^ 2 + r2 ^ 2 - r1 ^ 2) / (2 * r2)
  13. F = (-Dx ^ 2 - Dy ^ 2 + r2 ^ 2 - r1 ^ 2) / (2 * r1)
  14.  
  15. a = Dx / E
  16. b = Dy / E
  17. c = Dx / F
  18. d = Dy / F
  19.  
  20. cosbetap = (a + b * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  21. sinbetap = (b + a * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  22. cosbetam = (a - b * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  23. sinbetam = (b - a * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  24.  
  25. cosalphap = (c + d * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  26. sinalphap = (d + c * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  27. cosalpham = (c - d * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  28. sinalpham = (d - c * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  29.  
  30. int1x = C1x + r1 * cosalphap
  31. int1y = C1y + r1 * sinalpham
  32.  
  33. int2x = C2x + r2 * cosbetap
  34. int2y = C2y + r2 * sinbetam
  35.  
  36. CIRCLE (320 + C1x, C1y * -1 + 240), r1, 15
  37. CIRCLE (320 + C2x, C2y * -1 + 240), r2, 15
  38. CIRCLE (320 + int1x, int1y * -1 + 240), 3, 14
  39. CIRCLE (320 + int2x, int2y * -1 + 240), 3, 14
  40.  

... Whoever can derive the equations in there gets a prize from the Librarian.
« Last Edit: December 11, 2019, 06:35:15 am by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Intersection of two circles
« Reply #1 on: December 11, 2019, 06:50:13 am »
Oh, that's an interesting problem.  I may have a go at it.  They share a common sector and that will be my approach.  On first inspection, it looks as if things are slight different if one of the circle's centre lies entirely inside the other circle as opposed to the two centres being outside the other.
« Last Edit: December 11, 2019, 07:56:09 am by Qwerkey »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Intersection of two circles
« Reply #2 on: December 11, 2019, 08:19:24 am »
Hi Qwerk,

I calculated more terms than the demo needs, presumably to cover cases not tested. There are plus and minus solutions to the same equation so you have to choose which ones to toss.
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: Intersection of two circles
« Reply #3 on: December 11, 2019, 11:52:39 am »
Looks familiar: https://www.qb64.org/forum/index.php?topic=1485.0

The above link is full blown demo of this:
Code: QB64: [Select]
  1.  
  2. SUB intersect2Circles (x1, y1, r1, x2, y2, r2, ix1, iy1, ix2, iy2)
  3.     'x1, y1 origin of circle 1 with radius r1
  4.     'x2, y2 origin of circle 2 with radius r2
  5.     'ix1, iy1 is the first point of intersect
  6.     'ix2, iy2 is the 2nd point of intersect
  7.     'if ix1 = ix2 = iy1 = iy2 = 0 then no points returned
  8.  
  9.     DIM d, a, h, Px, pY
  10.     d = distance(x1, y1, x2, y2) 'distance between two origins
  11.     IF r1 + r2 < d THEN
  12.         'PRINT "The circles are too far apart to intersect.": END
  13.         'some signal ???    if ix1 = ix2 = iy1 = iy2 = 0 then no points returned
  14.         ix1 = 0: ix2 = 0: iy1 = 0: iy2 = 0
  15.         EXIT SUB
  16.     END IF
  17.     IF (d < r1 AND r2 + d < r1) OR (d < r2 AND r1 + d < r2) THEN 'one circle is inside the other = no intersect
  18.         ix1 = 0: ix2 = 0: iy1 = 0: iy2 = 0
  19.         EXIT SUB
  20.         'IF ABS(r1 - r2) > 3 THEN
  21.         '    PRINT "No intersect, same center (or nearly so) and different radii (or seemingly so).": END
  22.         'ELSE
  23.         '    PRINT "Infinite intersect, the circles are the same (or nearly so).": END
  24.         'END IF
  25.  
  26.     END IF
  27.     'results
  28.     a = (r1 ^ 2 - r2 ^ 2 + d ^ 2) / (2 * d)
  29.     Px = x1 + a * (x2 - x1) / d
  30.     pY = y1 + a * (y2 - y1) / d
  31.     h = (r1 ^ 2 - a ^ 2) ^ .5
  32.     ix1 = INT(Px - h * (y2 - y1) / d)
  33.     iy1 = INT(pY + h * (x2 - x1) / d)
  34.     'circle x1,y1,2,1 filled
  35.     'PRINT: PRINT "Intersect pt1: "; x1; ", "; y1
  36.     ix2 = INT(Px + h * (y2 - y1) / d)
  37.     iy2 = INT(pY - h * (x2 - x1) / d)
  38.     'circle x2,y2,2,1 filled
  39.     'PRINT: PRINT "Intersect pt2: "; x2; ", "; y2
  40.     'line x1,y1,x2,y2
  41.  
« Last Edit: December 11, 2019, 12:01:42 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Intersection of two circles
« Reply #4 on: December 11, 2019, 11:57:30 am »
That's a great program Bill, if I ever need to make nipples on a tipped over headless snowman. My mother used to tell me. "No! The carrot doesn't go there, but I digress.

Well, my hat's off to you again... and damn, there goes my Donald Trump comb-over, but still, very impressive. Can you tell me when I change the radius, to something like r1 = 140 and r2 = 150, it only gives me one point of intersection?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Intersection of two circles
« Reply #5 on: December 11, 2019, 12:35:16 pm »
Thanks for the hat tilt Pete, long time no see (kinda)!

Say, nice eye bplus. I never noticed your post. I guess you have a leg up in getting the librarians prize, all you need to do is provide the derivation of your code instead of mine. Tee hehe
You're not done when it works, you're done when it's right.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Intersection of two circles
« Reply #6 on: December 11, 2019, 01:20:17 pm »
Static, I don't know your challenge answer because I'm not that great in math. But I did spruce up your program and let the user plug in coordinates first and 2 radius numbers for 2 circles. Then it makes a graph and shows the circles and tells you the coordinate numbers where they intersect. It mostly works, except I did find a problem with this. Whenever they intersect at a 0 coordinate, for some reason it only states the first x and y intersection and not the second one. I have no idea why this happens, can any of you give me help on this? But like I said, it mostly works. To easily find where my problem is, plug in these numbers: X1 = 50, Y1 = 50, Radius = 50, X2 = 75, Y2 = 75, Radius = 75. Or you can just use any 3 numbers the same for the first circle and 3 numbers the same for the second circle that will still intersect. Like I said, it's an interactive program so no need to change the code to plot the points, unless you know how to fix the problem, thanks. Static, this still an incredible program.

Here is what I did with it:

Code: QB64: [Select]
  1. _TITLE "Circles Intersections Finder - by STxAxTIC and Ken G."
  2. Go:
  3. INPUT "X1 = ", C1x
  4. INPUT "Y1 = ", C1y
  5. INPUT "Radius = ", r1
  6. INPUT "X2 = ", c2X
  7. INPUT "Y2 = ", c2Y
  8. INPUT "Radius = ", r2
  9. FOR yy = 0 TO 600 STEP 10
  10.     LINE (320, yy)-(320, yy + 5), _RGB32(255, 255, 255)
  11. NEXT yy
  12. FOR xx = 0 TO 800 STEP 10
  13.     LINE (xx, 240)-(xx + 5, 240), _RGB32(255, 255, 255)
  14. NEXT xx
  15.  
  16. 'C1x = 0
  17. 'C1y = -10
  18. 'c2X = 10
  19. 'c2Y = 30
  20. 'r1 = 40
  21. 'r2 = 50
  22.  
  23. Dx = C1x - c2X
  24. Dy = C1y - c2Y
  25.  
  26. E = (Dx ^ 2 + Dy ^ 2 + r2 ^ 2 - r1 ^ 2) / (2 * r2)
  27. F = (-Dx ^ 2 - Dy ^ 2 + r2 ^ 2 - r1 ^ 2) / (2 * r1)
  28.  
  29. a = Dx / E
  30. b = Dy / E
  31. c = Dx / F
  32. d = Dy / F
  33. ON ERROR GOTO nointersection:
  34. cosbetap = (a + b * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  35. sinbetap = (b + a * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  36. cosbetam = (a - b * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  37. sinbetam = (b - a * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  38.  
  39. cosalphap = (c + d * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  40. sinalphap = (d + c * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  41. cosalpham = (c - d * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  42. sinalpham = (d - c * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  43.  
  44. int1x = C1x + r1 * cosalphap
  45. int1y = C1y + r1 * sinalpham
  46.  
  47. int2x = c2X + r2 * cosbetap
  48. int2y = c2Y + r2 * sinbetam
  49.  
  50. CIRCLE (320 + C1x, C1y * -1 + 240), r1, 15
  51. CIRCLE (320 + c2X, c2Y * -1 + 240), r2, 15
  52. CIRCLE (320 + int1x, int1y * -1 + 240), 3, 14
  53. CIRCLE (320 + int2x, int2y * -1 + 240), 3, 14
  54. LOCATE 1, 1
  55. PRINT "Intersects at: ";
  56. PRINT "("; int1x; ","; int1y; ") and ("; int2x; ","; int2y; ")"
  57. PRINT "Again (Y/N):";
  58. again:
  59. ag$ = INKEY$
  60. IF ag$ = "y" OR ag$ = "Y" THEN GOTO Go:
  61. IF ag$ = "n" OR ag$ = "N" THEN END
  62. GOTO again:
  63. nointersection:
  64. CIRCLE (320 + C1x, C1y * -1 + 240), r1, 15
  65. CIRCLE (320 + c2X, c2Y * -1 + 240), r2, 15
  66. LOCATE 1, 1
  67. INPUT "No intersection, press Enter to try again.", ag2$
  68. GOTO Go:
  69.  

 
« Last Edit: December 11, 2019, 01:21:49 pm by SierraKen »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Intersection of two circles
« Reply #7 on: December 11, 2019, 02:07:09 pm »
Thanks for having a look Ken.

You guys are definitely finding the edge cases and causes for modest IF statements to be edited in. This was expected, namely cause trig functions do funny things at quarter-pi intervals, and there are multi rooted quadratic solutions in there. Next time I'm at my desk I will finish this up. Thanks again all.
You're not done when it works, you're done when it's right.

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Re: Intersection of two circles
« Reply #8 on: December 11, 2019, 04:24:45 pm »
Hey everyone,

Heres a WIP voronoi function.
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. cirs = 25
  4.  
  5. DIM cirX(cirs)
  6. DIM cirY(cirs)
  7.  
  8. rad = 25
  9. FOR n = 1 TO cirs
  10.     cirX(n) = INT(RND * _WIDTH - 1) + 1
  11.     cirY(n) = INT(RND * _HEIGHT - 1) + 1
  12.     CIRCLE (cirX(n), cirY(n)), 2
  13.     PAINT (cirX(n), cirY(n)), _RGB(255, 255, 255)
  14.     CIRCLE (cirX(n), cirY(n)), rad
  15.  
  16.     CLS
  17.  
  18.     FOR x = 1 TO cirs
  19.         FOR n = 1 TO cirs
  20.             dist = SQR(((cirX(x) - (cirX(n))) ^ 2) + ((cirY(x) - cirY(n)) ^ 2))
  21.             IF dist <= rad * 2 THEN
  22.                 Dx = cirX(n) - cirX(x)
  23.                 Dy = cirY(n) - cirY(x)
  24.  
  25.                 E = (Dx ^ 2 + Dy ^ 2 + r2 ^ 2 - r1 ^ 2) / (2 * rad)
  26.                 F = (-Dx ^ 2 - Dy ^ 2 + r2 ^ 2 - r1 ^ 2) / (2 * rad)
  27.  
  28.                 a = Dx / E
  29.                 b = Dy / E
  30.                 c = Dx / F
  31.                 d = Dy / F
  32.  
  33.                 cosbetap = (a + b * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  34.                 sinbetap = (b + a * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  35.                 cosbetam = (a - b * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  36.                 sinbetam = (b - a * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  37.  
  38.                 cosalphap = (c + d * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  39.                 sinalphap = (d + c * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  40.                 cosalpham = (c - d * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  41.                 sinalpham = (d - c * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  42.  
  43.                 ipX = cirX(n) + rad * cosalphap
  44.                 ipY = cirY(n) + rad * sinalpham
  45.  
  46.                 ipX2 = cirX(x) + rad * cosbetap
  47.                 ipY2 = cirY(x) + rad * sinbetam
  48.  
  49.  
  50.                 LINE (ipX, ipY)-(ipX2, ipY2), _RGB(0, 0, 255)
  51.             END IF
  52.  
  53.         NEXT n
  54.     NEXT x
  55.  
  56.     rad = rad + 1
  57.     _DELAY (0.1)
  58.  
  59. LOOP UNTIL rad = 500
  60.  
  61. PRINT "DONE"
  62.  

Zep
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Intersection of two circles
« Reply #9 on: December 11, 2019, 04:36:34 pm »
Here's a version to play around with which doesn't use any math at all:

Code: QB64: [Select]
  1. DIM RedX AS INTEGER, RedY AS INTEGER, RedSize AS INTEGER
  2. DIM GreenX AS INTEGER, GreenY AS INTEGER, GreenSize AS INTEGER
  3. REDIM SHARED OverlapX(10), OverlapY(10)
  4. RedX = 200: RedY = 300: RedSize = 100
  5. GreenX = 500: GreenY = 300: GreenSize = 100
  6.  
  7.  
  8. SCREEN _NEWIMAGE(800, 600, 32)
  9.  
  10.     _LIMIT 30
  11.     CLS , 0
  12.     DrawRed RedX, RedY, RedSize
  13.     DrawGreen GreenX, GreenY, GreenSize
  14.  
  15.     MW = 0
  16.     WHILE _MOUSEINPUT: MW = MW + _MOUSEWHEEL: WEND
  17.     Mx = _MOUSEX: My = _MOUSEY: MB1 = _MOUSEBUTTON(1): MB2 = _MOUSEBUTTON(2)
  18.     IF MB2 AND NOT OM THEN toggle = NOT toggle
  19.     IF toggle THEN
  20.         C$ = "Red"
  21.         IF MB1 THEN RedX = Mx: RedY = My
  22.         RedSize = RedSize + MW
  23.     ELSE
  24.         C$ = "Green"
  25.         IF MB1 THEN GreenX = Mx: GreenY = My
  26.         GreenSize = GreenSize + MW
  27.     END IF
  28.  
  29.     LOCATE 1, 1: PRINT "Moving Circle: "; C$
  30.     LOCATE 2, 1: PRINT "Overlap: ";
  31.     CalculateOverlap
  32.     FOR i = 1 TO UBOUND(OverlapX)
  33.         PRINT "("; OverlapX(i); ","; OverlapY(i); ")";
  34.     NEXT
  35.     _DISPLAY
  36.     OM = MB2
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. SUB CalculateOverlap
  44.     REDIM OverlapX(10), OverlapY(10)
  45.     DIM m AS _MEM: m = _MEMIMAGE(0)
  46.     FOR x = 0 TO _WIDTH - 1
  47.         FOR y = 0 TO _HEIGHT - 1
  48.             IF _MEMGET(m, m.OFFSET + ((y * _WIDTH) + x) * 4, _UNSIGNED LONG) = &HFFFFFF00 THEN
  49.                 _TITLE "Overlap!"
  50.                 count = count + 1
  51.                 OverlapX(count) = x
  52.                 OverlapY(count) = y
  53.                 IF count > 9 THEN EXIT FOR
  54.             END IF
  55.         NEXT
  56.         IF count > 9 THEN EXIT FOR
  57.     NEXT
  58.     REDIM _PRESERVE OverlapX(count), OverlapY(count)
  59.  
  60.  
  61.  
  62. SUB DrawRed (x, y, size)
  63.     DIM Red AS LONG, m AS _MEM, m1 AS _MEM, o AS _OFFSET
  64.     Red = _NEWIMAGE(_WIDTH, _HEIGHT, 32): m = _MEMIMAGE(Red)
  65.  
  66.     D = _DEST: _DEST Red
  67.     CLS , 0
  68.     CIRCLE (x, y), size, &HFFFF0000
  69.     m1 = _MEMIMAGE(D)
  70.     o = 2 'blue, green, red, alpha, so 0, 1, 2, 3.... 2 is the starting offset
  71.     DO UNTIL o > m.SIZE
  72.         IF _MEMGET(m, m.OFFSET + o, _UNSIGNED _BYTE) = 255 THEN
  73.             _MEMPUT m1, m1.OFFSET + o + 1, 255 AS _UNSIGNED _BYTE 'dont forget to put the alpha
  74.             _MEMPUT m1, m1.OFFSET + o, 255 AS _UNSIGNED _BYTE
  75.         END IF
  76.         o = o + 4
  77.     LOOP
  78.  
  79.     _DEST D
  80.     _MEMFREE m
  81.     _MEMFREE m1
  82.     _FREEIMAGE Red
  83.  
  84. SUB DrawGreen (x, y, size)
  85.     DIM Green AS LONG, m AS _MEM, m1 AS _MEM, o AS _OFFSET
  86.     Green = _NEWIMAGE(_WIDTH, _HEIGHT, 32): m = _MEMIMAGE(Green)
  87.  
  88.     D = _DEST: _DEST Green
  89.     CLS , 0
  90.     CIRCLE (x, y), size, &HFF00FF00
  91.     m1 = _MEMIMAGE(D)
  92.     o = 1 'blue, green, red, alpha, so 0, 1, 2, 3.... 1 is the starting offset
  93.     DO UNTIL o > m.SIZE
  94.         IF _MEMGET(m, m.OFFSET + o, _UNSIGNED _BYTE) = 255 THEN
  95.             _MEMPUT m1, m1.OFFSET + o + 2, 255 AS _UNSIGNED _BYTE 'dont forget to put the alpha
  96.             _MEMPUT m1, m1.OFFSET + o, 255 AS _UNSIGNED _BYTE
  97.         END IF
  98.         o = o + 4
  99.     LOOP
  100.  
  101.     _DEST D
  102.     _MEMFREE m
  103.     _MEMFREE m1
  104.     _FREEIMAGE Green

Right mouse toggles between the red and green circle.
Hold the left button down to move a circle.
Use the mouse scroll to change circle size. 
« Last Edit: December 11, 2019, 06:16:11 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Intersection of two circles
« Reply #10 on: December 11, 2019, 05:29:36 pm »
Hey everyone,

Heres a WIP voronoi function.
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. cirs = 25
  4.  
  5. DIM cirX(cirs)
  6. DIM cirY(cirs)
  7.  
  8. rad = 25
  9. FOR n = 1 TO cirs
  10.     cirX(n) = INT(RND * _WIDTH - 1) + 1
  11.     cirY(n) = INT(RND * _HEIGHT - 1) + 1
  12.     CIRCLE (cirX(n), cirY(n)), 2
  13.     PAINT (cirX(n), cirY(n)), _RGB(255, 255, 255)
  14.     CIRCLE (cirX(n), cirY(n)), rad
  15.  
  16.     CLS
  17.  
  18.     FOR x = 1 TO cirs
  19.         FOR n = 1 TO cirs
  20.             dist = SQR(((cirX(x) - (cirX(n))) ^ 2) + ((cirY(x) - cirY(n)) ^ 2))
  21.             IF dist <= rad * 2 THEN
  22.                 Dx = cirX(n) - cirX(x)
  23.                 Dy = cirY(n) - cirY(x)
  24.  
  25.                 E = (Dx ^ 2 + Dy ^ 2 + r2 ^ 2 - r1 ^ 2) / (2 * rad)
  26.                 F = (-Dx ^ 2 - Dy ^ 2 + r2 ^ 2 - r1 ^ 2) / (2 * rad)
  27.  
  28.                 a = Dx / E
  29.                 b = Dy / E
  30.                 c = Dx / F
  31.                 d = Dy / F
  32.  
  33.                 cosbetap = (a + b * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  34.                 sinbetap = (b + a * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  35.                 cosbetam = (a - b * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  36.                 sinbetam = (b - a * SQR(a ^ 2 + b ^ 2 - 1)) / (a ^ 2 + b ^ 2)
  37.  
  38.                 cosalphap = (c + d * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  39.                 sinalphap = (d + c * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  40.                 cosalpham = (c - d * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  41.                 sinalpham = (d - c * SQR(c ^ 2 + d ^ 2 - 1)) / (c ^ 2 + d ^ 2)
  42.  
  43.                 ipX = cirX(n) + rad * cosalphap
  44.                 ipY = cirY(n) + rad * sinalpham
  45.  
  46.                 ipX2 = cirX(x) + rad * cosbetap
  47.                 ipY2 = cirY(x) + rad * sinbetam
  48.  
  49.  
  50.                 LINE (ipX, ipY)-(ipX2, ipY2), _RGB(0, 0, 255)
  51.             END IF
  52.  
  53.         NEXT n
  54.     NEXT x
  55.  
  56.     rad = rad + 1
  57.     _DELAY (0.1)
  58.  
  59. LOOP UNTIL rad = 500
  60.  
  61. PRINT "DONE"
  62.  

Zep

Ho Zep,
 
What's Voronoi have to do with circle Intersects?

Here is quick Voronoi Demo:
Code: QB64: [Select]
  1. _TITLE "Voronoi Demo" 'b+ 2019-12-11
  2. CONST xymax = 700, nPoints = 25
  3. TYPE pType
  4.     x AS SINGLE
  5.     y AS SINGLE
  6.     c AS _UNSIGNED LONG
  7. SCREEN _NEWIMAGE(xymax, xymax, 32)
  8. _SCREENMOVE 300, 20
  9. DIM pts(1 TO nPoints) AS pType
  10. FOR i = 1 TO nPoints
  11.     pts(i).x = xymax * RND
  12.     pts(i).y = xymax * RND
  13.     pts(i).c = _RGB32(155 * RND + 100, -(RND < .5) * 255 * RND, -(RND < .5) * 255 * RND)
  14. FOR i = 1 TO nPoints
  15.     CIRCLE (pts(i).x, pts(i).y), 5, pts(i).c
  16. FOR y = 0 TO xymax
  17.     FOR x = 0 TO xymax
  18.         minD = 49000
  19.         FOR p = 1 TO nPoints
  20.             d = ((pts(p).x - x) ^ 2 + (pts(p).y - y) ^ 2) ^ .5
  21.             IF d < minD THEN minD = d: saveP = p
  22.         NEXT
  23.         PSET (x, y), pts(saveP).c
  24.     NEXT
  25.  

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Intersection of two circles
« Reply #11 on: December 11, 2019, 05:50:42 pm »
I was going to say that code looks a bit like the ray tracing stuff I was shoe horning into my space flight ap, and when when you mentioned quadratics, that cinched it. Interesting stuff.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Intersection of two circles
« Reply #12 on: December 11, 2019, 06:08:53 pm »
Pretty neat SMcNeill. I see there could be more than 2 intersections which could be the problem on Static's and mine. It might just be giving the first 2 and then ending without having the ability to give more. It would need a big overhaul in the code to be able to do that I think so I'm just going to let Static look at his. And I also could be wrong. But it would make sense I think.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Intersection of two circles
« Reply #13 on: December 11, 2019, 06:41:39 pm »
Actually if I had to tackle this, I'd do so like Steve did. Let the computer figure it out. I'm so far out of trig, and I never even took calc. I suppose it is because of what I learned in my high school probability class... which is I probably would never need the stuff.

Pete

Mr. Computer to Steve: Please Mr. Hands, give me an algorithm!

Mr. Hands to Mr. Computer. What's that Mr. Computer, you want me to upload Mr. Sluggo?

Oh no Mr. Hands, not Mr. Sluggo. He'll be mean to me...

Well Mr. Bill's invention, then shut up and loop!!!!!!


 
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Intersection of two circles
« Reply #14 on: December 11, 2019, 10:00:58 pm »
Any method that can be reduced or duplicated by printing the two circles on two pages and holding the pages up to the light to look for intersections is... cute, but I hope we don't need to explain the fallibility of these methods. At this point I have a choice: (i) either spruce up the sample code so it works nicely in all quadrants, or (ii) write up the derivation so then anyone can do it properly. Choices choices... where's my bong?
You're not done when it works, you're done when it's right.