我正在尝试编写一个程序来检查圆圈是否包含另一个圆圈,如果某个圆圈位于圆圈内,或者我遇到问题,如果圆圈与另一个圆圈重叠。
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)。我感谢任何建议/意见。
答案 0 :(得分:1)
如果圆心之间的距离小于两个圆的半径之和,那么它们就会重叠。
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 :(得分:0)
1.-你必须将两个圆圈放在太空中,给他们一些坐标
2.-你必须得到2个圆圈的向量
3.-你必须normailze这些向量并获得正确的距离,单位,
我要使用像素
4.-最后你必须检查这两个矢量之间的距离是否小于两个圆的辐射,如果是,那么它们是重叠的。
在这里你有一个更好的解释链接:
https://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769,当我们想要检查圆圈碰撞时(对于2D游戏),这是我们在游戏开发中常用的东西
答案 2 :(得分:0)
您可以参考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)
}