gpt4 book ai didi

java - 如何检查圆是否重叠

转载 作者:行者123 更新时间:2023-11-30 02:13:22 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com