Java:两点之间的距离总是返回零
Java: Distance between two points always returning zero
我正在 class "CartesianPoint" 中编写一个方法来计算两个笛卡尔点之间的距离。每当我调用它时,无论我使用哪个点,打印出的距离始终为零。我相信我为找到距离而创建的新点以某种方式覆盖了我的实例变量,但我不知道如何正确编码。
这是笛卡尔点 Class:
public class CartesianPoint implements Point {
private static double x;
private static double y;
public CartesianPoint(double xCoord, double yCoord){
x = xCoord;
y = yCoord;
}
public double xCoordinate(){
return x;
}
public double yCoordinate(){
return y;
}
public double radius(){
double radius = Math.sqrt(Math.pow(xCoordinate(), 2)+Math.pow(yCoordinate(), 2));
return radius;
}
public double angle(){
double angle = Math.acos(xCoordinate() / radius());
return angle;
}
public double distanceFrom(Point other){
//System.out.println("x coordinate of this: " + xCoordinate());
//System.out.println("x coordinate of other: " + other.xCoordinate());
double xDistance = x - other.xCoordinate();
double yDistance = y - other.yCoordinate();
double distance = Math.sqrt(Math.pow(xDistance, 2) - Math.pow(yDistance, 2));
return distance;
}
//not currently being used
public Point rotate90(){
Point rotatedPoint = new CartesianPoint(0, 0);
return rotatedPoint;
}
}
这是我的测试仪中的方法调用 class:
public class tester{
public static void main(String[] args){
Point p = new CartesianPoint(3, 4);
Point a = new CartesianPoint(6, 7);
System.out.println("Cartesian: (" + p.xCoordinate() + ", " + p.yCoordinate() + ")");
System.out.println("Polar: (" + p.radius() + ", " + p.angle() + ")");
System.out.println("Distance: " + p.distanceFrom(a));
}
}
这是我得到的输出:
Cartesian: (6.0, 7.0)
Polar: (9.219544457292887, 0.8621700546672264)
Distance: 0.0
澄清一下,笛卡尔坐标和极坐标应该打印出 'p' 的坐标,而不是像现在这样打印出 'a' 的坐标。似乎每个新创建的点都覆盖了最后一个点的坐标。
非常感谢任何帮助!
提示:检查计算距离的公式(例如参见here)并将其与您在此处编写的内容进行比较:
Math.sqrt(Math.pow(xDistance, 2) - Math.pow(yDistance, 2));
你看出区别了吗?
提示 #2:减号???
当您编写了一些无法正常工作的代码时,它会付出代价:
- 仔细阅读您所写的内容
- 检查要求。
- 检查您的领域知识:在这种情况下 "the math"
在声明 CartesianPoint 的属性之前删除 static
关键字:
private double x;
private double y;
然后您将确保您正在访问每个 class 实例的正确属性(封装属性)。
此外,您用来计算两点之间距离的公式不正确,应该是
double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
因为公式是sqrt((xb - xa)2 + (yb - ya)2),正确的方法是:
public double distanceFrom(Point other){
//System.out.println("x coordinate of this: " + xCoordinate());
//System.out.println("x coordinate of other: " + other.xCoordinate());
double xDistance = x - other.xCoordinate();
double yDistance = y - other.yCoordinate();
double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
return distance;
}
我正在 class "CartesianPoint" 中编写一个方法来计算两个笛卡尔点之间的距离。每当我调用它时,无论我使用哪个点,打印出的距离始终为零。我相信我为找到距离而创建的新点以某种方式覆盖了我的实例变量,但我不知道如何正确编码。
这是笛卡尔点 Class:
public class CartesianPoint implements Point {
private static double x;
private static double y;
public CartesianPoint(double xCoord, double yCoord){
x = xCoord;
y = yCoord;
}
public double xCoordinate(){
return x;
}
public double yCoordinate(){
return y;
}
public double radius(){
double radius = Math.sqrt(Math.pow(xCoordinate(), 2)+Math.pow(yCoordinate(), 2));
return radius;
}
public double angle(){
double angle = Math.acos(xCoordinate() / radius());
return angle;
}
public double distanceFrom(Point other){
//System.out.println("x coordinate of this: " + xCoordinate());
//System.out.println("x coordinate of other: " + other.xCoordinate());
double xDistance = x - other.xCoordinate();
double yDistance = y - other.yCoordinate();
double distance = Math.sqrt(Math.pow(xDistance, 2) - Math.pow(yDistance, 2));
return distance;
}
//not currently being used
public Point rotate90(){
Point rotatedPoint = new CartesianPoint(0, 0);
return rotatedPoint;
}
}
这是我的测试仪中的方法调用 class:
public class tester{
public static void main(String[] args){
Point p = new CartesianPoint(3, 4);
Point a = new CartesianPoint(6, 7);
System.out.println("Cartesian: (" + p.xCoordinate() + ", " + p.yCoordinate() + ")");
System.out.println("Polar: (" + p.radius() + ", " + p.angle() + ")");
System.out.println("Distance: " + p.distanceFrom(a));
}
}
这是我得到的输出:
Cartesian: (6.0, 7.0)
Polar: (9.219544457292887, 0.8621700546672264)
Distance: 0.0
澄清一下,笛卡尔坐标和极坐标应该打印出 'p' 的坐标,而不是像现在这样打印出 'a' 的坐标。似乎每个新创建的点都覆盖了最后一个点的坐标。
非常感谢任何帮助!
提示:检查计算距离的公式(例如参见here)并将其与您在此处编写的内容进行比较:
Math.sqrt(Math.pow(xDistance, 2) - Math.pow(yDistance, 2));
你看出区别了吗?
提示 #2:减号???
当您编写了一些无法正常工作的代码时,它会付出代价:
- 仔细阅读您所写的内容
- 检查要求。
- 检查您的领域知识:在这种情况下 "the math"
在声明 CartesianPoint 的属性之前删除 static
关键字:
private double x;
private double y;
然后您将确保您正在访问每个 class 实例的正确属性(封装属性)。
此外,您用来计算两点之间距离的公式不正确,应该是
double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
因为公式是sqrt((xb - xa)2 + (yb - ya)2),正确的方法是:
public double distanceFrom(Point other){
//System.out.println("x coordinate of this: " + xCoordinate());
//System.out.println("x coordinate of other: " + other.xCoordinate());
double xDistance = x - other.xCoordinate();
double yDistance = y - other.yCoordinate();
double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
return distance;
}