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;
}
Point Distance Script
I am using the following code to calculate the distance between two 2d coordinates.
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?
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
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)));}
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 '[]'.
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 '[]'.
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 ]=-
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 ]=-
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement