Last active
November 27, 2022 09:02
-
-
Save unitycoder/2d98612217b2cb6ace55 to your computer and use it in GitHub Desktop.
Access 2D & 3D Flat Arrays 1D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2D array to 1D array | |
array[x,y] = array[y*width+x] | |
// 3D array to 1D array | |
array[x, y, z] = array[x+width*(y+depth*z)] | |
array[x, y, z] = array[x + HEIGHT* (y + WIDTH* z)] | |
array[x, y, z] = array[(z * xMax * yMax) + (y * xMax) + x] | |
// reverse: get x,y,z values from 1d index | |
var x = index % cells; | |
var y = (index / cells) % cells; | |
var z = (index / (cells * cells)) % cells; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment