xxxxxxxxxx
90
let nodes = [];
let contagious = [];
let population = 5;
let seed = 0.2;
let R = 2.5;
let diameter = 10;
function setup() {
createCanvas(700, 400);
// Create objects
for (let i = 0; i < population; i++) {
let n = new Node();
n.infectious = getsVirus(); // randomly infect some nodes
nodes.push(n);
// if (n.infectious) {
// contagious.push(n);
// }
}
frameRate(10);
// pg = createGraphics(700, 400);
}
function draw() {
background(40, 30, 40);
for (let i = 0; i < nodes.length; i++) {
nodes[i].display();
let n = nodes[i];
if (n.infectious) {
n.findNeighbours(nodes);
}
//nodes[i].spreadVirus();
}
}
// Node class
class Node {
constructor(infectious) {
this.x = random(width);
this.y = random(height);
this.diameter = diameter;
this.infectious = false;
this.neighbours = [];
this.name = '_' + random().toString(36).substr(2, 9);
}
findNeighbours(a) {
let d = diameter * 3;
// let neighbours = a.filter(function(n) {
// return dist(this.x, this.y, n.x, n.y) < d
// })
let neighbours = a.filter(function(n) {
return !n.infectious;
})
//this.neighbours = neighbours;
for (let i = 0; i < neighbours.length; i++) {
print(neighbours[i].name);
}
}
spreadVirus() {
findNeigbours(nodes);
print('...spread! ' + this.neighbours);
// let affected = int(randomGaussian(R));
// let n = max(affected, this.neighbours.length);
// for (let i = 0; i < n; i++) {
// this.neighbours[i].infectious = true;
// }
}
display() {
circle(this.x, this.y, this.diameter);
if (this.infectious) {
fill(255, 0, 20);
// let d = this.diameter * 2;
// pg.noFill();
// pg.circle(this.x, this.y, d);
} else {
fill(255);
}
}
}
function getsVirus() {
let prob = random();
if (prob < seed) {
return true;
}
return false;
}