java - 如何检查圆是否重叠

标签 java graph encoding geometry

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

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;

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

最佳答案

您可以引用Relative position of two circles .

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

关于java - 如何检查圆是否重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49433930/

相关文章:

java - 函数 ref 作为 arg 的行为不明确

algorithm - 将不等式合并为一个

javascript - 难倒: Javascript Comparison Error

java - 如何在 Android 设备上将 WAV 编码为 mp3

csv - 使用pyspark读取csv文件时编码错误

Java-如何验证泰语字符是否正确编码从 UTF-8 到 TIS620

java - 抛出新的 NullPointerException VS 在 null 对象上调用方法

java - 如何避免在重写的 Collections 方法中进行未经检查的强制转换?

java - -Dcom.ibm.mq.cfg.useIBMCipherMappings=false 不工作并获取 MQRC_UNSUPPORTED_CIPHER_SUITE

algorithm - 如何找到距离给定节点小于 n 的节点?