'un-flattening' a 3D array

Started by
1 comment, last by cephalo 9 years, 1 month ago

So in the case of a 2D array, you can have 2 coordinates to describe a location in that array. You can also describe the data as a 1D array by calculating the raw index by x + y * width. If you have the raw index, you can get the y coordinate with an integer divide like so: y = i/width. Then you can get the x coordinate with the modulus operator x = i % width.

This should also be possible to get the coordinates for a 3D array, but I can't figure it out. If you know the width(maxX), height(maxY) and depth(maxZ) of a 3D array, you should be able to convert a raw index into coordinates... right?

Advertisement

index = x + y * width + z * width * height

z = index / (width * height)
y = (index - z * (width * height)) / width
x = index - y * width + z * width * height

Thanks!

This topic is closed to new replies.

Advertisement