Created
November 24, 2017 23:53
-
-
Save wmelton/232add8588f64b16a0ff74562a1540a3 to your computer and use it in GitHub Desktop.
Circles Intersect
This file contains hidden or 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
//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