-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed-fill.js
45 lines (39 loc) · 1.08 KB
/
seed-fill.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
const {
pointsToEdges, setPixel, getPixel, getYMin, getYMax, lerp
} = require('./util');
// Seed Fill
module.exports = function seedFill(points, img, seed) {
if(points.length < 3) return;
// construct edge list
const edges = pointsToEdges(points);
// verify seed
if(!isPointInPolygon(seed, edges)) throw new Error('seed not in polygon');
// initialize stack
const stack = [seed];
while(stack.length > 0) {
const [x, y] = stack.pop();
setPixel(img, x, y);
// visit neighbors
for(let h = -1; h <= 1; h++) {
for(let k = -1; k <= 1; k++) {
if(h != 0 && k != 0 || h == 0 && k == 0) continue;
const point = [x + h, y + k];
// neighbor is in polygon
if(!getPixel(img, ...point) && isPointInPolygon(point, edges)) {
stack.push(point);
}
}
}
}
};
// odd-even rule
function isPointInPolygon([x, y], edges) {
let isIn = false;
for(const edge of edges) {
if(y >= getYMin(edge) && y < getYMax(edge)) {
// ray crosses edge
if(lerp(y, edge) >= x) isIn = !isIn;
}
}
return isIn;
}