#### Archived

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

# Fastest Distance Calculation

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

## 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 on other sites
there is big article on www.flipcode.com

##### Share on other sites
What are you using it for? The fasest distance function is the one that you can leave squared

##### Share on other sites
How often are you calculating the distance? Have you profled the code and found that your distance calculations are an issue? Otherwise I''d just use the D3DX functions as they are spiffy quick.

Stay Casual,

Ken
Drunken Hyena

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 10
• 16
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632929
• Total Posts
3009276
• ### Who's Online (See full list)

There are no registered users currently online

×