4 directions floodfill for 2d array
With fast algorithm – west to east with queue – and some statistics returned
npm i jeromedecoster/floodfill
Start from a coordinate
var floodfill = require('floodfill')
var array = [
[1,1,1,1,1,1,1,1,1],
[1,0,0,0,1,0,0,0,1],
[1,0,0,0,1,0,0,0,1],
[1,0,0,1,0,0,0,0,1],
[1,1,1,0,0,0,1,1,1],
[1,0,0,0,0,1,0,0,1],
[1,0,0,0,1,0,0,0,1],
[1,0,0,0,1,0,0,0,1],
[1,1,1,1,1,1,1,1,1]
]
// {count: 23, x: 1, y: 1, width: 7, height: 7, area: 49}
var ret = floodfill(array, 4, 4, 0, 2)
/*
[
[1,1,1,1,1,1,1,1,1],
[1,0,0,0,1,2,2,2,1],
[1,0,0,0,1,2,2,2,1],
[1,0,0,1,2,2,2,2,1],
[1,1,1,2,2,2,1,1,1],
[1,2,2,2,2,1,0,0,1],
[1,2,2,2,1,0,0,0,1],
[1,2,2,2,1,0,0,0,1],
[1,1,1,1,1,1,1,1,1]
]
*/
console.log(array)
Scan all the array
var floodfill = require('floodfill')
var array = [
[1,1,1,1,1,1,1,1,1],
[1,0,0,0,1,0,0,0,1],
[1,0,0,0,1,0,0,0,1],
[1,0,0,1,0,0,0,0,1],
[1,1,1,0,0,0,1,1,1],
[1,0,0,0,0,1,0,0,1],
[1,0,0,0,1,0,0,0,1],
[1,0,0,0,1,0,0,0,1],
[1,1,1,1,1,1,1,1,1]
]
/*
[
{count: 8, x: 1, y: 1, width: 3, height: 3, area: 9},
{count: 23, x: 1, y: 1, width: 7, height: 7, area: 49},
{count: 8, x: 5, y: 5, width: 3, height: 3, area: 9}
]
*/
var ret = floodfill.all(array, 0, 2)
/*
[
[1,1,1,1,1,1,1,1,1],
[1,2,2,2,1,2,2,2,1],
[1,2,2,2,1,2,2,2,1],
[1,2,2,1,2,2,2,2,1],
[1,1,1,2,2,2,1,1,1],
[1,2,2,2,2,1,2,2,1],
[1,2,2,2,1,2,2,2,1],
[1,2,2,2,1,2,2,2,1],
[1,1,1,1,1,1,1,1,1]
]
*/
console.log(array)
Mainly forked / inspired on floodfill and wikipedia
MIT