javascript - How to write an array with three indices in JS? -
i have series of equal cubes in 3d space known centers. given cube center, can give rest of information cube. in fact, each center's coordinates in 3d represents sort of id corresponding cube.
in program in javascripts, each cube object. has properties such center, color, vertices, etc. can have array such "allcubes" can store cubes in 1 place , recall them "allcubes[0]", "allcubes[1]",... . however, want write program in way when give center coordinates cube i'm pointing at.
of course, can make loop , use comparison find out not efficient when number of cubes high. looking array accepts 3 arguments, example if write "allcubes[i,j,k]" can cube pointing @ or may use dynamic object naming , assign each cube name based on center coordinates don't know how this.
i appreciate if me out. thanks!
javascript doesn't have multi-dimensional arrays. instead, make arrays of arrays 2 dimensions, arrays of arrays of arrays 3 dimensions, , on. access successive dimensions, index return value of previous access. access element of 3-dimensional array, it's arrayname[i][j][k]
.
here's how loop on entire array, processing every cube.
for (var = 0; < allcubes.length; i++) { (var j = 0; j < allcubes[i].length; j++) { (var k = 0; k < allcubes[i][j].length; k++) { var thiscube = allcubes[i][j][k]; // thiscube } } }
Comments
Post a Comment