Calculating distance between two points in 3D

2019-04-08 12:45发布

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

    }

}

5条回答
欢心
2楼-- · 2019-04-08 13:11

You have two possible approaches, according to what you want to achieve.

You can put your "distanceTo" method inside the class Point3D:

public class Point3d {
  ...
  public double distanceTo(Point3d ) {
    return Math.sqrt( Math.pow(this.x - that.x, 2) + Math.pow(this.y - that.y, 2) + Math.pow(this.z - that.z, 2));
}

In this case, you are always using the first point as the first argument, and any other point as the one you want to compute the distance from.

Alternatively, you can have a generic distanceTo method that lives somewhere(such as in your Program class, where you have your main method), that takes two points and compute the distance between those:

public class Program {
  static public void main(String[] args) {}
  public double distanceTo(Point3d p1, Point3d p2) {
    return Math.sqrt( Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2) + Math.pow(p1.z - p2.z, 2));
  }
}

Which one is better? Depends on how you use them in the common case :)

查看更多
欢心
3楼-- · 2019-04-08 13:14
public double distanceTo(Point3d other) {
    return Math.sqrt(Math.pow(this.xCoord-other.getxCoord(), 2)
            + Math.pow(this.yCoord-other.getyCoord(), 2)
            + Math.pow(this.zCoord-other.getzCoord(), 2));
}

Add this to your Point3d class. When you need to calculate the distance in the TestPoints class, you do something like

double distance = firstPoint.distanceTo(secondPoint);
查看更多
Root(大扎)
4楼-- · 2019-04-08 13:23

Two ideas.

Either add a:

public double distanceTo(Point3d otherPoint) {
   // return distance by using this.getxCoord(), this.getyCoord(), etc.
   // and otherPoint.getxCoord(), otherPoint.getyCoord()
}

method to your Point3d class. Then, at the end of your main method, you can do:

System.out.println(firstPoint.distanceTo(secondPoint));
System.out.println(tenthPoint.distanceTo(ninthPoint));

Or, add a static method to your main TestPoints class:

public static double distanceBetween(Point3d point1, Point3d point2) {
   // return distance by using point1.getxCoord(), etc. and point2.getxCoord()
}

Then, at the end of your main method, you can do:

System.out.println(distanceBetween(firstPoint, secondPoint));
System.out.println(distanceBetween(tenthPoint, ninthPoint));
查看更多
成全新的幸福
5楼-- · 2019-04-08 13:33

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 :)

查看更多
SAY GOODBYE
6楼-- · 2019-04-08 13:34

Just use the getters

float distance = Math.sqrt(Math.pow(secondPoint.getXCoord() - firstPoint.getXCoord()), 2) + ...)
查看更多
登录 后发表回答