Java – find out if one circle is inside another
•
Java
I'm in a bit of trouble. I have a task asking me to find a second circle, whether it overlaps, inside or in the second circle However, I cannot check for overlap if the second circle is within the first circle
(the variables used are x1, X2, Y1, Y2, R1, R2, distance)
This is 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'm worried about overlap and internal checking, but I can't figure out how to set it correctly, so I can reliably check whether the second circle is in the first
Any help or advice will be appreciated because I have tried many ways, but the solution is just to escape me every time
Solution
If you only need to check the internal distance before overlapping, the internal distance is < = overlapping distance
if (distance > (r1 + r2)) { // No overlap System.out.println("Circle2 does not overlap Circle1"); } else if ((distance <= Math.abs(r1 - r2)) { // Inside System.out.println("Circle2 is inside Circle1"); } else // if (distance <= r1 + r2) { // Overlap System.out.println("Circle2 overlaps Circle1"); }
Revise the answer according to Chris's opinion
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码