Forked from anonymous/circle_circle_intersection.coffee.py
Last active
December 22, 2015 09:09
-
-
Save zeffii/6450228 to your computer and use it in GitHub Desktop.
This file contains 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
tributary.circle_circle_intersection = (p0, r0, p1, r1) -> | |
# literal translation of | |
# http://paulbourke.net/geometry/circlesphere/tvoght.c | |
# dx and dy are the x, y distances between the circle centers. | |
[x0, y0] = [p0.x, p0.y] | |
[x1, y1] = [p1.x, p1.y] | |
[dx, dy] = [x1 - x0, y1 - y0] | |
# Determine the straight-line distance between the centers. | |
# d = hypot(dx,dy); // Suggested by Keith Briggs | |
d = Math.sqrt((dy*dy) + (dx*dx)); | |
# Check for solvability. quit early | |
return None if (d > (r0 + r1)) or (d < Math.abs(r0 - r1)) | |
# the distance from point 0 to point 2. | |
a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ; | |
# coordinates of point 2. | |
x2 = x0 + (dx * a/d); | |
y2 = y0 + (dy * a/d); | |
# distance from point 2 to either of the intersection points. | |
h = Math.sqrt((r0*r0) - (a*a)); | |
# offsets of the intersection points from point 2. | |
rx = -dy * (h/d); | |
ry = dx * (h/d); | |
# absolute intersection points. | |
[{x: x2 + rx, y: y2 + ry}, {x: x2 - rx, y: y2 - ry}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment