Reputation: 670
I have these three methods to check if a circle is inside another circle, everything works except for the fact that the intersecting circle is marked as inside and intersecting. I've been reading articles but none of the suggested options seem to make it work correctly. Here are my methods:
public boolean isInside(Circle c2) {
// get the distance between the two center points
double distance = Math.sqrt((x - c2.x) * (x - c2.x) + (y - c2.y) * (y - c2.y));
// check to see if we are inside the first circle, if so then return
// true, otherwise return false.
if (distance <= ((radius) + (c2.radius))) {
System.out.println(distance);
return true;
} else {
return false;
}
}
public boolean isOutside(Circle c2) {
double distance = Math.sqrt((x - c2.x) * (x - c2.x) + (y - c2.y) * (y - c2.y));
if (distance > ((radius) + (c2.radius))) {
System.out.println(distance);
return true;
} else {
return false;
}
}
public boolean isIntersecting(Circle c2) {
double distance = Math.sqrt((x - c2.x) * (x - c2.x) + (y - c2.y) * (y - c2.y));
if (Math.abs((radius - c2.radius)) <= distance && distance <= (radius + c2.radius)) {
System.out.println(distance);
return true;
} else {
return false;
}
}
Upvotes: 1
Views: 5141
Reputation: 427
The isInside() calculation is just doing an intersection test. If you want to test if a circle completely envelops another circle, you'd want to test if the distance between the two circles plus the radius of the smaller circle is less than the radius of the larger circle.
For example:
public boolean isInside(Circle c2) {
return distanceTo(c2) + radius() <= c2.radius();
}
Upvotes: 5