Search code examples
c#vectormathematical-optimizationgame-development

How to Find the Cube Vector coord (X, Y, Z) of an Index number?


I can convert a Coordinate into an Index like so:

index = z + y * maxZ + x * maxY * maxZ

So for instance, I have a cube of 4x8x4... But my brain, isn't connecting how to convert it back. I want to take a given 'Index' and get 'x', 'y' and 'z'. That is in a 'quick' smart way. I know I could do it with loops and stuff.


Solution

  • For index = z + y * maxZ + x * maxY * maxZ, you get (x, y, z) = (index / maxY / maxZ, (index / maxZ) % maxY, index % maxZ).