两个坐标之间的距离

时间:2013-10-06 21:47:15

标签: java distance

我试图获得坐标之间的距离,但由于某些原因它似乎不起作用。如果有人能提供帮助,我会感激不尽!

输出:

  

从点a到点b的距离为0.0。距离点a的距离   到点b是0.0。从点a到点b的距离是0.0。该   从点a到点b的距离是0.0。从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));
}
}

2 个答案:

答案 0 :(得分:2)

我看到的一件事是当你运行你的main时,你在得到点后四次调用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方法。

答案 1 :(得分: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现在在other方法中被称为distanceTo

this.x只是写x的一种更冗长的方式。我想明确指出,此x变量属于distanceTo方法的接收者(在这种情况下为p1)。

最后我更改了toString,以便打印点的X和Y坐标。在Java实例中,删除的distance等变量始终用0初始化。这就是px.toString()始终打印的原因

  

从点a到点b的距离为0.0