如何获取矩阵中所选元素周围的元素

时间:2014-10-18 01:26:47

标签: javascript jquery matrix

我有一个3x3矩阵,当我在这个矩阵中选择一个元素时,我需要找到该元素周围的元素(即top,left,right,bottom)。 下面是我用来创建3x3矩阵的代码 -

You can view this

// declare array of multi dimensions
var arrOfArr = new Array(3);
// or declare as [[],[],[]] as suggested by Elijah

// allocate storage
for (var x = 0; x < arrOfArr.length; x++) {
    arrOfArr[x] = new Array(3);
}

// Populate the array
// first array
arrOfArr[0][0] = "00";
arrOfArr[0][1] = "01";
arrOfArr[0][2] = "02";

// second array
arrOfArr[1][0] = "10";
arrOfArr[1][1] = "11";
arrOfArr[1][2] = "12";

// third array
arrOfArr[2][0] = "20";
arrOfArr[2][1] = "21";
arrOfArr[2][2] = "22";

alert(arrOfArr);

如果我在此矩阵中选择一个元素,我需要获得所选元素的top,left,right和bottom元素。我怎么能这样做

3 个答案:

答案 0 :(得分:0)

此函数将为您提供一个对象,表示矩阵xymatrix处所选元素周围的元素:

function getSurroundingElements(x, y, matrix) {
  var x_limit = matrix.length;
  if (x_limit == 0) return null; //matrix is empty

  var y_limit = matrix[0].length; //Assumes all rows in the matrix are of same length (otherwise, not a matrix, right?)

  return {
    'tl':((x-1 >= 0 && y-1 >= 0)?matrix[x-1][y-1]:null),
    'tc':((y-1 >= 0)?matrix[x][y-1]:null),
    'tr':((x+1 < x_limit && y-1 >= 0)?matrix[x+1][y-1]:null),

    'ml':((x-1 >= 0)?matrix[x-1][y]:null),
    'mr':((x+1 < x_limit)?matrix[x+1][y]:null),

    'bl':((x-1 >= 0 && y+1 < y_limit)?matrix[x-1][y+1]:null),
    'bc':((y+1 < y_limit)?matrix[x][y+1]:null),
    'br':((x+1 < x_limit && y+1 < y_limit)?matrix[x+1][y+1]:null)
  };
}

注意:如果矩阵为空并且null是为非法值设置的值(即边缘情况),则返回null - 感谢Troy Gizzi指出完整的解决方案。

答案 1 :(得分:0)

此函数将返回一个包含四个值的对象,该边缘的值将返回undefined。

var MAXTRIX_LENGTH = 3;
function select(x, y) {
    var maxIndex = MAXTRIX_LENGTH - 1;
    if (x >= 0 && x <= maxIndex && y >= 0 && y <= maxIndex) {
        return {
            "top": y > 0 ? arrOfArr[x - 1][y] : undefined,
            "bottom": y < maxIndex ? arrOfArr[x + 1][y] : undefined,
            "left": x > 0 ? arrOfArr[x][y - 1] : undefined,
            "right": x < maxIndex ? arrOfArr[x][y + 1] : undefined
        };
    } else {
        return undefined;
    }
}

var result = select(0, 1);

if (result == undefined) {
    alert("Index over range.")
} else {
    alert("top: " + result.top + " bottom: " + result.bottom + " left: " + result.left + " right: " + result.right);
}

答案 2 :(得分:0)

请检查我的小提琴。 DEMO

var arrOfArr = new Array(3);
for (var x = 0; x < arrOfArr.length; x++) {
    arrOfArr[x] = new Array(3);
}
arrOfArr[0][0] = "450";
arrOfArr[0][1] = "212";
arrOfArr[0][2] = "102";
// second array
arrOfArr[1][0] = "120";
arrOfArr[1][1] = "1211";
arrOfArr[1][2] = "1112";
// third array
arrOfArr[2][0] = "220";
arrOfArr[2][1] = "2121";
arrOfArr[2][2] = "2222";
$("input[type=submit]").click(function (){
    var selectedVal=($("input[type=text]").val());
    var result="Element not found in matrix";
    //alert( arrOfArr[2][0]==selectedVal);
    ($.each(arrOfArr,function(index,value){
        $.each(value,function(subIndex,subVal){
            if(selectedVal==subVal){
            var leftval = checkNoElement(arrOfArr, index, subIndex-1);
            var rightval=checkNoElement(arrOfArr, index,subIndex+1);
            var upperVal=checkNoElement(arrOfArr, index-1, subIndex);
            var lowerVal=checkNoElement(arrOfArr,index+1, subIndex);
                result=(" Left:"+leftval+ " \n right:" + rightval + " \n Upper:" + upperVal + " \n Lower:" + lowerVal);
            }
          });
    }));
    alert(result)
});

function checkNoElement(element,index1,index2){
var firstChk=element[index1]== undefined ? undefined :"";
    if(firstChk==undefined){
        return "no element"    
    }else{
    return (element[index1][index2]== undefined ? "no element":element[index1][index2]);
    }

}