查找圆圈是否在另一个圆圈内 [英] Finding if a circle is inside another circle
问题描述
我遇到了一些麻烦。我有一项任务,要求我查找第二个圆圈是重叠,内部还是第二个圆圈。但是,我无法检查重叠,如果第二个圆圈在第一个圈内。
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.
(使用的变量是x1,x2,y1,y2,r1,r2,距离)
(variables used are x1, x2, y1, y2, r1, r2, distance)
这是我所拥有的:
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.
推荐答案
你需要在重叠之前检查内部,因为内部的距离是< =重叠距离
you just need to check for inside before overlap as distance for inside is <= distance for overlap
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");
}
根据Chris的评论修改答案
answer modified as per Chris's comments
这篇关于查找圆圈是否在另一个圆圈内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!