hexagons = {
let cells = new Set(japanHexagons);
function includedNeighbors(origin) {
const neighbors = h3.kRing(origin, kRingSize)
return neighbors.reduce(
(count, n) => count += cells.has(n) ? 1 : 0, 0
);
}
function dilate() {
for (const cell of cells) {
const neighbors = h3.kRing(cell, 1);
for (const neighbor of neighbors.filter(n => !cells.has(n))) {
if (includedNeighbors(neighbor) >= dThreshold) {
cells.add(neighbor);
}
}
}
}
function erode() {
for (const cell of cells) {
if (includedNeighbors(cell) <= eThreshold) {
cells.delete(cell);
}
}
}
for (let i = 0; i < passes; i++) {
if (firstOp === 'dilate, then erode') {
dilate();
erode();
} else {
erode();
dilate();
}
}
return Array.from(cells);
}