【问题标题】:Distance between two coordinates两个坐标之间的距离
【发布时间】:2013-10-06 21:47:14
【问题描述】:

我正在尝试获取坐标之间的距离,但由于某种原因它似乎不起作用。如果有人能提供帮助,不胜感激!

输出:

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));
}
}

【问题讨论】:

  • 我看不出问题出在哪里,只是一个建议。对于您的 distanceTo 函数,您应该只需要 1 个点作为参数。第一点将是你“在”的点,即这个。第二点将是参数。
  • 究竟是什么问题?鉴于您的输出方式,您将获得应有的值。从 p1 到 p2 的距离是 4.24。 p1 到 p3 是 12.727,这就是你得到的。正如 Andrew_CS 指出的那样,您的困惑可能是您在计算之前输出了缓存的距离。
  • 如果这个问题得到了回答,您应该接受一个答案 - 这样它就不会被标记为未解决。

标签: java distance


【解决方案1】:

我看到的一件事是,当您运行 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 方法,所以在最后两行输出了数字。

【讨论】:

    【解决方案2】:

    这对你有用吗?

    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

    .

    【讨论】:

      猜你喜欢
      • 2010-11-23
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多