I'm looking for an algorithm to detect if a circle intersects with any other circle in the same plane (given that there can be more than one circle in a plane).
One method I have found is to do the separating axis test. It says:
Two objects don't intersect if you can find a line that separates the two objects, i.e. a line such that all objects or points of an object are on different sides of the line.
However, I don't know how to apply this method to my case.
Can anybody help me?
To do this, you need to work out the radius and the centre of each circle. If the sum of the radii and the distance between the centres are equal, then the circles touch externally. If the difference between the radii and the distance between the centres are equal, then the circles touch internally.
Two circles may intersect in two imaginary points, a single degenerate point, or two distinct points. The intersections of two circles determine a line known as the radical line.
The common tangent which is formed by touching the top ends of both the circles. Therefore, The common tangents which can be drawn if two circles are touching externally at a single point = 3.
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.
Assuming filled circle intersection (ie: One circle inside another is an intersection).
Where:
Code:
boolean intersects = Math.hypot(x0-x1, y0-y1) <= (r0 + r1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With