Skip to content

Instantly share code, notes, and snippets.

@wmelton
Created November 24, 2017 23:53
Show Gist options
  • Save wmelton/232add8588f64b16a0ff74562a1540a3 to your computer and use it in GitHub Desktop.
Save wmelton/232add8588f64b16a0ff74562a1540a3 to your computer and use it in GitHub Desktop.
Circles Intersect
//Two circles intersect if, and only if, the distance between their centers is between the sum and the difference of their radii. Given two circles (x0,y0,R0) and (x1,y1,R1), the formula is as follows:
ABS(R0-R1) <= SQRT((x0-x1)^2+(y0-y1)^2) <= (R0+R1)
//Squaring both sides lets you avoid the slow SQRT, and stay with ints if your inputs are integer:
(R0-R1)^2 <= (x0-x1)^2+(y0-y1)^2 <= (R0+R1)^2
//Since you need only a yes/no test, this check is faster than calculating the exact intersection points.
//Edit: corrected for the "one circle inside the other" case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment