Public Group

# the distance between two points (again)

This topic is 4978 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I finally got around to messing with finding the distance between two points. But I noticed that most of the time, the ditance is 0. For example, the code below prints out "Why did it not work?"
int main()
{
int x1 = 10;
int y1 = 10;
int x2 = 70;
int y2 = 70;

if(sqrt((x2-x1)^2 + (y2-y1)^2) != 0)
std::cout << "YEAH, it worked!";
else
std::cout << "Why did it not work?";
}

But if I change the two 70's to 60's, it prints out "YEAH, it worked!" And if I change the two 10's to 0's and the two 70's to 50's, then just do this.. std::cout << sqrt((x2-x1)^2 + (y2-y1)^2); It prints out a 2. What does 2 represent? What am I doing wrong? Thanks

##### Share on other sites
http://en.wikipedia.org/wiki/Distance

Why don't you print out the actual answer? You are assuming anything that isn't a 0 is the right answer.

##### Share on other sites
Instead of using integers try using floats.

##### Share on other sites
Quote:
 std::cout << sqrt((x2-x1)^2 + (y2-y1)^2);

outch...
do you know what operator ^ means? I think you don't.
^ is the EXCLUSIVE OR OPERATOR, not the power.

What you want to do is x*x + y*y, or pow(x,2)+pow(y,2), not x^2+y^2.

##### Share on other sites
In addition to using ^ instead of pow or *, you are comparing the result of sqrt, which I believe returns a double, to an integer 0. You may actually get away with this, since I think 0 is exactly representable in real implementations (erm, maybe even has to be?), but in general, using an equality operator on a floating point value is a bad idea.

const float epsilon = 0.000001;

float x1 = 10.0f;
float y1 = 10.0f;
float x2 = 70.0f;
float y2 = 70.0f;

float dx = x2 - x1;
float dy = y2 - y1;
float distance = sqrt(dx * dx + dy * dy);
if (distance < epsilon)
std::cout << "they are very very close\n";
else
std::cout << "they are not very very close\n";

If you really do want exactly the same, and you really do mean integers, either compare x1 == x2 && y1 == y2, or leave the square root operation out and compare the square of the distance with 0.

##### Share on other sites
Sorry, I have been on a "programming vacation" for a while. I am sort of warming up my programming muscles, so to speak.

Quote:
 Why don't you print out the actual answer?

I did print out the actual answer when I was testing it. I changed the code when I posted it here to illustrate that the answer was usually 0.
Quote:
 do you know what operator ^ means? I think you don't.

You would be correct. I copied the code out of another post a while back. I just assumed it was for powers.
Quote:
 the result of sqrt, which I believe returns a double

Good to know. I guess I should have done a little more homework first. :)

At any rate, as usual, you guys really came through. Thanks so much for all your input.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634074
• Total Posts
3015347
×