• 12
• 14
• 13
• 10
• 11

Point Distance Script

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

Recommended Posts

I am using the following code to calculate the distance between two 2d coordinates.
double PointDist(double x1, double y1, double x2, double y2){
double rise, run, length;
rise = y2 - y1;
run = x2 - x1;
length = sqrt(pow(rise, 2) + pow(run, 2));

return length;
}


The number I always get is 0.6000000x, where x is where the variation actually starts in the number thats returned. Heres a couple of numbers i got back. 0.60000002318111012 0.60000002075052783 Can anyone see the problem?

Share on other sites
It almost looks like maybe the wrong overloaded function is being called (like pow(int, int) instead of pow(double, int)), but I don't really know.

What happens if you try
double PointDist(double x1, double y1, double x2, double y2){   return sqrt(((y2 - y1) * (y2 - y1)) + ((x2 - x1) * (x2 - x1)));}

Share on other sites
i tried your version and i'm getting the same results back.

Share on other sites
What are the (x1, y1) and (x2, y2) that you are passing in?

Share on other sites
x1 and y1 or the coordinates of a point and x2 and y2 are mouse co-ordinates i've converted into ogl points.

I've tested my mouse points and they are correct ogl points. It's sorta working now, but the problem is now it seems to think that the x1, y1 point is frither away from the center than it really is.

ei:

0 <--- Center
|
|
|
X <--- My OGl point

[] <--- Where it think's the point is.

I'm testing this by having a square drawn at this point light up with the distance is < 0.6, but it lights up when the mouse is within 0.6 of point '[]'.

Share on other sites
I'd replace the pow()'s with just a simple run*run and rise*rise.

I'm not sure if it will solve your problems with the inaccuracy, but it will more than likely be faster. Of course if you're only calling this function once in a blue moon, it'd not really make that much difference.

-=[ Megahertz ]=-