计算 3D 中两点之间的距离

Calculating distance between two points in 3D

我的任务是创建 main class,其中我将任何点的值初始化为 (0,0,0) 并能够访问和改变所有三个值 (x,y ,z) 单独。为此,我使用了 getter 和 setter。我的下一个任务是在我的 main class(我将称之为 "distanceTo")中创建一个方法来计算两点之间的距离。

我如何着手创建方法“distanceTo”,通过获取 x、y、z 坐标来计算两点之间的距离?我假设我的答案与 sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2) 有关,但我不知道如果我的点在我的第二个测试点 [=25] 之前没有定义,我该如何在我的主要 class 的方法中编写它=]

到目前为止我只有两个点,但我正在寻找一个更通用的答案(这样如果我创建了三个点,p1 p2 和 p3,我可以计算 p1 和 p2 之间的距离或 p2 之间的距离和 p3 或 p1 和 p3 之间的距离。

我的主要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 ){


    }

我的class与测试点: 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);

    }

}

只需使用 getters

float distance = Math.sqrt(Math.pow(secondPoint.getXCoord() - firstPoint.getXCoord()), 2) + ...)
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));
}

将此添加到您的 Point3d class。当您需要计算测试点 class 中的距离时,您可以执行类似

的操作
double distance = firstPoint.distanceTo(secondPoint);

两个想法。

要么加一个:

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

您的 Point3d class 方法。 然后,在 main 方法的末尾,您可以执行以下操作:

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

或者,添加一个静态方法到你的 main TestPoints class:

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

然后,在您的 main 方法结束时,您可以:

System.out.println(distanceBetween(firstPoint, secondPoint));
System.out.println(distanceBetween(tenthPoint, ninthPoint));

根据您想要实现的目标,您有两种可能的方法。

您可以将 "distanceTo" 方法放在 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));
}

在这种情况下,您始终使用第一个点作为第一个参数,并将任何其他点作为您要计算距离的点。

或者,您可以在某个地方使用一个通用的 distanceTo 方法(例如在您的程序 class 中,您有 main 方法),它需要两个点并计算它们之间的距离:

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

哪个更好?取决于您在常见情况下如何使用它们:)

正如@Dude 在评论中指出的那样,您应该编写一个方法:

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

然后,如果你想获得两点之间的距离,你只需调用:

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

您甚至可以将方法设为静态并给它 2 分进行比较:

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

P.S。我的第一个答案:)