Author Topic: Circle Intersecting Circle by STxAxTIC  (Read 3499 times)

0 Members and 1 Guest are viewing this topic.

Offline The Librarian

  • Moderator
  • Newbie
  • Posts: 39
Circle Intersecting Circle by STxAxTIC
« on: March 05, 2020, 11:06:44 pm »
Circle Intersecting Circle

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

Description:
Here we present two (equivalent) methods for calculating the intersection points between any two circles.

Source Code:
Code: QB64: [Select]
  1.  
  2. C1x = -100
  3. C1y = 50
  4. C2x = 100
  5. C2y = 100
  6. r1 = 150
  7. r2 = 100
  8.  
  9.         IF _MOUSEBUTTON(1) THEN
  10.             C2x = _MOUSEX - 320
  11.             C2y = 240 - _MOUSEY
  12.         END IF
  13.         IF _MOUSEBUTTON(2) THEN
  14.             C1x = _MOUSEX - 320
  15.             C1y = 240 - _MOUSEY
  16.         END IF
  17.     LOOP
  18.  
  19.     CLS
  20.     CIRCLE (320 + C1x, C1y * -1 + 240), r1, 8
  21.     CIRCLE (320 + C2x, C2y * -1 + 240), r2, 7
  22.  
  23.     ''' Toggle between the two functions here.
  24.     'CALL CircleIntersectCircle(C1x, C1y, r1, C2x, C2y, r2, i1x, i1y, i2x, i2y)
  25.     CALL CircleIntersectCircle2(C1x, C1y, r1, C2x, C2y, r2, i1x, i1y, i2x, i2y)
  26.     '''
  27.     LOCATE 1, 1: PRINT i1x, i1y, i2x, i2y
  28.  
  29.     IF (i1x OR i1y OR i2x OR i2y) THEN
  30.         CIRCLE (320 + i1x, i1y * -1 + 240), 3, 14
  31.         CIRCLE (320 + i2x, i2y * -1 + 240), 3, 15
  32.     END IF
  33.  
  34.     _DISPLAY
  35.     _LIMIT 30
  36.  
  37. SUB CircleIntersectCircle (c1x, c1y, r1, c2x, c2y, r2, i1x, i1y, i2x, i2y)
  38.     ' Returns i1x, i1y, i2x, i2y
  39.     i1x = 0: i1y = 0: i2x = 0: i2y = 0
  40.     Dx = c1x - c2x
  41.     Dy = c1y - c2y
  42.     D2 = Dx * Dx + Dy * Dy
  43.     IF (D2 ^ .5 < (r1 + r2)) THEN
  44.         F = (-D2 + r2 * r2 - r1 * r1) / (2 * r1)
  45.         a = Dx / F
  46.         b = Dy / F
  47.         g = a * a + b * b
  48.         IF (g > 1) THEN
  49.             h = SQR(g - 1)
  50.             i1x = c1x + r1 * (a + b * h) / g
  51.             i1y = c1y + r1 * (b - a * h) / g
  52.             i2x = c1x + r1 * (a - b * h) / g
  53.             i2y = c1y + r1 * (b + a * h) / g
  54.         END IF
  55.     END IF
  56.  
  57. SUB CircleIntersectCircle2 (c1x, c1y, r1, c2x, c2y, r2, i1x, i1y, i2x, i2y)
  58.     ' Returns i1x, i1y, i2x, i2y
  59.     d = ((c1x - c2x) ^ 2 + (c1y - c2y) ^ 2) ^ .5
  60.     alpha = _ACOS((r1 ^ 2 + d ^ 2 - r2 ^ 2) / (2 * r1 * d))
  61.     x1 = r1 * COS(alpha)
  62.     l = r1 * SIN(alpha)
  63.     angle = _ATAN2(c2y - c1y, c2x - c1x)
  64.     p3x = c1x + x1 * COS(angle)
  65.     p3y = c1y + x1 * SIN(angle)
  66.     i1x = p3x + l * COS(angle - _PI / 2)
  67.     i1y = p3y + l * SIN(angle - _PI / 2)
  68.     i2x = p3x + l * COS(angle + _PI / 2)
  69.     i2y = p3y + l * SIN(angle + _PI / 2)

CircleintersectCircle.png
* CircleIntersectCircle.bas (Filesize: 1.98 KB, Downloads: 391)
« Last Edit: September 25, 2021, 07:58:31 am by Junior Librarian »