Calculating distance between two points in 3D

Idan Gelber picture Idan Gelber · Jun 2, 2015 · Viewed 9.3k times · Source

My assignment is to create main class in which I initialize the value of any point to be at (0,0,0) and to be able to access and mutate all three values (x,y,z) individually. To do this I have used getters and setters. My next task is to create a method within my main class (which I shall call "distanceTo") that calculates the distance between two points.

How do I go about creating the method "distanceTo" that calculates the distance between two points by taking in the x,y,z coordinates ? I assume my answer will have something to do with sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2) but I do not know how I can write that in my method in my main class if my points are not defined until my second test point class

So far I only have two points, but I am looking for a more general answer (so that if I created three points, p1 p2 and p3, I could calculate the distance between p1 and p2 or the distance between p2 and p3 or the distance between p1 and p3.

My main class:

package divingrightin;

public class Point3d {

    private double xCoord;
    private double yCoord;
    private double zCoord;


    public Point3d(double x, double y, double z){
        xCoord = x;
        yCoord = y;
        zCoord = z;
    }

    public Point3d(){
        this (0,0,0);
    }

    public double getxCoord() {
        return xCoord;
    }
    public void setxCoord(double xCoord) {
        this.xCoord = xCoord;
    }
    public double getyCoord() {
        return yCoord;
    }
    public void setyCoord(double yCoord) {
        this.yCoord = yCoord;
    }
    public double getzCoord() {
        return zCoord;
    }
    public void setzCoord(double zCoord) {
        this.zCoord = zCoord;
    }

    //public double distanceTo(double xCoord, double yCoord, double zCoord ){


    }

My class with the test points: package divingrightin;

public class TestPoints {

    public static void main(String[] args) {

        Point3d firstPoint = new Point3d();

        firstPoint.setxCoord(2.2);
        firstPoint.setyCoord(1);
        firstPoint.setzCoord(5);

        //System.out.println(firstPoint.getxCoord());

        Point3d secondPoint = new Point3d();

        secondPoint.setxCoord(3.5);
        secondPoint.setyCoord(22);
        secondPoint.setzCoord(20);

    }

}

Answer

KarlTheGreat picture KarlTheGreat · Jun 2, 2015

As @Dude pointed out in the comments, you should write a method:

public double distanceTo(Point3d p) {
    return Math.sqrt(Math.pow(x - p.getxCoord(), 2) + Math.pow(y - p.getyCoord(), 2) + Math.pow(z - p.getzCoord(), 2));
}

Then if you want to get the distance between 2 points you just call:

myPoint.distanceTo(myOtherPoint);
//or if you want to get the distance to some x, y, z coords
myPoint.distanceTo(new Point3d(x,y,z);

You could even make the method static and give it 2 points to compare:

public static double getDistance(Point3d p1, Point3d p2) {
    return Math.sqrt(Math.pow(p1.getxCoord() - p2.getxCoord(), 2) + ...
}

P.S. my first answer :)