2013-10-06 94 views
0

我想要获得坐标之间的距离,并且由于某种原因它似乎不起作用。感恩,如果有人能帮助!两个坐标之间的距离

输出:

从A点到B点的距离为0.0。点a 到点b的距离为0.0。从点a到点b的距离为0.0。从点a到点b的距离为0.035。从P1到P2的距离 4.242640687119285从P1到P3的距离12.727922061357855

package GC01; 

public class Point { 
    private final double x; 
    private final double y; 
    private double distance; 

    public Point(){ 
     x=0.0; 
     y=0.0; 
    } 

    public Point(double x, double y) { 
     this.x=x; 
     this.y=y; 
    } 

    public double distanceTo(Point a, Point b) { 
     double dx = a.x - b.x; 
     double dy = a.y - b.y; 
     distance = Math.sqrt(dx*dx + dy*dy); 
     return distance; 
    } 
    public String toString(){ 
     return "The distance from Point a to Point b is " + distance +"."; 
    } 

public static void main(String[] args){ 
    Point p0 = new Point(); 
    Point p1 = new Point(0.0,0.0); 
    Point p2 = new Point(3.0,3.0); 
    Point p3 = new Point(9.0,9.0); 
    System.out.println(p0.toString()); 
    System.out.println(p1.toString()); 
    System.out.println(p2.toString()); 
    System.out.println(p3.toString()); 
    System.out.println("The distance from p1 to p2 is "+p1.distanceTo(p1,p2)); 
    System.out.println("The distance from p1 to p3 is "+p1.distanceTo(p1,p3)); 
} 
} 
+1

我没有看到问题出在哪里,而只是一个建议。对于你的distanceTo函数,你应该只需要1个点的参数。第一点将是你“进入”的点,即这个。第二点将是参数。 – Kindread

+0

其实问题是什么?鉴于你如何输出,你正在得到你应该的价值。从p1到p2的距离是4.24。而p1到p3是12.727,这就是你所得到的。正如Andrew_CS指出的那样,您的困惑可能是您在计算之前输出了缓存距离。 – Kindread

+0

如果回答这个问题,你应该接受一个答案 - 这样它就不会被标记为未解决。 –

回答

2

一两件事,我看到的是,当你运行你的主,你调用Point.toString()方法四你做出积分后的时间。当你这样做时,距离变量还没有被设置,因为distanceTo方法没有被调用。

Point p0 = new Point(); 
Point p1 = new Point(0.0,0.0); 
Point p2 = new Point(3.0,3.0); 
Point p3 = new Point(9.0,9.0); 
System.out.println(p0.toString()); 
System.out.println(p1.toString()); 
System.out.println(p2.toString()); 
System.out.println(p3.toString()); 

当这些Point.toString调用发生,distanceTo方法还没有被调用所以距离尚未设置任何这些点。

因为您调用distanceTo方法,所以您会在最后两行输出数字。

0

这是否适合您?

public class Point { 
    private final double x; 
    private final double y; 

    public Point() { 
     x = 0.0; 
     y = 0.0; 
    } 

    public Point(double x, double y) { 
     this.x = x; 
     this.y = y; 
    } 

    public double distanceTo(Point other) { 
     double dx = other.x - this.x; 
     double dy = other.y - this.y; 
     double distance = Math.sqrt(dx * dx + dy * dy); 
     return distance; 
    } 

    public String toString() { 
     return x + "/" + y; 
    } 

    public static void main(String[] args) { 
     Point p0 = new Point(); 
     Point p1 = new Point(0.0, 0.0); 
     Point p2 = new Point(3.0, 3.0); 
     Point p3 = new Point(9.0, 9.0); 
     System.out.println(p0.toString()); 
     System.out.println(p1.toString()); 
     System.out.println(p2.toString()); 
     System.out.println(p3.toString()); 
     System.out 
       .println("The distance from p1 to p2 is " + p1.distanceTo(p2)); 
     System.out 
       .println("The distance from p1 to p3 is " + p1.distanceTo(p3)); 
    } 
} 

我从你的类删除distance变量,因为只能有一个点和彼此之间的距离;一个点本身没有距离。

我也改变了distanceTo:当您拨打p1.distanceTo(p2)时,您将p2的参考号传递给p1。 p2现在在distanceTo方法中被称为other

写作this.x只是一种更详细的写作方式,只是写作x。我想表明这x变量属于distanceTo方法(在这种情况下为p1)的接收方。

最后我改变了toString,以便它打印你的点的X和Y坐标。在Java实例变量,如除去distance总是以0初始化这就是为什么px.toString()总是打印

从点A到点B的距离为0.0