tags:

views:

849

answers:

9

Hi, 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 Punkt {

    public double x;
    public double y;

    Punkt(double xkoord, double ykoord){
        this.x = xkoord;
        this.y = ykoord;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }    
}

package org.totalbeginner.tutorial;

The second class.

package org.totalbeginner.tutorial;
public class Strecke{

    double x;
    double y;

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

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

I am a total programmer newbie and i am not sure how to get a point object (the middlepoint) between both defined points. it is part of my java class, so yes it is somekind of homework.

I can create point objects but i am not sure how to return a point object through my mittelpunkt() method that lies between those two point objects. Yes i am fully aware that this is totally trivial question, but i am just at the beginning. Don't hate me because i am worthless :)

A: 

Try Pythagorean Theorem.

rjstelling
+4  A: 

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

Math.sqrt(Math.pow((p2.getX() - p1.getX()), 2) + Math.pow((p2.getY() - p1.getY()), 2))
Oli
+15  A: 

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

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

Don't you just love ASCII art?

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

public Punkt mittelpunkt (Punkt p1, Punkt p2) {
    return new Punkt ((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 Punkt.java:

class Punkt {
    double x, y;
    Punkt (double xkoord, double ykoord) {
        this.x = xkoord;
        this.y = ykoord;
    }
    public double getX() {
        return x;
    }
    public double getY() {
        return y;
    }
}

Then Strecke.java:

public class Strecke {
    Punkt p1, p2;
    Strecke (Punkt punkt1, Punkt punkt2) {
        this.p1 = punkt1;
        this.p2 = punkt2;
    }
    public Punkt mittelPunkt() {
        return new Punkt ((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[]) {
        Strecke s = new Strecke (new Punkt(2.0, 2.0), new Punkt(5.0, 6.0));
        Punkt mp = s.mittelPunkt();
        System.out.println ("Midpoint = (" + mp.getX() + "," + mp.getY() + ")");
        double as = s.abstand();
        System.out.println ("Length   = " + as);
    }
}

These two files, when compiled and run, generate the correct:

Midpoint = (3.5,4.0)
Length   = 5.0
paxdiablo
Some people are such show-offs.
Oli
Guilty as charged, your honor :-)
paxdiablo
U beat me with nice root pics. Lovin it :)
Johannes Schaub - litb
All assuming euclidian geometry. None of these are correct in other coordinate systems like cylindrical, elliptical, hyperbolic, etc.
duffymo
I believe that would be covered by my "flat surface" clause, @duffymo.
paxdiablo
Agreed, I acknowledge that with my "assuming euclidian" preface. Just a point of interest for a noob and anyone who's still a card-carrying member of the Flat Earth Society. They might not have heard of other coordinate systems.
duffymo
+7  A: 
 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

Johannes Schaub - litb
or you can use Math.hypot() -- slightly shorter
MatrixFrog
A: 

They teach Pythagoras' theorem in early high school here. There is a good chance that anything you learnt in maths will be useful in computer science at some point.

Peter Lawrey
Really? I learned how to look down Sarah Jane Smith's blouse in high school math. I'm pretty sure I've never been able to use that in my long career in computers :-) [not her real name, obviously. I'm a happily married man now and don't want any trouble].
paxdiablo
I had male maths teachers, but I did have female English teachers I took a shine to. ;)
Peter Lawrey
A: 
eKek0
+3  A: 

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., 'Strecke') 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 ..

Punkt(Punkt a, Punkt 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:

Punkt p1 = new Punkt(2,2);
Punkt p2 = new Punkt(4,4);
Punkt midpoint = new Punkt(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(Punkt a, Punkt 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:

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

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

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

public Punkt mittelpunkt() {
    // calculate the middle x and y
    double x = (p1.getX() + p2.getX()) / 2;
    double y = (p1.getY() + p2.getY()) / 2;
    // now make a new Punkt with those values, and return it
    return new Punkt(x, y);
}
Paul Fisher
+4  A: 

A lazy guy solution

fortran
...But no less incorrect, and fewer lines of code.
banjollity