-
-
Save barbuza/540763b1a1443d9e5f68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getEmptyIndexes(arr) { | |
var px; | |
var py; | |
var i; | |
var rows = []; | |
var columns = []; | |
for (i = 0; i < arr.length; i++) { | |
columns[i] = 0; | |
} | |
for (i = 0; i < arr[0].length; i++) { | |
rows[i] = 0; | |
} | |
for (px = 0; px < arr.length; px++) { | |
for (py = 0; py < arr.length; py++) { | |
if (arr[px][py] == 1) { | |
//помечаем столбец, что он уже не пустой | |
columns[px] = 1; | |
//помечаем строку, что она уже не пустая | |
rows[py] = 1; | |
} | |
} | |
} | |
var emptyRowsIndexes = []; | |
for (i = 0; i < rows.length; i++) { | |
if (rows[i] == 0) { | |
emptyRowsIndexes.push(i); | |
} | |
} | |
var emptyColumnsIndexes = []; | |
for (i = 0; i < columns.length; i++) { | |
if (columns[i] == 0) { | |
emptyColumnsIndexes.push(i); | |
} | |
} | |
return [emptyRowsIndexes, emptyColumnsIndexes]; | |
} | |
function getCropIndexesByEmptyIndexes(emptyIndexex, size) { | |
var i; | |
var cropFromBegin = 0; | |
for (i = 0; i < size; i++) { | |
if (emptyIndexex[i] == i) { | |
//start from left; | |
cropFromBegin++; | |
} else { | |
break; | |
} | |
} | |
var cropFromEnd = 0; | |
for (i = size - 1; i >= 0; i--) { | |
if (emptyIndexex.pop() == i) { | |
//start from left; | |
cropFromEnd++; | |
} else { | |
break; | |
} | |
} | |
return [cropFromBegin, cropFromEnd]; | |
} | |
function splitToFilledRectangles(arr) { | |
var emptyIndexes = getEmptyIndexes(arr); | |
var emptyRows = emptyIndexes[0]; | |
//trace("emptyRows",emptyRows); | |
var cropRows = getCropIndexesByEmptyIndexes(emptyRows, arr[0].length); | |
//trace("cropRows",cropRows); | |
//если индексы пустых строк идут подряд, убрать лишние | |
var emptyColumns = emptyIndexes[1]; | |
var cropColumns = getCropIndexesByEmptyIndexes(emptyColumns, arr.length); | |
//trace("cropColumns",cropColumns); | |
var rect = [cropColumns[0], cropRows[0], arr.length - cropColumns[0] - cropColumns[1], arr[0].length - cropRows[0] - cropRows[1]]; | |
return [rect]; | |
} | |
var temp = [ | |
[0, 0, 0], | |
[0, 1, 0], | |
[0, 0, 0], | |
[0, 1, 0], | |
[0, 1, 0], | |
[0, 0, 0] | |
]; | |
//trace(splitToFilledRectangles(temp)); | |
console.log(splitToFilledRectangles(temp)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment