I tried the formula given here that is a supposed answer and everyone voted way up although it's seriously flawed. I wrote a program in JavaFX to allow the user to test whether two circles intersect by changing each circles centerX, centerY, and Radius values and this formula absolutely does not work except one way...I can't figure out why but when I move circle 2 near circle 1 it works but when I move circle 1 to the other side near circle 2 it doesn't work.....????? that's a bit odd...figured the formula needed to be tested the opposite way as well so tried that and it doesn't work
if (Math.abs(circle1Radius - circle2Radius) <=
Math.sqrt(Math.pow((circle1X - circle2X), 2)
+ Math.pow((circle1Y - circle2Y), 2)) &&
Math.sqrt(Math.pow((circle1X - circle2X), 2)
+ Math.pow((circle1X - circle2Y), 2)) <=
(circle1Radius + circle2Radius)} {
return true;
} else {
return false;
}
This works:
// dx and dy are the vertical and horizontal distances
double dx = circle2X - circle1X;
double dy = circle2Y - circle1Y;
// Determine the straight-line distance between centers.
double d = Math.sqrt((dy * dy) + (dx * dx));
// Check Intersections
if (d > (circle1Radius + circle2Radius)) {
// No Solution. Circles do not intersect
return false;
} else if (d < Math.abs(circle1Radius - circle2Radius)) {
// No Solution. one circle is contained in the other
return false;
} else {
return true;
}
Go here for the formula Intersection of two circles
The formula used is not my formula all credit goes to Paul Bourke(April 1997)
First calculate the distance d between the center of the circles. d = ||P1 - P0||.
If d > r0 + r1 then there are no solutions, the circles are separate.
If d < |r0 - r1| then there are no solutions because one circle is contained within the other.
If d = 0 and r0 = r1 then the circles are coincident and there are an infinite number of solutions.
Considering the two triangles P0P2P3 and P1P2P3 we can write
a2 + h2 = r02 and b2 + h2 = r12
Using d = a + b we can solve for a,
a = (r02 - r12 + d2 ) / (2 d)
It can be readily shown that this reduces to r0 when the two circles touch at one point, ie: d = r0 + r1
Solve for h by substituting a into the first equation, h2 = r02 - a2
So
P2 = P0 + a ( P1 - P0 ) / d
And finally, P3 = (x3,y3) in terms of P0 = (x0,y0), P1 = (x1,y1) and P2 = (x2,y2), is
x3 = x2 +- h ( y1 - y0 ) / d
y3 = y2 -+ h ( x1 - x0 ) / d