Permalänk

Need help with an exercise

Hi! This will be my very first post at this forum. I recently started out my first programming course just one week ago. I find it very interesting but at the same time (because I have no former practice in the field) I also find it extremely hard : )
This is the thing, I'm trying to figure out a way to find out the distance between 2 objects on a x,y-coordinate system (that correct English?), but so far I haven't succeeded. This is the code I used in the previous exercise where you had to calculate the distance between 1 object and the origin:

public double distanceOrigo(){ int x = this.getXPos(); int y = this.getYPos(); //Pythagoras double d = Math.sqrt((x*x)+(y*y)) return d; }

And then I used the command:

System.out.println("Distance to origo:" + t1.distanceOrigo()); //t1 is the first object in the system, t2 is the second one

Now for 2 objects in the system (both objects have a x,y-coordinate). I'm supposed to use a similar approach as in this previous code but I dont know what to do : (

Best regards,
Mikael

Permalänk
Medlem

You want to do something like this:

Distance(objectA, objectB) is equal to sqrt((objectB.X - objectA.X)^2 + (objectB.Y - objectA.Y)^2).

In other words you simply take the square root of the difference between each axis squared.

Visa signatur

Corsair 750D | i5 4670k | ASUS Z87-a | 8GB Corsair Dominator | Asus GTX770-DC2OC | Samsung 840 EVO 250GB | WD 500GB X2

Permalänk

@reflect:

Thank you so much for your reply, at last I got it : ) I have a presentation tomorrow in class and it seems I understand now. Again, thank you : )