-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-voronoi.js
48 lines (42 loc) · 1.28 KB
/
utils-voronoi.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
import { Delaunay } from "d3-delaunay";
import { pointInBMPMask } from "./utils.js"
export function voronoiPolysFromPointsAndMask(points, width, height, margin, maskFunc, dela)
{
const maskedPoints = points.filter((entry) => {
return maskFunc(entry);
});
let polyGen;
const delaunay = Delaunay.from(maskedPoints);
if (dela)
{
polyGen = delaunay.trianglePolygons();
}
else
{
const voronoi = delaunay.voronoi([margin, margin, width - margin, height - margin]);
polyGen = voronoi.cellPolygons();
}
const polys = [];
const partiallyInsidePolys = [];
const fullyOutsidePolys = [];
while (true)
{
const poly = polyGen.next();
if (poly.done) break;
let fullyInside = true;
let partiallyInside = false;
for (let i = 0; i < poly.value.length; ++i)
{
if (maskFunc(poly.value[i])) partiallyInside = true;
else fullyInside = false;
}
if (fullyInside) polys.push(poly.value);
else if (partiallyInside) partiallyInsidePolys.push(poly.value);
else fullyOutsidePolys.push(poly.value);
}
return {
fullyInside: polys,
partiallyInside: partiallyInsidePolys,
fullyOutside: fullyOutsidePolys
};
}