[英]Get 1D item with 2D coordinates with 1D array beginning at bottom left of 2D array
在提供 2D 坐標后,如何獲得從 2D 數組左下角開始的 1D 項目?
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????????
}
為了說明,這里是上面代碼中一維數組的二維數組:
X
0---2
0 6 7 8
Y | 3 4 5
2 0 1 2
*二維數組中的數字代表一維索引中的 position。
要提供所需的訪問權限(使用反向行順序),您可以使用以下公式:
indx = (height - 1 - y) * width + x
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.