// Gravitational Painting 3 John Clavin - June 20, 2016 var system_1; var system_2; var nLines = 96; function setup() { createCanvas(1060, 540); background(140); fill(243); stroke(110); strokeWeight(3); rect(20, 20, 500, 500); rect(540, 20, 500, 500); system_1 = new System(); system_2 = new System(); for (var i = 0; i < nLines; i++) { var b = new Brush(); system_1.addBrush(b) } for (var j = 0; j < nLines; j++) { system_1.assignBrushFriend(j, int(random(20, 22))); } for (var k = 0; k < nLines; k++) { b = new Brush(); system_2.addBrush(b) } for (var l = 0; l < nLines; l++) { system_2.assignBrushFriend(l, int(random(20, 22))); } } function draw() { translate(20, 20); system_1.runSystem(); translate(520, 0); system_2.runSystem(); if (frameCount > 460) { translate(-540, -20); noFill(); stroke(110); strokeWeight(4); rect(20, 20, 500, 500); rect(540, 20, 500, 500); noLoop(); } } var System = function() { this.brushes = []; this.addBrush = function(b) { this.brushes.push(b); } this.assignBrushFriend = function(n, f) { this.brushes[n].friend = f; if(f == 20) { this.brushes[n].color = color(200, 0, 0, 112); } else { this.brushes[n].color = color(30, 112); } } this.runSystem = function() { for (var i = 0; i < this.brushes.length; i++) { var bFriend = this.brushes[i].friend; var force = this.brushes[i].calculateAttraction(this.brushes[bFriend]); force.mult(1.8); this.brushes[i].applyForce(force); this.brushes[i].update(); this.brushes[i].checkEdges(); this.brushes[i].display(); } } } var Brush = function() { this.color; var choice = int(random(4)); if (choice === 0) { this.x = random(500); this.y = 0; } else if(choice == 1) { this.x = random(500); this.y = 500; } else if(choice == 2) { this.x = 0; this.y = random(500); } else { this.x = 500; this.y = random(500); } this.pos = createVector(this.x, this.y); this.vel = createVector(0, 0); this.acc = createVector(0, 0); this.friend = 0; this.edgeFlag = false; this.calculateAttraction = function(p) { var force = p5.Vector.sub(p.pos, this.pos); force.normalize(); return force; } this.applyForce = function(force) { this.acc.add(force); } this.update = function() { this.vel.add(this.acc); this.pos.add(this.vel); this.acc.mult(0); this.vel.mult(0); } this.checkEdges = function() { if( this.pos.x < -1 || this.pos.x > 501) { this.edgeFlag = true; } if( this.pos.y < -1 || this.pos.y > 501) { this.edgeFlag = true; } } this.display = function() { if(!this.edgeFlag) { strokeWeight(3); stroke(this.color); point(this.pos.x, this.pos.y); } } }