-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcreateVoronoiDiagram.js
More file actions
131 lines (100 loc) · 2.85 KB
/
Copy pathcreateVoronoiDiagram.js
File metadata and controls
131 lines (100 loc) · 2.85 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Delaunay } from "d3-delaunay";
import { polygonCentroid } from "d3";
import { distToSegment } from "./distToSegment";
const defaultOpts = {
width: 1024,
height: 1024,
points: [],
relaxIterations: 8,
};
function createVoronoiDiagram(opts) {
opts = Object.assign({}, defaultOpts, opts);
opts.points = opts.points.map((point) => [point.x, point.y]);
const delaunay = Delaunay.from(opts.points);
const voronoi = delaunay.voronoi([0, 0, opts.width, opts.height]);
const diagramPoints = [];
for (let k = 0; k < opts.relaxIterations; k++) {
for (let i = 0; i < delaunay.points.length; i += 2) {
const cell = voronoi.cellPolygon(i >> 1);
if (cell === null) continue;
const x0 = delaunay.points[i];
const y0 = delaunay.points[i + 1];
const [x1, y1] = polygonCentroid(cell);
delaunay.points[i] = x0 + (x1 - x0) * 1;
delaunay.points[i + 1] = y0 + (y1 - y0) * 1;
}
voronoi.update();
}
for (let i = 0; i < delaunay.points.length; i += 2) {
const x = delaunay.points[i];
const y = delaunay.points[i + 1];
diagramPoints.push({
x,
y,
});
}
let cells = [];
for (let i = 0; i < delaunay.points.length; i += 2) {
const cell = voronoi.cellPolygon(i >> 1);
if (cell === null) continue;
cells.push({
...formatCell(cell),
neighbors: [...voronoi.neighbors(i)].map((index) => {
return {
...formatCell(voronoi.cellPolygon(index)),
};
}),
});
}
return {
cells: cells.map((cell, index) => {
const neighbors = [...voronoi.neighbors(index)];
cell.neighbors = neighbors.map((index) => cells[index]);
return cell;
}),
points: diagramPoints,
};
}
function formatCell(points) {
return {
points,
innerCircleRadius: getClosestEdgeToCentroid(points),
centroid: {
x: polygonCentroid(points)[0],
y: polygonCentroid(points)[1],
},
};
}
function getClosestEdgeToCentroid(points) {
const centroid = polygonCentroid(points);
const pointsSorted = sortPointsByAngle(centroid, points);
let closest = distToSegment(centroid, pointsSorted[0], pointsSorted[1]);
for (let i = 1; i < points.length - 1; i++) {
if (points[i + 1]) {
const dist = distToSegment(
centroid,
pointsSorted[i],
pointsSorted[i + 1]
);
if (dist < closest) {
closest = dist;
}
}
}
return closest;
}
function sortPointsByAngle(centroid, points) {
const centerPoint = centroid;
const sorted = points.slice(0);
const sortByAngle = (p1, p2) => {
return (
(Math.atan2(p1[1] - centerPoint[1], p1[0] - centerPoint[0]) * 180) /
Math.PI -
(Math.atan2(p2[1] - centerPoint[1], p2[0] - centerPoint[0]) * 180) /
Math.PI
);
};
sorted.sort(sortByAngle);
return sorted;
}
export { createVoronoiDiagram };