I need this for angular gridster when I add new item so I know the dimension of the new element I'm adding (when there is no space for current element), but to simplify lets assume that I have 2 dimension array with value true or false and I want to search the first free space in array to find position x,y and width,height of free space. So far I have this:
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);
but this will fail for array like this:
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]
];
How can I fix my code to make it work for array like this? The result should be:
{x:0, y:3, width: 2, height: 2}