简体   繁体   English

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

[英]Calculating distance between two points in 3D

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. 我的任务是创建主类,在其中我将任何点的值初始化为(0,0,0)并且能够分别访问和改变所有三个值(x,y,z)。 To do this I have used getters and setters. 为此,我使用了getter和setter。 My next task is to create a method within my main class (which I shall call "distanceTo") that calculates the distance between two points. 我的下一个任务是在我的主类(我称之为“distanceTo”)中创建一个计算两点之间距离的方法。

How do I go about creating the method " distanceTo " that calculates the distance between two points by taking in the x,y,z coordinates ? 如何创建方法“ distanceTo ”,通过获取x,y,z坐标来计算两点之间的距离? 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 我假设我的答案将与sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)但我不知道如何在我的方法中写出我的主要课程,如果我的要点直到我的第二个测试点课程才定义

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. 到目前为止我只有两点,但我正在寻找一个更一般的答案(所以如果我创建三个点,p1 p2和p3,我可以计算p1和p2之间的距离或p2和p3之间的距离或距离在p1和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; 我的班级有测试点: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);

    }

}
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. 将其添加到Point3d类。 When you need to calculate the distance in the TestPoints class, you do something like 当你需要在TestPoints类中计算距离时,你会做类似的事情

double distance = firstPoint.distanceTo(secondPoint);

As @Dude pointed out in the comments, you should write a method: 正如@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));
}

Then if you want to get the distance between 2 points you just call: 然后,如果你想获得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);

You could even make the method static and give it 2 points to compare: 你甚至可以使方法静态并给它2分进行比较:

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

PS my first answer :) PS我的第一个答案:)

You have two possible approaches, according to what you want to achieve. 根据您想要实现的目标,您有两种可能的方法。

You can put your "distanceTo" method inside the class Point3D: 您可以将“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));
}

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: 或者,您可以拥有一个生活在某处的通用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));
  }
}

Which one is better? 哪一个更好? Depends on how you use them in the common case :) 取决于你如何在常见情况下使用它们:)

只需使用吸气剂

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

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. 你的Point3d类的方法。 Then, at the end of your main method, you can do: 然后,在main方法结束时,您可以执行以下操作:

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

Or, add a static method to your main TestPoints class: 或者,向主TestPoints类添加静态方法:

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: 然后,在main方法结束时,您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM