jeromedecoster / floodfill

4 directions floodfill for 2d array

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

floodfill

4 directions floodfill for 2d array

With fast algorithm – west to east with queue – and some statistics returned

Install

npm i jeromedecoster/floodfill

API

floodfill(array, x, y, search, replace)

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)

floodfill.all(array, search, replace)

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)

Thanks

Mainly forked / inspired on floodfill and wikipedia

License

MIT

About

4 directions floodfill for 2d array


Languages

Language:JavaScript 100.0%