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.
1d array index to 3d indizes
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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement