1

I'm trying to use this answer to detect whether or not there is an intersection between two circles. The answer says: The above solution should work even for the "one circle inside the other" case.

However, when I try with the following input:

x0: 296
y0: 728
r0: 14

x1: 299
y1: 731
r1: 7

Which looks like this visually:

enter image description here

Then it doesn't seem to work. That is, the following equation fails:

(R0 - R1)^2 <= (x0 - x1)^2 + (y0 - y1)^2 <= (R0 + R1)^2

49 <= 18 <= 441

That is, the function would return false (they do not intersect), when clearly they are intersecting.

Am I doing something wrong, or is the formula not correct?

Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136

2 Answers2

0

The circles are nested, but not intersecting

Alexander
  • 139
  • 8
0

Let's consider two exterior circles, which means they have nothing in common, not even a tangent point.

Such circles verify that the distance between their centers is greater than the sum of their radious.
Thus (pseudocode):

//we don't need to calculate square roots. Squaring is enough and faster.
centerDist2 = (c1x - c2x)^2 + (c1y-c2y)^2
radiousSum2 = (R1 + R2)^2
//condition test
if centerDist2 > radiousSum2
  return false //no intersection
else
  return true

Notice the > in the test. This excludes tangents. If you want them to get included as "intersection" then substitute with >=

Also, due to floating numbers issues you better use some threshold:

littleValue = 0.0001
if centerDist2 > (radiousSum2 - littleValue)
...
Ripi2
  • 7,031
  • 1
  • 17
  • 33