Archived

This topic is now archived and is closed to further replies.

Fastest Distance Calculation

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Since square roots are so cpu intensive I try to stay away from them. I use to use a fast distance calculator in 2d space it was 97% accurate but way faster (think used Newton Raphson approx method), here's the code...
int FastDistance(int dx, int dy)
// Calculate the distance quickly given dx, dy (can be negative)

{
dx=abs(dx);
dy=abs(dy);
int mn = min(dx,dy);

return(dx+dy-(mn>>1)-(mn>>2)+(mn>>4));
} //End FastDistance

I tried to adapt it over to 3d, by adding a dz (also changed all to floats) but couldn't get it to give me reasonable values. Anyone have the 3d equivalent ?? Righ now I am using D3DXVec3Length which I believe uses sqrt(dx+dy+dz). In general what other d3d or math functions do you try to stay way from (i.e. tan(), D3DXVec3Length, D3DXVec3Dot ...etc). Not saying these functions are bad just using as example... Thanks, rhuala [edited by - rhuala on November 28, 2003 9:25:07 AM]

Share this post


Link to post
Share on other sites