[ACCEPTED]-Calculating the distance between two points-java

Accepted answer
Score: 50

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

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

But, if all 12 you want is the midpoint of your two points, you 11 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 10 will return a brand new point object with 9 the points set to the middle of the given 8 two points (without having to concern yourself 7 with any other math). And, since your second 6 class is a line, you only need the two end 5 points to describe it, so I'd make some 4 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 3 compiled and run with the endpoints 2,2 and 2 5,6 (the hypotenuse of a classic 3/4/5 right-angled 1 triangle), generate the correct:

Midpoint = (3.5,4.0)
Length   = 5.0
Score: 20

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

Math.sqrt(Math.pow((p2.getX() - p1.getX()), 2) + Math.pow((p2.getY() - p1.getY()), 2))

0

Score: 11
 X
 +
 |\
 | \
a|  \c
 |   \
 |    \
 +-----+ 
    b   Y

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

sqrt(a^2 + b^2);

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

Lookup 1 the Pythagorean theorem

Score: 6

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

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

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

.. then, elsewhere 8 let's say you already have a couple of points 7 you want to use this on, you use the constructor 6 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 5 two points, that's not really an attribute 4 of either point, so it makes sense to use 3 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 2 in the calling code, you can use it this 1 way:

Point p1 = new Point(2,2);
Point p2 = new Point(4,4);
System.out.println("Distance between them is " + Point.distance(p1, p2));
Score: 2

You can use a Maths function for this:

public Point midpoint() {
    //Calculate the difference between the old and new x/y
    double dx = p1.getX() - p2.getX();
    double dy = p1.getY() - p2.getY();

    double newX = Math.pow(dx, 2D);
    double newY = Math.pow(dz, 2D);
    return new Point(newX, newZ);
}

Math.pow handles 3 the issues with negative values and etc. For 2 you, using Math.pow gives you a safe method 1 because it has a lot of checks built inside.

Score: 1

You can use the Pythagorean Theorem, as other said. Here is a visually demostration 1 from the Wolfram Demostration Project.

alt text http://demonstrations.wolfram.com/DistanceBetweenTwoPoints/HTMLImages/index.en/popup_5.jpg

Score: 1

In your second class, it looks like you're 12 trying to set the values of x and y that are 11 used to construct your mp variable. All your 10 formulas are correct, but you need to consider 9 the order that everything is executed. In the 8 code as it is, it's creating the x and y variables, which 7 start out as 0, then the various Points. x and 6 y are still 0, so mp is set to Point(0, 0).

What you probably 5 want to do is change the return type of midpoint to Point, so that 4 when you call that function, you get back 3 a Point. Then you can create a new Point object with 2 the values you calculate. It should look 1 more like this:

public Point midpoint() {
    // calculate the middle x and y
    double x = (p1.getX() + p2.getX()) / 2;
    double y = (p1.getY() + p2.getY()) / 2;
    // now make a new Point with those values, and return it
    return new Point(x, y);
}

More Related questions