SocialNetworks_duan/src/utils/generateNode.js

31 lines
734 B
JavaScript
Raw Normal View History

2025-07-17 10:28:56 +08:00
export function generateNonOverlappingNodes(count, width, height, minDist) {
const nodes = [];
let tryCount = 0;
for (let i = 0; i < count; i++) {
let valid = false;
let x, y;
while (!valid && tryCount < 1000) {
x = Math.random() * (width - minDist) + minDist / 2;
y = Math.random() * (height - minDist) + minDist / 2;
valid = true;
for (const node of nodes) {
const dx = node.x - x;
const dy = node.y - y;
if (Math.sqrt(dx * dx + dy * dy) < minDist) {
valid = false;
break;
}
}
tryCount++;
}
nodes.push({
id: `user_${i}`,
name: `User ${i}`,
x,
y,
category: 1
});
}
return nodes;
}