查找圆圈是否在另一个圆圈内

我遇到了一些麻烦。 我有一项任务,要求我查找第二个圆圈是重叠,内部还是第二个圆圈。 但是,我无法检查重叠,如果第二个圆圈在第一个圆圈内。

(使用的变量是x1,x2,y1,y2,r1,r2,距离)

这就是我所拥有的:

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"); } 

我担心问题在于重叠和内部检查,但我无法弄清楚如何正确设置它以便我可以可靠地检查第二个圆是否在第一个圆圈内。

我会非常感激任何帮助或建议,因为我尝试了多种方法,但解决方案每次都让我感到惊讶。

你需要在重叠之前检查内部,因为内部距离<=重叠距离

 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"); } 

根据克里斯的评论修改答案

这个问题可能最容易在视觉上解决,然后编写代码。 你看起来像是没有内部和完全内部的正确逻辑。

解决这个问题的简单方法是,如果它们不完全在内部而不是完全在外面,那么它们必须重叠。 这当然是我编码的方式。 数学比其他两个有点棘手。

 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 { // Overlap System.out.println("Circle2 overlaps Circle1"); } 

实际情况是:

r2>r1-dr2 < r1+d

通过对称,我们不需要同时进行两种方式(如果你在两者中交换r2和r1并进行一些重新排列,你会得到相同的方程对)。

最容易将其留在“其他”类别中,而不是编码,除非您出于某种原因需要。

那么,如果距离和较小半径的总和小于另一个半径,则较小的圆应该在较大的圆内。

你差不多就在那里。 只是条件的顺序是错误的。

 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 { // Overlap System.out.println("Circle2 overlaps Circle1"); } 

在“非重叠”情况下检查“内部”情况可确保不会意外地将其视为重叠。 然后所有其余的必须重叠。

通过评论代理编辑显而易见性:

毕达哥拉斯描述了空间点之间的距离:

  distance = sqrt( travelled_x_squared + travelled_y_squared ); 

这当然转化为代码

  distance = Math.sqrt( (x1-x2)*(x1-x2) + (y1 - y2)*(y1 - y2) ); 

距离在r1 + r2处接触。

在编辑线索之前:您需要圆圈之间的角度。

然后计算从circle1到圆2的距离。如果它小于radii1 + radii2,你就在里面。

atan2可能是一个感兴趣的函数。

或者直接选择毕达哥拉斯距离。

这是一项简单的任务,

取两个圆的半径之和。 说r1 + r2。 现在找到两个圆的中心之间的距离,即sq1((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 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

 /** * * @param values { x0, y0, r0, x1, y1, r1 } * @return true if circles is intersected */ public static boolean isCircleIntersect(double... values) { /* check using mathematical relation: ABS(R0-R1) <= SQRT((x0-x1)^2+(y0-y1)^2) <= (R0+R1) */ if (values.length == 6) { /* get values from first circle */ double x0 = values[0]; double y0 = values[1]; double r0 = values[2]; /* get values from second circle */ double x1 = values[3]; double y1 = values[4]; double r1 = values[5]; /* returun result */ return (Math.abs(r0 - r1) <= Math.sqrt(Math.pow((x0 - x1), 2) + Math.pow((y0 - y1), 2))) && (Math.sqrt(Math.pow((x0 - x1), 2) + Math.pow((y0 - y1), 2)) <= (r0 + r1)); } else { /* return default result */ return false; } }