circles = {
refresh;
const circles = [],
rndR = d3.randomUniform(0.1, 0.3),
rndP = d3.randomUniform(-1.0, 1.0);
for (let i = 0; i < numCircles; ++i) {
circles.push({ i, x: rndP(), y: rndP(), r: rndR() });
}
if (numCircles > 10) {
const graph = {
nodes: circles.map((d) => Object.assign({}, d)),
links: []
};
circles.map((c, src) => {
const others = circles.filter((d) => d.i !== c.i),
{ r } = c;
others.map((d, tar) => {
graph.links.push({ source: src, target: tar, r });
});
});
circles.graph = graph;
const tick = () => {
const scaleX = d3
.scaleLinear()
.domain(d3.extent(graph.nodes, (d) => d.x))
.range([-1, 1]),
scaleY = d3
.scaleLinear()
.domain(d3.extent(graph.nodes, (d) => d.y))
.range([-1, 1]);
circles.map((c, i) => {
c.x = scaleX(graph.nodes[i].x);
c.y = scaleY(graph.nodes[i].y);
});
};
const simulation = d3
.forceSimulation()
.nodes(graph.nodes)
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(0, 0))
.force(
"collide",
d3.forceCollide((d) => d.r)
)
.on("tick", tick);
}
return circles;
}