Point Distance Script

Started by
5 comments, last by Zmurf 18 years, 5 months ago
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?
If it's possible for us to conceive it, than it must be possible.
Advertisement
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)));}
i tried your version and i'm getting the same results back.
If it's possible for us to conceive it, than it must be possible.
What are the (x1, y1) and (x2, y2) that you are passing in?
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 '[]'.
If it's possible for us to conceive it, than it must be possible.
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 ]=-
-=[Megahertz]=-
i found the problem, it was cus the mouse x/y was wrong cus of my viewport.
If it's possible for us to conceive it, than it must be possible.

This topic is closed to new replies.

Advertisement