Sign in to follow this  
Zmurf

Point Distance Script

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 ]=-

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this