Advertisement

1d array index to 3d indizes

Started by July 29, 2012 08:24 PM
0 comments, last by clb 12 years, 6 months ago
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.
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;
}

This topic is closed to new replies.

Advertisement