idx(x, y, z)
This is a crucial helper function for this sketch. Since JavaScript arrays are 1D, we need a way to map 3D coordinates to 1D indices. The formula works like converting a 3D address into a single number: think of it as stacking NรN grids on top of each other, then finding the position within that stack.
function idx(x, y, z) { return x + N * (y + N * z); }
๐ง Subcomponents:
return x + N * (y + N * z);
Converts 3D coordinates (x, y, z) into a single index for the 1D array, enabling storage of 3D data in a flat array
Line by Line:
return x + N * (y + N * z);- Calculates a unique index by treating the 3D grid as layers. Each z-layer contains NรN cells, each row contains N cells. This formula maps any (x,y,z) coordinate to a unique position in a 1D array.