xxxxxxxxxx
53
let nodes = [];
let numberOfNodes = 5;
let canvasWidth = 400;
let canvasHeight = 400;
function setup() {
createCanvas(canvasWidth, canvasHeight);
background(220);
for (i = 0; i < numberOfNodes; i ++){
randomX = random(canvasWidth);
randomY = random(canvasHeight);
nodes.push(new node(randomX, randomY));
}
for (i = 0; i < nodes.length; i ++){
//circle(nodes[i].x, nodes[i].y, 10);
text(i, nodes[i].x, nodes[i].y);
}
for (i = 0; i < nodes.length; i ++)
{
let sumOfDistances = 0;
for (j = 0; j < nodes.length; j ++){
if (i != j){
let run = nodes[j].x - nodes[i].x;
let rise = nodes[j].y - nodes[i].y;
let distance = sqrt(pow(run, 2) + pow(rise, 2));
sumOfDistances += distance;
}
}
node[i].averageDistance = sumOfDistances / (nodes.length - 1);
}
}
function draw() {
}
class node{
constructor(x, y){
this.x = x;
this.y = y;
this.averageDistance = null;
}
}