Skip to content

Instantly share code, notes, and snippets.

@johan
Forked from mbostock/intersections.js
Created October 20, 2012 06:38
Show Gist options
  • Save johan/3922394 to your computer and use it in GitHub Desktop.
Save johan/3922394 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