I need to extract a 4D position from a 1D array. I can see how it goes for 2D and 3D but I'm having a hard time wrapping my head around the 4th dimension..
For 2D:
int* array = new int[width * height];
int index = y * width + x;
int x = index / height
int y = index - x * height;
For 3D:
int* array = new int[width * height * depth];
int index = z * width * height + y * width + z;
int x = index / (height * depth);
int y = index - (x * height * depth) / depth;
int z = index - (x * height * depth) - (y * depth);
For 4D ?
int* array = new int[width * height * depth * duration];
int index = w * width * height * depth + z * width * height + y * width + w;
int x = index / (height * depth * duration);
int y = ??
See Question&Answers more detail:os