Sign in to follow this  
coffeecup

1d array index to 3d indizes

Recommended Posts

i flattened a 3d array z*size*size + y*size + x to get the 1D array index and now i would like to convert back, so far i've got

x = i % (size*size) % size
y = i % ( size*size ) / size
z = i / ( size * size )

but this doesn't work when my index is > size.

Share this post


Link to post
Share on other sites
Try this:

[code]
const int size = 100;

int Array3DTo1D(int x, int y, int z)
{
assert(0 <= x,y,z < size);
return (z * size + y) * size + x;
}

void Array1DTo3D(int index, int &outX, int &outY, int &outZ)
{
assert(0 <= index < size*size*size);
outX = index % size;
index /= size;
outY = index % size;
index /= size;
outZ = index;
}
[/code] Edited by clb

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this