• Advertisement
Sign in to follow this  

1d array index to 3d indizes

This topic is 2090 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

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
Advertisement
Try this:


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;
}
Edited by clb

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement