// Infectious Spread -- by John Clavin -- July 22, 2016 var restartFlag; var counter; var society; function setup() { createCanvas(640, 680); background(235); restartFlag = false; counter = 0; society = new Society(); } function draw() { counter++; society.runSociety(); if(counter > 650) { restartFlag = true; noLoop(); } } function mousePressed() { if(restartFlag === true) { society.resetSociety(); restartFlag = false; loop(); } return false; } Society = function() { this.people = []; var virusColor = color(0); var p = new Person(createVector(width/2, height + 6)); this.people.push(p); this.resetSociety = function() { background(235); counter = 0; this.people = []; var pp = new Person(createVector(width/2, height + 6)); this.people.push(pp); } this.runSociety = function() { var virusColor1 = color(random(200), random(100), random(20), 120); var virusColor2 = color(random(20), random(200), random(100), 120); var virusColor3 = color(random(20), random(100), random(200), 120); var virusColor4 = color(random(160), random(160), random(160), 120); if((counter % 61) == 1) { virusColor = virusColor1; } else if((counter % 61) == 16) { virusColor = virusColor2; } else if((counter % 61) == 31) { virusColor = virusColor3; } else if((counter % 61) == 46) { virusColor = virusColor4; } for (var i = 0; i < this.people.length; i++) { this.people[i].pointColor = virusColor; if(this.people[i].aliveFlag === true) { this.people[i].update(); this.people[i].render(); if(this.people[i].timeToSpread()) { if(this.people.length < 2048) { this.people.push(this.people[i].spreadInfection()); this.people.push(this.people[i].spreadInfection()); } } } else { this.people.splice(i, 1); } } } } function Person(pos) { this.pointColor; this.position = pos; this.repelLocation = createVector(width/2, height + 24); this.attractLocation = createVector(width/2, 0); this.randomForce = createVector(0, 0); this.repelForce = createVector(0, 0); this.attractForce = createVector(0, 0); this.counter = 0; this.aliveFlag = true; this.update = function() { this.randomForce.x = random(-1, 1); this.randomForce.y = random(-0.3, 0.3); this.randomForce.normalize(); var randomF = this.randomForce.mult(3.2); this.repelForce = p5.Vector.sub(this.position, this.repelLocation); this.repelForce.normalize(); var repelF = this.repelForce.mult(1.2); this.attractForce = p5.Vector.sub(this.attractLocation, this.position); this.attractForce.normalize(); var attractF = this.attractForce.mult(1); this.position.add(randomF); if(counter < 120) { this.position.add(attractF); } else { this.position.add(repelF); } } this.spreadInfection = function() { return new Person(createVector(this.position.x, this.position.y)); } this.timeToSpread = function() { this.counter++; if(this.counter == 16) { this.aliveFlag = false; return true; } else { return false; } } this.render = function() { strokeWeight(3); stroke(this.pointColor); point(this.position.x, this.position.y); } }