Do two circles overlap (intersect)

Jan 27, 2011 at 6:51am
I found a formula that tells me if two circles intersect. I applied it to my code and with the circles that I have the answer came out wrong. I followed the formula exact...so I'm not sure what's going on.

My constructor is setup Circle2D::Circle2D(double x, double y, double radius), where x and y make up the center. My circles are Circle2D c1(2, 2, 5.5) and Circle2D c3(4, 5, 10.5)
According to looking at a graph c1 is inside c3, but they do not overlap. My formula tells me they do though...something is wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool Circle2D::overlaps(Circle2D &otherCircle)
{
	// 'this' points to the object in which calls the function
    double c3Radius = otherCircle.getRadius();
    double c3X = otherCircle.getx();      
    double c3Y = otherCircle.gety();   
    double c1Radius = this->getRadius();         
    double c1X = this->getx();               
    double c1Y = this->gety();   

	/* Circle equation (x - h)^2 + (y - k)^2 = r^2....Basically you just have to find the distance between the two centers.
Now take the two radii and add them up. If they are less than this distance, they won't intersect. */

	double X = c3X - c1X;
	double Y = c3Y - c1Y;
	double D = sqrt( (X*X + Y*Y) );   // distance between two centers
	double sumRad = c3Radius + c1Radius;

	// Testing for overlap
	if (D < sumRad)
		return true; // true means they intersect
	else
		return false;
}
Last edited on Jan 27, 2011 at 6:54am
Jan 27, 2011 at 7:18am
Time for some maths.
x2+y2=r2
(x-x0)2+(y-y0)2=r2
---
Intersect when:
Sum of radii <= Distance between centers
r1+r2 <= √((x2-x1)2+(y2-y1)2)
Removing the root for optimization:
(r1+r2)2 <= (x2-x1)2+(y2-y1)2


Work out all the intermediate variables you have and check if it is the same as either of the lower equations (preferably the lowest, since it's easier to calculate).
Jan 27, 2011 at 7:29am
The first equation is basically what I have just in parts. The only difference is that you say

sum of radii <= Distance between centers. --You say they do intersect

I have read it everywhere as:

sum of radii <= Distance --They don't intersect.
Jan 27, 2011 at 11:04am
Your formula tells you if any of each circle occupies any of the same space as the other circle.

If one circle is completely within the other, your formula will indicate that they do share the same space, even though their boundaries do not cross; they do overlap and your formula is telling you this.

I suggest you get a piece of paper and draw a small circle inside a big circle, and see it for yourself.
Last edited on Jan 27, 2011 at 11:05am
Jan 27, 2011 at 11:09am
Didn't your last question solve the problem of finding whether a circle is completely contained within another circle? You could apply your current formula to decide whether they occupy any of the same space, along with your formula to determine whether one is contained by the other

if they occupy some of the same space and neither one contains the other, then their boundaried must cross.
Last edited on Jan 27, 2011 at 12:13pm
Topic archived. No new replies allowed.