For the angular gridster, I need to determine the dimensions of a new element being added when there is no space for the current element. Let's simplify this by considering a 2-dimensional array with values true or false. My goal is to search for the first available space in the array to find the position (x, y) and the width and height of that free space. Here is what I have so far:
var matrix = [
[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<matrix.length; i++) {
for (var j=0; j<matrix[i].length; j++) {
if (matrix[i][j] && !place.x && !place.y) {
place.x = j;
place.y = i;
place.width = 0;
place.height = 0;
for (var y=i; y<matrix.length; y++) {
for (var x=j; x<matrix[y].length; x++) {
if (matrix[y][x]) {
place.width = x - j + 1;
place.height = y - i + 1;
}
}
}
break loop;
}
}
}
console.log(place);
However, this approach fails for arrays like:
var matrix = [
[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]
];
I am seeking guidance on how to adjust my code to work for arrays like the one above. The expected result should be:
{x:0, y:3, width: 2, height: 2}