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.
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)
returntrue; // true means they intersect
elsereturnfalse;
}
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).
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.
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.