JSFiddle - React, Tailwind, and code Playground
JavaScript
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]
]));