### Author Topic: Circle Intersecting Circle by STxAxTIC  (Read 6171 times)

0 Members and 1 Guest are viewing this topic.

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

« Last Edit: September 25, 2021, 07:58:31 am by Junior Librarian »