Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the distance between two points

Tags:

java

I need to create a class which calculates the distance between two points. I am stuck and I am a total beginner. Here are my classes:

package org.totalbeginner.tutorial;

public class Point {

    public double x;
    public double y;

    Point(double xcoord, double ycoord){
        this.x = xcoord;
        this.y = ycoord;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }    
}

The second class.

package org.totalbeginner.tutorial;

public class Line {

    double x;
    double y;

    Point p1 = new Point(2.0,2.0);
    Point p2 = new Point(4.0,4.0);
    Point mp = new Point(x,y);

    public void midpoint() {
        x = (p1.getX() + p2.getX()) / 2;
        y = (p1.getY() + p2.getY()) / 2;
    }
}

I am not sure how to get a point object (the middle point) between both defined points.

I can create point objects but I am not sure how to return a point object through my midpoint() method that lies between those two point objects.

like image 731
mrt181 Avatar asked May 30 '09 14:05

mrt181


4 Answers

The distance between two points (x1,y1) and (x2,y2) on a flat surface is:

    ____________________
   /       2          2
 \/ (y2-y1)  + (x2-x1)

But, if all you want is the midpoint of your two points, you should change your midpoint function to:

public Point midpoint (Point p1, Point p2) {
    return new Point ((p1.getX() + p2.getX()) / 2, (p1.getY() + p2.getY()) / 2);
}

This will return a brand new point object with the points set to the middle of the given two points (without having to concern yourself with any other math). And, since your second class is a line, you only need the two end points to describe it, so I'd make some minor changes.

First Point.java:

class Point {
    double x, y;
    Point (double xcoord, double ycoord) {
        this.x = xcoord;
        this.y = ycoord;
    }
    public double getX() { return x; }
    public double getY() { return y; }
}

Then Line.java:

public class Line {
    Point p1, p2;
    Line (Point point1, Point point2) {
        this.p1 = point1;
        this.p2 = point2;
    }
    public Point midpoint() {
        return new Point ((p1.getX()+p2.getX())/2, (p1.getY()+p2.getY())/2);
    }
    public double abstand() {
        return Math.sqrt(
            (p1.getX() - p2.getX()) *  (p1.getX() - p2.getX()) + 
            (p1.getY() - p2.getY()) *  (p1.getY() - p2.getY())
        );
    }
    static public void main (String args[]) {
        Line s = new Line (new Point(2.0, 2.0), new Point(5.0, 6.0));
        Point mp = s.midpoint();
        System.out.println ("Midpoint = (" + mp.getX() + "," + mp.getY() + ")");
        double as = s.abstand();
        System.out.println ("Length   = " + as);
    }
}

These two files, when compiled and run with the endpoints 2,2 and 5,6 (the hypotenuse of a classic 3/4/5 right-angled triangle), generate the correct:

Midpoint = (3.5,4.0)
Length   = 5.0
like image 155
paxdiablo Avatar answered Sep 23 '22 01:09

paxdiablo


Simple Pythag... root(dx^2 + dy^2)

Math.sqrt(Math.pow((p2.getX() - p1.getX()), 2) + Math.pow((p2.getY() - p1.getY()), 2))
like image 26
Oli Avatar answered Sep 22 '22 01:09

Oli


 X
 +
 |\
 | \
a|  \c
 |   \
 |    \
 +-----+ 
    b   Y

Imagine X and Y are your points on a flat surface. Then a is X.y - Y.y and b is Y.x - X.x . The length of c is their distance, and is the length of the hypotenuse of that triangle. It is calculated using

sqrt(a^2 + b^2);

Since you see we are squaring a and b, the sign of them isn't relevant - it will come down to the same. So this method always works, where ever the points lie.

Lookup the Pythagorean theorem

like image 30
Johannes Schaub - litb Avatar answered Sep 22 '22 01:09

Johannes Schaub - litb


Do you really need the distance, or are you trying to just get the midpoint? Because from your code snippet, it kind of looks like you just want to create a new point that is half-way between two existing points.

If you're really just after the midpoint, you don't really need an entire 2nd class (i.e., 'Line') to accomplish that. Since the thing you are trying to find is also a point, it makes sense to add a constructor to your existing Point class, like so ..

Point(Point a, Point b)
{
  x = (a.x + b.x) / 2;
  y = (a.y + b.y) / 2;
}

.. then, elsewhere let's say you already have a couple of points you want to use this on, you use the constructor thus:

Point p1 = new Point(2,2);
Point p2 = new Point(4,4);
Point midpoint = new Point(p1, p2);

and if you really want distance between two points, that's not really an attribute of either point, so it makes sense to use a static method for that, like so

public static double distance(Point a, Point b)
{
  double dx = a.x - b.x;
  double dy = a.y - b.y;
  return Math.sqrt(dx * dx + dy * dy);
}

and back in the calling code, you can use it this way:

Point p1 = new Point(2,2);
Point p2 = new Point(4,4);
System.out.println("Distance between them is " + Point.distance(p1, p2));
like image 38
JustJeff Avatar answered Sep 20 '22 01:09

JustJeff