#### Archived

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

# 2d->3d convertion of this cool distance approximation

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

## Recommended Posts

i ran a search on this section looking for fast distance approximations, and this was the most interesting chain I found:
int findDist(int x, int y)
{
if (y < x)
{
int t = x;
x = y;
y = t;
}
return y + 5 * x / 16;
}

int finddist(int x,int y)
{
if(x < y) return x + y*5/16;
else return y + x*5/16;
}

#define FOS 0.31285
int finddist(int x,int y)
{
return (x < y ? (x + y*FOS) : (y + x*FOS));
}

#define FOS(v) (((v<<2)+v)>>4)
__forceinline int finddist(int x,int y)
{
return (x < y ? (x + FOS(y)) : (y + FOS(x)));
}


the final one has exactly the amount of complexity im looking for and seems like its really fast. anyhow, i was just wondering, does anyone know how to write that approximation forumla for integer 3d points?

##### Share on other sites
dist3d(x,y,z) = dist2d(dist2d(x,y),z)

##### Share on other sites
cool, thanx man

i played around with it a little to try and reduce the error% and I found htat this suits my needs perfectly:

dist3d(x,y,z>>1)

where dist3d(x,y,z) = dist2d(dist2d(x,y),z)

....

and a rewrite:

int a;
__forceinline int finddist(int x,int y,int z)
{
z>>=1;
int a = (x < y ? (x + FOS(y)) : (y + FOS(x)));
return (a < z ? (a + FOS(z)) : (z + FOS(a)));
}

[edited by - Uthman on August 17, 2003 7:07:22 PM]

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 21
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632606
• Total Posts
3007383

×