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 integers:
(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.
The above solution should work even for the "one circle inside the other" case.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…