簡體   English   中英

兩個坐標之間的距離

[英]Distance between two coordinates

我正在嘗試獲取坐標之間的距離,由於某種原因,它似乎不起作用。 感謝任何人都可以提供幫助!

輸出:

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

我看到的一件事是,當您運行主程序時,您在獲得積分后四次調用Point.toString()方法。 執行此操作時,尚未設置distance變量,因為尚未調用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方法。

這對您有用嗎?

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)你傳遞的參考p2p1 現在,p2在distanceTo方法中稱為other

編寫this.x只是編寫x一種更詳細的方法。 我想弄清楚這個x變量屬於distanceTo方法的接收者(在這種情況下為p1 )。

最終,我將toString更改為可打印您的點的X和Y坐標。 在Java實例中,變量(例如,已刪除的distance始終以0初始化。這就是為什么始終打印px.toString()原因

點a到點b的距離是0.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM