如何向 3D 点类添加方法来计算两点之间的距离?

问题描述 投票:0回答:5

我创建了一个

Point3d
类,其中包含点的 x、y 和 z 坐标。我的下一个任务是在我的类中创建一个方法(我将其称为“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 ){ // // } }
我的班级有测试点:

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); } }
    
java coordinates distance euclidean-distance
5个回答
9
投票
正如

@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) + ... }
    

6
投票
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);
    

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

您可以将“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)); } }

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


0
投票
只需使用吸气剂

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

0
投票
两个想法。

添加:

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));
    
© www.soinside.com 2019 - 2024. All rights reserved.