-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
69 lines (60 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module.exports = fill
function fill(grid, x, y, filler) {
var height = grid.shape[1]
var width = grid.shape[0]
var data = grid.data
var empty = grid.get(x, y)
var queuex = [x]
var queuey = [y]
var curry, currx
var minx = x
var miny = y
var maxx = x
var maxy = y
var area = 0
var verty
var north
var south
var n
while (queuey.length) {
currx = queuex.pop()
curry = queuey.pop()
minx = currx < minx ? currx : minx
maxx = currx > maxx ? currx : maxx
row = currx*height
if (grid.get(currx, curry) === empty) {
north = south = curry
do {
north -= 1
} while (
grid.get(currx, north) === empty &&
north >= 0
)
do {
south += 1
} while (
grid.get(currx, south) === empty &&
south < height
)
miny = north+1 < miny ? north+1 : miny
maxy = south-1 > maxy ? south-1 : maxy
for (n = north + 1; n < south; n += 1) {
grid.set(currx, n, filler)
area += 1
if (grid.get(currx - 1, n) === empty) {
queuex.push(currx - 1)
queuey.push(n)
}
if (grid.get(currx + 1, n) === empty) {
queuex.push(currx + 1)
queuey.push(n)
}
}
}
}
return {
lo: [minx, miny]
, hi: [maxx, maxy]
, area: area
}
}