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

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

如何创建方法“ distanceTo ”,通过获取x,y,z坐标来计算两点之间的距离? 我假设我的答案将与sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)但我不知道如何在我的方法中写出我的主要课程,如果直到我的第二个测试点课程,我的要点才被定义

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

我的主要课程:

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

正如@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)); } 

然后,如果你想获得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) + ... } 

PS我的第一个答案:)

 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类。 当您需要计算TestPoints类中的距离时,您可以执行类似的操作

 double distance = firstPoint.distanceTo(secondPoint); 

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

您可以将“distanceTo”方法放在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方法(例如在您的Program类中,您有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)); } } 

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

只需使用吸气剂

 float distance = Math.sqrt(Math.pow(secondPoint.getXCoord() - firstPoint.getXCoord()), 2) + ...) 

两个想法。

添加一个:

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

你的Point3d类的方法。 然后,在main方法结束时,您可以执行以下操作:

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

或者,向主TestPoints类添加静态方法:

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