简体   繁体   English

如何计算具有 3 个坐标点的类的两个对象之间的距离?

[英]How to calcualate the distance between two Object of a class which have 3 coordinates points?

This is my class 3DObj in 3DObj.java.这是我在 3DObj.java 中的类 3DObj。

public class 3DObj {
    private double a;
    private double b;
    private double c;
    public double dist;

    public 3DObj() {

    }

    public 3DObj(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;

    }
    public double getA() {
        return a;
    }
    public double getB() {
        return b;
    }
    public double getC() {
        return c;
    }

    public double dist(3DObj p1, 3DObj p2, 3DObj p3) {
        dist = Math.sqrt(Math.pow((p2.a - p1.a), 2)
                         + Math.pow((p2.b - p1.b), 2) 
                         + Math.pow((p2.c - p1.c), 2));
        return dist;
    }

    public double getDist() {
        return dist;
    }
}

Below is my 3DObjTest class in 3DObjTest.java下面是我在 3DObjTest.java 中的 3DObjTest 类

public class 3DObjTest {
    public static void main(String[] args) {
        3DObjTest 3DObj1 = new 3DObj(2.5, 2.5, 2.5);
        3DObjTest 3DObj2 = new 3DObj(2.0, 2.2, 3.0);
        System.out.println("Distance is " + 3DObj1.getDist());
    }
}

but it is only showing 0.0 instead of any other number.但它只显示 0.0 而不是任何其他数字。

How can I calculate the distance between 3DObj1 and 3DObj2?如何计算 3DObj1 和 3DObj2 之间的距离?

You never assign your dist variable to the result of your dist() function.您永远不会将dist变量分配给dist()函数的结果。 This variable is auto-assigned to 0.0 and so it shows you 0.0.此变量自动分配为 0.0,因此它显示为 0.0。

All in all, your set up for this class is not good because dist is not a characteristics of your object.总而言之,您对此类的设置不好,因为dist不是您的对象的特征。 The function calculating distance should be created in another class, which would treat various operations on groups of 3DObj .计算距离的函数应该在另一个类中创建,它将处理对3DObj组的各种操作。

Another solution would be to have distance of this 3DObj to another 3DObj calculated through object1.dist(object2) .另一种解决方案是通过3DObj object1.dist(object2)计算出这个3DObj到另一个3DObj距离。 In such case you don't need dist variable anyway (again, it is not a characteristics of one object).在这种情况下,无论如何您都不需要dist变量(同样,它不是一个对象的特征)。

You don't need the dist member in 3DObj .您不需要3DObjdist成员。 Distance is always measured between two object instances.始终在两个对象实例之间测量距离。 Respectfully, no need for 3DObje.getDist()恭敬地,不需要3DObje.getDist()

You need to fix your dist() function:您需要修复dist()函数:

public double dist(3DObj arg) {
    double dist = Math.sqrt(Math.pow((this.a - arg.a), 2)
                     + Math.pow((this.b - arg.b), 2) 
                     + Math.pow((this.c - arg.c), 2));
    return dist;
}

And in your main you can use:在您的主要内容中,您可以使用:

System.out.println("Distance is " + 3DObj1.dist(3DObj2));

You should calculate distance outside of 3DObj.您应该计算 3DObj 之外的距离。 With your current code, it seems you are trying to calculate the distance between 3 points (p1, p2, p3).使用您当前的代码,您似乎正在尝试计算 3 个点(p1、p2、p3)之间的距离。 And you are also trying to calculate distance inside the 3DObj, which constitutes the blueprint for just a single data point (1 3D object).而且您还试图计算 3DObj 内部的距离,这构成了单个数据点(1 个 3D 对象)的蓝图。 Instead, distance should be calculated out of this class.相反,距离应该从这个类中计算出来。

Also, the a, b, c attributes are private within 3DObj, so you should access them using your accessors (getA(), getB(), getC()).此外,a、b、c 属性在 3DObj 中是私有的,因此您应该使用访问器(getA()、getB()、getC())访问它们。

