ActiveTrak Subregions
by Matthew Vasallo
JavaScript
const input = [
[5, 0, 25, 5, 145, 250],
[0, 5, 95, 115, 165, 250],
[15, 5, 175, 250, 185, 160],
[5, 0, 145, 250, 245, 140],
[115, 210, 60, 5, 230, 220],
[0, 80, 45, 95, 170, 145],
];
const findCellAboveThreshold = (grid, threshold) => {
const cellsThatMatch = []
for (let y = input.length - 1; y >= 0; y--) {
for (let x = 0; x < input[y].length; x++) {
const cellValue = input[y][x]
if (cellValue > threshold) {
console.log(`hit cell{ ${x}, ${input[y].length - y - 1}}`)
cellsThatMatch.push({
threshold: cellValue, x, y: input[y].length - y - 1,
})
}
}
}
return cellsThatMatch
}
const cellsAreAdjacent = (cellA, cellB) => Math.max(Math.abs(cellA.x - cellB.x), Math.abs(cellA.y - cellB.y)) === 1;
const cellIsInRegion = (cellToCheck, region) => region.some((cell) => cellsAreAdjacent(cell, cellToCheck));
const findRegionCellIsIn = (cellToCheck, regions) => regions.find(region => cellIsInRegion(cellToCheck, region));
const findRegions = (cells) => {
const regions = []
while (cells.length > 0) {
let currentCell = cells.pop();
//1. Check remainig cells to see if they are adjacent and not already in a group
const adjacentCellsNotAlreadyInRegion = cells.map(cell => ccellsAreAdjacent(cell, currentCell))
/* let regionToPutCell = findRegionCellIsIn(currentCell, regions);
if (regionToPutCell) {
regionToPutCell.push(currentCell);
} else {
regions.push([currentCell]);
} */
if (currentRegion.length === 0 || cellIsInRegion(currentCell, currentRegion)) {
currentRegion.push(currentCell)
} else {
regions.push(currentRegion)
currentRegion = [currentCell]
}
}
return regions
}
const cellsAboveThreshold = findCellAboveThreshold(input, 200);
/*...