#### Archived

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

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

This topic is 5602 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
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013707
×