Jump to content
  • Advertisement
Sign in to follow this  
coffeecup

1d array index to 3d indizes

This topic is 2178 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!