Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding if a circle is inside another circle

Tags:

java

math

I'm having a bit of trouble. I have an assignment that requires me to find if a second circle is overlapping, inside, or neither a second circle. However, I am having trouble checking for overlapping and if the second circle is inside the first.

(variables used are x1, x2, y1, y2, r1, r2, distance)

Here's what I have:

if (distance > (r1 + r2)) {
        // No overlap
        System.out.println("Circle2 does not overlap Circle1");
    } else if (distance <= Math.abs(r1 + r2)) {
        // Overlap
        System.out.println("Circle2 overlaps Circle1");
    } else if ((distance <= Math.abs(r1 - r2)) {
        // Inside
        System.out.println("Circle2 is inside Circle1");
}

I fear the problem is with the overlapping and inside checks, but I cannot figure out how to properly set it up so I can reliably check if the second circle is inside the first.

Any help or advice would be greatly appreciated as I've tried multiple approaches but the solution simply escapes me every time.

like image 556
Battleroid Avatar asked Feb 28 '12 17:02

Battleroid


1 Answers

Its a simple task,

take the sum of the radius of two circle. say r1+ r2 . Now find the distance between center of two circle which is sqrt((x1-x2)^2 + (y1-y2)^2) if r1+r2 = sqrt((x1-x2)^2 + (y1-y2)^2) they just touch each other. if r1+r2 > sqrt((x1-x2)^2 + (y1-y2)^2) the circle overlaps(intersect) if r1+ r2 < sqrt((x1-x2)^2 + (y1-y2)^2) the circle doesnot intersect

like image 173
Invictus Avatar answered Oct 14 '22 16:10

Invictus