检测二维数组中的空闲空间

时间:2016-09-27 14:49:25

标签: javascript arrays

当我添加新项目时,我需要这个角度网格,所以我知道我添加的新元素的尺寸(当没有当前元素的空间时),但为了简化,我假设我有2个维度值为true或false的数组,我想搜索数组中的第一个空闲空间,以查找位置x,y和宽度,可用空间的高度。到目前为止,我有这个:



var array = [
  [false, false, false, false, false, false],
  [false, false, false, false, false, false],
  [false, false, false, false, false, false],
  [false, false, false, true, true, true],
  [false, false, false, true, true, true]
];
var place = {};
loop:
for (var i=0; i<array.length; i++) {
  for (var j=0; j<array[i].length; j++) {
    if (array[i][j] && !place.x && !place.y) {
      place.x = j;
      place.y = i;
      place.width = 0;
      place.height = 0;
      for (var y=i; y<array.length; y++) {
        for (var x=j; x<array[y].length; x++) {
          if (array[y][x]) {
            place.width =  x - j + 1;
            place.height = y - i + 1;
          }
        }
      }
      break loop;
    }
  }
}
console.log(place);
&#13;
&#13;
&#13;

但这对于这样的数组会失败:

var array = [
  [false, false, false, false, false],
  [false, false, false, false, false],
  [false, false, false, false, false],
  [true, true, false, true, true],
  [true, true, false, true, true]
];

如何修复代码以使其适用于此类数组?结果应该是:

{x:0, y:3, width: 2, height: 2}

2 个答案:

答案 0 :(得分:1)

我想你可能会这样做;它将返回左上角的xy坐标以及免费开场的widthheight。如果没有空缺(全部为假),您将返回false

var array = [
  [false, false, false, false, false],
  [false, false, false, false, false],
  [false, false, true, true, true],
  [false, false, true, true, true],
  [false, false, true, true, true]
],
 emptyXY = (a) => { var x,
                        y = a.findIndex(row => (x = row.findIndex(col => col), x !== -1));
                        w = 0,
                        h = 0;
                    while (a[y] && a[y][x+w]) w++;
                    while (a[y+h] && a[y+h][x]) h++;
                    return !!~y && {x:x,y:y,width:w,height:h};
                  };
console.log(emptyXY(array));

答案 1 :(得分:1)

这是另一种选择。我试图用较小的问题来划分问题:

  1. 找到矩阵中的第一个真元素
  2. 从那里,找到与1
  3. 相邻的真实元素的数量

    这是fiddle,这是代码。希望它有所帮助。

    function findCoords(matrix) {
      for (var row = 0; row < matrix.length; row++) {
        for (var col = 0; col < matrix[row].length; col++) {
          if (matrix[row][col]) {
            return createCoordsObj(row, col, matrix);
          }
        }
      }
      return null;
    }
    
    function createCoordsObj(row, col, matrix) {
      return {
        x: col,
        y: row,
        width: find('width', row, col, matrix),
        height: find('height', row, col, matrix)
      };
    }
    
    function find(type, row, col, matrix) {
      var res = 0;
      while (matrix[row] && matrix[row][col]) { // will finish when element in matrix is false || undefined
        res += 1;
        col += type === 'width' ? 1 : 0;
        row += type === 'width' ? 0 : 1;
      }
      return res;
    }
    
    console.log(findCoords([
      [false, false, false, false, false],
      [false, false, false, false, false],
      [false, false, false, false, false],
      [true, true, false, true, true],
      [true, true, false, true, true]
    ]));