G = {
new_graph;
let N = Math.round(1000 * s);
let R = 4000;
let seed = 0.1234;
seed = Math.random();
let random_source_x = d3.randomLcg(seed);
let random_source_y = d3.randomLcg(seed / 2);
let random_x = d3.randomUniform.source(random_source_x)(0, w);
let random_y = d3.randomUniform.source(random_source_y)(0, h);
let pts = d3.range(N).map(() => [random_x(), random_y()]);
let nodes = pts.map((p, i) => ({
x: p[0],
y: p[1],
index: i,
cy_data: { id: `n${i}` }
}));
let links = nodes
.map((node, i) => nodes.slice(i + 1).filter((n) => dist2(node, n) < R))
.map((a, i) =>
a.map((o) => ({
source: nodes[i],
target: nodes[o.index],
length2: Math.sqrt(dist2(nodes[i], nodes[o.index])),
cy_data: { id: `n${i}--n${o.index}` }
}))
)
.flat();
nodes.forEach(
(o) =>
(o.valence = links.filter(
(l) => l.source.index == o.index || l.target.index == o.index
).length)
);
let max_valence = d3.max(nodes.map((o) => o.valence));
let root_node = nodes.filter((o) => o.valence == max_valence)[0];
let G = { nodes, links, max_valence, root_node };
return G;
function dist2(o1, o2) {
return (o1.x - o2.x) ** 2 + (o1.y - o2.y) ** 2;
}
}