How do I get a 1D item which starts at the bottom left of a 2D array after providing 2D coordinates?
var width = 3; // the 2D array width var height = 3; // the 2D array height var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8]; // 1D array console.log(getIndex(0,2)); console.log(getIndex(1,2)); console.log(getIndex(2,2)); console.log(getIndex(0,1)); console.log(getIndex(1,1)); console.log(getIndex(2,1)); console.log(getIndex(0,0)); console.log(getIndex(1,0)); console.log(getIndex(2,0)); //Desired output: 0 1 2 3 4 5 6 7 8 function getIndex(x, y) { return ... ; // how???????? } To illustrate, here's the 2D array of the 1D array in the code above:
X 0---2 0 6 7 8 Y | 3 4 5 2 0 1 2 https://stackoverflow.com/questions/65768310/get-1d-item-with-2d-coordinates-with-1d-array-beginning-at-bottom January 18, 2021 at 11:06AM
没有评论:
发表评论