我安静地环顾四周,有很多类似的东西,但他们总是使用变量 X1、X2 和 Y1 Y2,而我不允许这样做。 对于一项作业,我有 2 个类(class),我们称之为 A 和 B
Class A
//Punt (x,y)
Punt mp1 = new Punt(1.0, 2.0)
Punt mp2 = new Punt(3.0, 4.0)
//Circle(center, radius)
Circle c1 = new Circle(mp1, 1.0)
Circle c2 = new Circle(mp1, 1.0)
现在在 B 类中,我需要查看圆是否重叠,所以我想查看中心点之间的距离是否 < radius1 + radius2。我必须公开 boolean 重叠(圈出那个)
Class B
private Punt center
private double radius
public Circle(Punt mp, double ra)
center = mp
radius = ra
public boolean overlap(Circle that)
//here I would need to find the distance between the distance of the centers with Pythagorean theorem
double sumRadius = this.radius + that.radius //this one works
if (distCenter <= sumRadius )
return true
else
return false;
我已经尝试了超出我能想到的,但没有任何效果,有什么建议吗?
我不允许只制作 X1 和 X2 并在 A 类等中创建 getx1()
最佳答案
你的 Circle 类肯定有 getRadius() 和 getCenter() 方法,对吗?我们得到中心值并计算欧氏距离,然后与半径之和进行比较。实际上,您甚至不需要 getCenter 方法,因为您可以直接访问两个圆(this
圆和 that
圆)的中心点、Punt 字段。请注意,欧几里得距离是您找到的公式 -
Math.sqrt(deltaX * deltaX + deltaY * deltaY)
其中 deltaX 是两个圆中心点 X 值的差值,deltaY 也是如此。
您需要向我们展示您的 Punt 对象。我必须假设您可以从中获取 x 和 y 值,这就是您问题的解决方案。即 center.getX()
和 center.getY()
关于java - 从点对象获取值,该对象在 Java 中用作圆对象的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25960009/