Skip to content

Instantly share code, notes, and snippets.

@skynet
Created December 19, 2018 18:17
Show Gist options
  • Save skynet/e39382d2e9e24689e318df9ff147004c to your computer and use it in GitHub Desktop.
Save skynet/e39382d2e9e24689e318df9ff147004c to your computer and use it in GitHub Desktop.
Find the intersection points of two circles
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
}
@skynet
Copy link
Author

skynet commented Dec 19, 2018

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment