-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfloodfill.js
40 lines (33 loc) · 882 Bytes
/
floodfill.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
module.exports = function(boundsArray, minX, maxX, minY, maxY, landId) {
function floodFillMap(x, y) {
if (x < minX || x > maxX || y < minY || y > maxY) {
return;
}
for (var i = 0; i < boundsArray.length; i++) {
if (boundsArray[i].x == x && boundsArray[i].y == y) {
if (boundsArray[i].id == landId || boundsArray[i].id == -1) {
return;
} else {
boundsArray[i].id = -1;
}
}
}
floodFillMap(x + 1, y);
floodFillMap(x - 1, y);
floodFillMap(x, y + 1);
floodFillMap(x, y - 1);
}
floodFillMap(minX, minY);
var inside = [];
var outside = [];
for (var i = 0; i < boundsArray.length; i++) {
if (boundsArray[i].id == -1) {
boundsArray[i].id = 0; //This line right here drove me insane for months
outside.push(boundsArray[i]);
} else {
boundsArray[i].id = landId;
inside.push(boundsArray[i]);
}
}
return inside;
};