Here is your edited code.这是您编辑的代码。 Although I didn't test if it works, it should give you an idea of what needs to be changed.虽然我没有测试它是否有效,但它应该让您了解需要更改的内容。

public class 3DObj {
    private double a;
    private double b;
    private double c;

    public 3DObj(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public double getA() {
        return a;
    }
    public double getB() {
        return b;
    }
    public double getC() {
        return c;
    }
}

You need some other class to work with multiple 3DObj.您需要一些其他类来处理多个 3DObj。 Let's call is 3DObjCollection, and let's say it calculates the distance between two 3DObj.让我们调用 3DObjCollection,假设它计算两个 3DObj 之间的距离。

public class 3DObjCollection {
    private 3DObj p1;
    private 3DObj p2;

    public 3DObjCollection(3DObj p1, 3DObj p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public dist() {
        double result = Math.sqrt(Math.pow((p2.getA() - p1.getA()), 2)
                        + Math.pow((p2.getB() - p1.getB()), 2) 
                        + Math.pow((p2.getC() - p1.getC()), 2));
        return(result);
    }
}

Now, let's create the testing class, 3DObjTest:现在,让我们创建测试类 3DObjTest:

public class 3DObjTest {
    public static void main(String[] args) {
        3DObj p1 = new 3DObj(2,4,6);
        3DObj p2 = new 3DObj(3,4,7);
        3DObjCollection c1 = new 3DObjCollection(p1, p2);
        System.out.println("Distance is " + c1.dist());
    }
}

You never called this method, so the default value that is assigned to dist is 0 .您从未调用过此方法,因此分配给 dist 的默认值为0

public double dist(3DObj p1, 3DObj p2, 3DObj p3)

Why does this method need 3 3D points ?为什么这种方法需要 3 个 3D 点? You could do with 2 and calculate this.你可以用 2 做并计算这个。

xdiff = x2-x1
ydiff = y2-y1
zdiff = z2-z1
diff = Math.sqrt(xdiff*xdiff + ydiff*ydiff + zdiff*zdiff)

Your dist variable is public and also has a getter() which defeats some of the encapsulation.您的dist变量是公开的,并且还有一个getter()可以破坏一些封装。 Please correct that too.也请更正。 You're better off with a static helper method (not necessarily in the same class) that returns this value.最好使用返回此值的静态辅助方法(不一定在同一类中)。 There is no need to maintain the result since it would not be helpful.没有必要维护结果,因为它没有帮助。

You need to subtract the 3DObj2 position with 3DObj1 position and then compute the length of the results.您需要用 3DObj1 位置减去 3DObj2 位置,然后计算结果的长度。

Eg:例如:

X = 3DObj2.x - 3Dobj1.x;
Y = 3DObj2.y - 3Dobj1.y;
Z = 3DObj2.z - 3Dobj1.z;

and results would be:结果将是:

Distance = X+Y+Z;

or要么

Distance = sqrt(X*X+Y*Y+Z*Z);

I would suggest you use https://docs.oracle.com/cd/E17802_01/j2se/javase/technologies/desktop/java3d/forDevelopers/J3D_1_3_API/j3dapi/javax/vecmath/Point3d.html我建议你使用https://docs.oracle.com/cd/E17802_01/j2se/javase/technologies/desktop/java3d/forDevelopers/J3D_1_3_API/j3dapi/javax/vecmath/Point3d.html

It also has a distance method built in.它还内置了距离方法。

This answer comes with some disclaimers:这个答案附带了一些免责声明:

  1. I don't know how to check when class point3d was introduced so it may have not existed at the time of your op.我不知道如何检查 point3d 类是什么时候引入的,所以在你的操作时它可能不存在。

  2. I can easily imagine you were assigned this problem in an educational setting so you may have been asked to build your own code from scratch.我可以很容易地想象,您在教育环境中被分配了这个问题,因此您可能被要求从头开始构建自己的代码。

Please excuse the lack of formatting, posting from a mobile.请原谅缺乏格式,从手机发布。

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

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