简体   繁体   中英

Distance between two coordinates

I am trying to get the distance between the coordinates, and it doesn't seem to work for some reason. Grateful if anyone could help!

Output:

The distance from Point a to Point b is 0.0. The distance from Point a to Point b is 0.0. The distance from Point a to Point b is 0.0. The distance from Point a to Point b is 0.0. The distance from p1 to p2 is 4.242640687119285 The distance from p1 to p3 is 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));
}
}

One thing that I see is when you run your main, you're calling the Point.toString() method four times after you make the points. When you do this the distance variable hasn't been set yet because the distanceTo method hasn't been called.

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

When these Point.toString calls happen, the distanceTo method hasn't been called so the distance hasn't been set for any of those points.

You get numbers outputting on your last two lines because you call the distanceTo method.

Does this work for you?

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

I removed the distance variable from your class because there can only be a distance between one point and another; a point on its own has no distance.

I also changed distanceTo : When you call p1.distanceTo(p2) you pass a reference of p2 to p1 . p2 is now called other in the distanceTo method.

Writing this.x is simply a more verbose way of writing just x . I wanted to make clear that this x variable belongs to the receiver of the distanceTo method (which is p1 in this case).

Finally I changed toString so that it prints the X and Y coordinate of your point. In Java instance variables such as the removed distance are always initialized with 0. This is why px.toString() always printed

The distance from Point a to Point b is 0.0

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM