// Two Paintings 2 // September 14, 2017 -- John Clavin var society_1; var society_2; var numOfAttractors = 5; var move = false; function setup() { createCanvas(1080, 580); background(246); frameRate(60); society_1 = new Society(); society_2 = new Society(); for (var i = 0; i < numOfAttractors; i++) { var attractor = new Attractor(); society_1.addAttractor(attractor); } for (i = 0; i < numOfAttractors; i++) { attractor = new Attractor(); society_2.addAttractor(attractor); } for (i = 0; i < 460; i++) { var aChoice = floor(random(numOfAttractors)); var pAttractor = society_1.attractors[aChoice]; var person = new Person(pAttractor); society_1.addPerson(person); } for (i = 0; i < 460; i++) { aChoice = floor(random(numOfAttractors)); pAttractor = society_2.attractors[aChoice]; person = new Person(pAttractor); society_2.addPerson(person); } } function draw() { society_1.runSociety(); translate(500, 0); society_2.runSociety(); if (frameCount > 1000) { move = true; } if (frameCount > 3600) { noLoop(); } } function Society() { this.people = []; this.attractors = []; this.addPerson = function(a) { this.people.push(a); } this.addAttractor = function(a) { this.attractors.push(a); } this.runSociety = function() { for (var i = 0; i < this.people.length; i++) { this.people[i].runPeople(); } } } function Person(a) { this.position = createVector(random(100, 480), random(100, 480)); this.velocity = createVector(0, 0); this.attractor = a; this.runPeople = function() { this.setColor(); this.render(); this.calcAttraction(); this.spreadInfection(); } this.calcAttraction = function() { if (move === true) { var force = p5.Vector.sub(this.attractor.position, this.position); force.normalize(); this.velocity = force.mult(0.2); } } this.spreadInfection = function() { this.x_decision = int(random(3, 6)); if (this.x_decision == 3) { this.spreadto_x = -1; } else if (this.x_decision == 4) { this.spreadto_x = 0; } else { this.spreadto_x = 1; } this.y_decision = int(random(3, 6)); if (this.y_decision == 3) { this.spreadto_y = -1; } else if (this.y_decision == 4) { this.spreadto_y = 0; } else { this.spreadto_y = 1; } this.spreadto_x += this.position.x; this.spreadto_y += this.position.y; this.position.x = this.spreadto_x; this.position.y = this.spreadto_y; this.position.add(this.velocity); } this.setColor = function() { stroke(this.attractor.aColor); } this.render = function() { point(this.position.x, this.position.y); } } function Attractor() { this.position = createVector(random(100, 480), random(100, 480)); this.aColor = color(random(240), random(200), random(200)); }