简体   繁体   中英

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

This is my class 3DObj in 3DObj.java.

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

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.

How can I calculate the distance between 3DObj1 and 3DObj2?

You never assign your dist variable to the result of your dist() function. This variable is auto-assigned to 0.0 and so it shows you 0.0.

All in all, your set up for this class is not good because dist is not a characteristics of your object. The function calculating distance should be created in another class, which would treat various operations on groups of 3DObj .

Another solution would be to have distance of this 3DObj to another 3DObj calculated through object1.dist(object2) . In such case you don't need dist variable anyway (again, it is not a characteristics of one object).

You don't need the dist member in 3DObj . Distance is always measured between two object instances. Respectfully, no need for 3DObje.getDist()

You need to fix your dist() function:

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. With your current code, it seems you are trying to calculate the distance between 3 points (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). 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()).

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. Let's call is 3DObjCollection, and let's say it calculates the distance between two 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:

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 .

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

Why does this method need 3 3D points ? You could do with 2 and calculate this.

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. 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.

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

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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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