如何检查圆是否重叠

Tho*_*123 7 java geometry encoding graph

我正在尝试编写一个程序来检查一个圆是否包含另一个圆,某个点是否在圆内,或者我遇到问题的点,如果一个圆与另一个圆重叠。

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



    }
}
Run Code Online (Sandbox Code Playgroud)

所以我的重叠方法一直在底部,但我实际上没有任何东西在里面,因为我不确定到底要做什么。我试过这个:

if (distance <= radius) return true;
        else return false;
Run Code Online (Sandbox Code Playgroud)

但这没有用。所以我不确定还可以尝试什么。仅供参考,我试图检查 c1 是否与参数为 (3, 5, 2.3) 的圆重叠。我很感激任何建议/意见。

big*_*ast 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)
}
Run Code Online (Sandbox Code Playgroud)


har*_*rsh 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;
Run Code Online (Sandbox Code Playgroud)