简体   繁体   中英

How to get the elements surrounding a selected element in a matrix

I have a 3x3 matrix and when I select a element in this matrix, I need to find elements surrounding that element(ietop, left, right, bottom). Below is the code I used to create a 3x3 matrix -

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);

If I select an element in this matrix, I need to get top,left,right and bottom element of the selected element. How can I do this

This function will give you an object representing the elements around the selected element at x , y in the matrix matrix :

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)
  };
}

NOTE: null is returned if the matrix is empty and null is the value set for illegal values (ie edge cases) - Thanks to Troy Gizzi for pointing out a complete solution.

This function will return a object contains four value, the value in the edge will return 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);
}

Please chech my fiddle. 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]);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM