Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created July 12, 2012 01:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mbostock/3095037 to your computer and use it in GitHub Desktop.
Save mbostock/3095037 to your computer and use it in GitHub Desktop.
Circle-Circle Intersection
function intersections(a, b) {
var R = a.r,
r = b.r,
dx = b.x - a.x,
dy = b.y - a.y,
d = Math.sqrt(dx * dx + dy * dy),
x = (d * d - r * r + R * R) / (2 * d),
y = Math.sqrt(R * R - x * x);
dx /= d;
dy /= d;
return [
[a.x + dx * x - dy * y, a.y + dy * x + dx * y],
[a.x + dx * x + dy * y, a.y + dy * x - dx * y]
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment