如何检查圆是否重叠

7

我正在尝试编写一个程序,检查一个圆是否包含另一个圆,一个特定的点是否在圆内,或者我遇到困难的是,一个圆是否与另一个圆重叠。

import javafx.scene.shape.Circle;
public class Problem10_11 {
    public static void main(String[] args) {
        //Create circle with certain parameters.
        Circle2D c1 = new Circle2D(2, 2, 5.5);

        //Create output which will be tested by all our methods.
        System.out.println("The area for circle 1 is " +c1.getArea()+ 
                " and its perimeter is " + c1.getPerimeter());
        System.out.println("Is (3,3) contained within circle 1? " 
                + c1.contains(3, 3));
        System.out.println("Does circle 1 contain circle 2? " 
                + c1.contains(new Circle2D(4,5,10.5)));
        System.out.println("Does circle 1 overlap with circle 3? " 
                + c1.overlaps(new Circle2D(3, 5, 2.3)));
    }
}
class Circle2D {
    double x; //first parameter
    double y; //second parameter
    double radius; //third parameter

    Circle2D() {
    }
    public Circle2D(double x, double y, double radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    public void setX(double x) {
        this.x = x;  //set x
    }
    public double getX() {
        return x; //grab x
    }
    public void setY(double y) {
        this.y = y; //set y
    }
    public double getY() {
        return y; //grab y
    }
    public void setRadius(double radius) {
        this.radius = radius; //set radius
    }
    public double getRadius() {
        return radius; //grab radius
    }
    public double getArea() {
            double area = Math.PI*radius*radius; //formula for area
                return area;
    }
    public double getPerimeter() {
            double perimeter = 2*Math.PI*radius; //formula for perimeter
                return perimeter;
    }
    public boolean contains(double x, double y) {
        //Use distance formula to check if a specific point is within our circle. 
            double distance = Math.sqrt(Math.pow(this.x - x, 2) + (Math.pow(this.y - y, 2)));
            if (distance  <= radius * 2)
                return true;
            else {
                return false;
            }
    }
    public boolean contains(Circle2D circle) {
        //Use distance formula to check if a circle is contained within another circle.
        double distance = Math.sqrt(Math.pow(circle.getX() - x, 2) + (Math.pow(circle.getY() - y, 2)));
        if (distance <= (this.radius - circle.radius)) {
            return true;
        } else {
            return false;
        }
    }
    public boolean overlaps(Circle2D circle) {
        //Use distance formula to check if a circle overlaps with another circle.
            double distance = Math.sqrt(Math.pow(circle.getX() - x, 2) + (Math.pow(circle.getY() - y, 2)));



    }
}

所以我的重叠方法在底部,但我没有具体的内容,因为我不确定该怎么做。我尝试过这样做:
if (distance <= radius) return true;
        else return false;

但是那并没有起作用。所以我不确定还有什么其他尝试的办法。FYI,我正在尝试检查c1是否与参数为(3, 5, 2.3)的圆重叠。我感谢任何建议/意见。

4个回答

5
你可以参考两个圆的相对位置
public boolean overlaps(Circle2D circle) {
        //Use distance formula to check if a circle overlaps with another circle.
        double distance = Math.sqrt(Math.pow(circle.getX() - x, 2) + (Math.pow(circle.getY() - y, 2)));
        return distance <= (this.radius + circle.radius) and distance >= Math.abs(this.radius - circle.radius)
}

尝试过这个,但仍然输出true,而应该是false。 - Thomas123
太棒了,尝试了多种情况,这确实有效 :) 谢谢! - Thomas123

2
如果两个圆心之间的距离小于两个圆的半径之和,则它们会重叠。
double minDistance = Math.max(circle.getRadius(),this.radius) - Math.min(circle.getRadius(),this.radius);
    if (distance <= (this.radius + circle.getRadius()) && distance>= minDistance) return true;
            else return false;

1
是的,但如果圆包含在另一个圆内,它不会返回true吗? - Thomas123
1
是的,在这种情况下它们是重叠的,而且一个圆包含在另一个圆内。如果你想要在这种情况下得到 false,你还需要一个条件,即距离应该大于两个圆中较大的半径。 - harsh
这里可能有更好的计算最小距离的方法,但是思路应该是相同的,距离应该大于两个圆的半径差。 - harsh
我尝试了一个有重叠的图表,它给出了错误的输出,即“false”。 - Thomas123
你还需要一个条件,即距离应该大于两个圆中较大的半径,但当小圆的中心位于大圆内部时,它们仍会相交。请参阅我的答案以获取详细信息。 - parsecer

1

这里大多数答案都是错误的。

需要考虑三种情况:

  1. enter image description here

圆形重叠,较小圆的中心在较大圆内部

  1. enter image description here

圆形重叠,较小圆的中心在较大圆外面

3.输入图像描述

两个圆在它们的边界处相接触

该算法(使用Java)如下:

  1. 计算中心之间的距离:
double centersDistance = Math.sqrt((x2 - x1)^2 + (y2 - y1)^2)
  • 检查一个圆是否包含另一个圆:
  • boolean blueContainsRed = blue.radius > centersDistance + red.radius;
    boolean redContainsBlue = red.radius > centersDistance + blue.radius;
    
    1. 检查它们是否重叠:
    boolean circlesOverlap = centersDistance <= blue.radius + red.radius;
    

    <= 会确保在边界仅相接的情况下返回 true。如果不想要这个结果,可以使用 <

    1. 因此,最终公式将是:
    return !blueContainsRed && !redContainsBlue && circlesOverlap;
    

    这个表格也可能会很有用(来自https://planetcalc.com/8098/):

    enter image description here


    0

    1.- 你需要在空间中放置两个圆,并给它们一些坐标。
    2.- 你需要从这两个圆中获取向量。
    3.- 你需要对这些向量进行归一化,并以单位的形式得到正确的距离,我将使用像素。
    4.- 最后,你需要检查这两个向量之间的距离是否小于两个圆的半径,如果是,则它们重叠了。
    这里有一个更好的解释链接: https://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769,实际上这是我们在游戏开发中经常使用的东西,当我们想要检查圆形碰撞时(用于2D游戏)。


    可能是有些误解,因为这并没有对我的情况有所帮助。 - Thomas123

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接