Circle Intersecting CircleAuthor:
@bplus @STxAxTIC Source: qb64.org Forum
URL:
https://www.qb64.org/forum/index.php?topic=1973.0Version: 2019
Tags: [2d], [geometry], [intersections]
Description:
Here we present two (equivalent) methods for calculating the intersection points between any two circles.Source Code:
C1x = -100
C1y = 50
C2x = 100
C2y = 100
r1 = 150
r2 = 100
CIRCLE (320 + C1x
, C1y
* -1 + 240), r1
, 8 CIRCLE (320 + C2x
, C2y
* -1 + 240), r2
, 7
''' Toggle between the two functions here.
'CALL CircleIntersectCircle(C1x, C1y, r1, C2x, C2y, r2, i1x, i1y, i2x, i2y)
CALL CircleIntersectCircle2
(C1x
, C1y
, r1
, C2x
, C2y
, r2
, i1x
, i1y
, i2x
, i2y
) '''
CIRCLE (320 + i1x
, i1y
* -1 + 240), 3, 14 CIRCLE (320 + i2x
, i2y
* -1 + 240), 3, 15
SUB CircleIntersectCircle
(c1x
, c1y
, r1
, c2x
, c2y
, r2
, i1x
, i1y
, i2x
, i2y
) ' Returns i1x, i1y, i2x, i2y
i1x = 0: i1y = 0: i2x = 0: i2y = 0
Dx = c1x - c2x
Dy = c1y - c2y
D2 = Dx * Dx + Dy * Dy
F = (-D2 + r2 * r2 - r1 * r1) / (2 * r1)
a = Dx / F
b = Dy / F
g = a * a + b * b
i1x = c1x + r1 * (a + b * h) / g
i1y = c1y + r1 * (b - a * h) / g
i2x = c1x + r1 * (a - b * h) / g
i2y = c1y + r1 * (b + a * h) / g
SUB CircleIntersectCircle2
(c1x
, c1y
, r1
, c2x
, c2y
, r2
, i1x
, i1y
, i2x
, i2y
) ' Returns i1x, i1y, i2x, i2y
d = ((c1x - c2x) ^ 2 + (c1y - c2y) ^ 2) ^ .5
alpha
= _ACOS((r1
^ 2 + d
^ 2 - r2
^ 2) / (2 * r1
* d
)) angle
= _ATAN2(c2y
- c1y
, c2x
- c1x
) p3x
= c1x
+ x1
* COS(angle
) p3y
= c1y
+ x1
* SIN(angle
) i1x
= p3x
+ l
* COS(angle
- _PI / 2) i1y
= p3y
+ l
* SIN(angle
- _PI / 2) i2x
= p3x
+ l
* COS(angle
+ _PI / 2) i2y
= p3y
+ l
* SIN(angle
+ _PI / 2)