5

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. To do this I have used getters and setters. My next task is to create a method within my main class (which I shall call "distanceTo") that calculates the distance between two points.

How do I go about creating the method "distanceTo" that calculates the distance between two points by taking in the x,y,z coordinates ? 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

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.

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;

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

    }

}
kdopen
  • 8,032
  • 7
  • 44
  • 52
Idan Gelber
  • 195
  • 3
  • 4
  • 11
  • you want a distanceTo method in the `Point3d` class. Something like: `public double distanceTo(Point3d p){ ... }` there you can do your distance calculations – Dude Jun 02 '15 at 14:04

5 Answers5

8

As @Dude pointed out in the comments, you should write a method:

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:

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:

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

P.S. my first answer :)

KarlTheGreat
  • 142
  • 10
  • Is there a way to use the first method you used 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)); } but pass two points instead of one through so that I could get the distance between any two points, not just the first and another but maybe the second and another or the 9th and the 5th point... – Idan Gelber Jun 02 '15 at 14:42
  • Yes if you use the static Method, you just write it in your Point3d Class and then you can call it via: `Point3d.getDistance(myPoint1, myPoint2);` wich will return the distance between the two points. – KarlTheGreat Jun 02 '15 at 14:48
  • if you want to learn more about static fields and method you can hook up on the [doc](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – KarlTheGreat Jun 02 '15 at 14:51
  • No problem, nice to help on my 1st answer :P Since you understand it now, you should accept an answer, so others see your queststion ist answered + to give some free reputation. Since you are new to SE like me you can read up when you should [accept](http://stackoverflow.com/help/accepted-answer) an answer. – KarlTheGreat Jun 02 '15 at 15:00
  • shouldn't you *call* the method (i.e. `p.getxCoord()` instead of `p.getxCoord`)? – Willem Van Onsem Jun 02 '15 at 15:02
  • if you want an abs() distance result do `(x-X)*(x-X)` where `X` is the greater number... the result will never be negative. – Tcll Apr 10 '17 at 03:15
  • Squaring any number gives a positive number. There is no difference between `(10-1)*(10-1)` and `(1-10)*(1-10)` – KarlTheGreat Apr 19 '17 at 14:03
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));
}

Add this to your Point3d class. When you need to calculate the distance in the TestPoints class, you do something like

double distance = firstPoint.distanceTo(secondPoint);
Jan Martiška
  • 1,151
  • 5
  • 7
3

You have two possible approaches, according to what you want to achieve.

You can put your "distanceTo" method inside the class 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:

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 :)

Diego Martinoia
  • 4,592
  • 1
  • 17
  • 36
  • Perhaps it is better to call the methods of that: `that.getxCoord` instead of `that.x`. If one later wants to provide an interface of point and have several classes (polar, euclidean,...) this method will still work. – Willem Van Onsem Jun 02 '15 at 15:03
  • It was just for the sake of an example: If you read the question, OP provides getters, but you are right. – Diego Martinoia Jun 02 '15 at 15:35
0

Just use the getters

float distance = Math.sqrt(Math.pow(secondPoint.getXCoord() - firstPoint.getXCoord()), 2) + ...)
Shaun
  • 3,777
  • 4
  • 25
  • 46
0

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. Then, at the end of your main method, you can do:

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

Or, add a static method to your main TestPoints class:

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:

System.out.println(distanceBetween(firstPoint, secondPoint));
System.out.println(distanceBetween(tenthPoint, ninthPoint));
David Lavender
  • 8,021
  • 3
  • 35
  • 55