Created
December 19, 2018 18:17
-
-
Save skynet/e39382d2e9e24689e318df9ff147004c to your computer and use it in GitHub Desktop.
Find the intersection points of two circles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d = hypot(B.x - A.x, B.y - A.y) | |
if (d <= A.r + B.r && d >= abs(B.r - A.r)) { | |
ex = (B.x - A.x) / d | |
ey = (B.y - A.y) / d | |
x = (A.r * A.r - B.r * B.r + d * d) / (2 * d) | |
y = sqrt(A.r * A.r - x * x) | |
P1 = { | |
x: A.x + x * ex - y * ey, | |
y: A.y + x * ey + y * ex | |
} | |
P2 = { | |
x: A.x + x * ex + y * ey, | |
y: A.y + x * ey - y * ex | |
} | |
} else { | |
// No Intersection, far outside or one circle within the other | |
P1 = P2 = null | |
} |
Author
skynet
commented
Dec 19, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment