// Conflict -- The Shape of Conflict // by John Clavin -- Aug 8, 2016 var restartFlag; var mCounter; var chain; function setup() { createCanvas(640, 640); frameRate(40); background(235); restartFlag = false; mCounter = 0; chain = new Chain(); } function draw() { mCounter++; chain.runChain(); if(mCounter > 1120) { restartFlag = true; noLoop(); } } function mousePressed() { if(restartFlag === true) { chain.resetChain(); restartFlag = false; loop(); } return false; } Chain = function() { this.chainLinks_L = []; this.chainLinks_R = []; this.waveOffset_L = random(62); this.waveOffset_R = this.waveOffset_L + random(-32, 32); this.waveScale_L = random(.015, 0.06); this.waveScale_R = random(.015, 0.06); var countPlus; this.resetChain = function() { background(235); mCounter = 0; chain = new Chain(); } var linkL = new Link_L(createVector( - 20, height)); this.chainLinks_L.push(linkL); for (var i = 0; i < this.chainLinks_L.length; i++) { if(this.chainLinks_L.length < 348) { this.chainLinks_L.push(this.chainLinks_L[i].addLink()); } } for (var j = 0; j < this.chainLinks_L.length; j++) { countPlus = (j + this.waveOffset_L) * this.waveScale_L; this.chainLinks_L[j].wave(countPlus); } var linkR = new Link_R(createVector( width + 20, height)); this.chainLinks_R.push(linkR); for (var k = 0; k < this.chainLinks_R.length; k++) { if(this.chainLinks_R.length < 348) { this.chainLinks_R.push(this.chainLinks_R[k].addLink()); } } for (var m = 0; m < this.chainLinks_R.length; m++) { countPlus = (m + this.waveOffset_R) * this.waveScale_R; this.chainLinks_R[m].wave(countPlus); } this.runChain = function() { for (var i = 0; i < this.chainLinks_L.length; i++) { var xLeft = this.chainLinks_L[i].position.x; var xRight = this.chainLinks_R[i].position.x if(xLeft + 16 > xRight - 16) { this.chainLinks_L[i].colorFlag = false; this.chainLinks_R[i].colorFlag = false; } else { this.chainLinks_L[i].colorFlag = true; this.chainLinks_R[i].colorFlag = true; } } for (var j = 0; j < this.chainLinks_L.length; j++) { this.chainLinks_L[j].update(); this.chainLinks_L[j].render(); } for (var k = 0; k < this.chainLinks_R.length; k++) { this.chainLinks_R[k].update(); this.chainLinks_R[k].render(); } } } function Link_L(pos) { this.randColor = color(random(240), random(100), random(100)); this.colorFlag; this.position = pos; this.cirX = 0; this.cirY = 0; this.waveAmount; this.waveForce = createVector(0, 0); this.randomVector = createVector(0, 0); this.moveForce = createVector(0, 0); this.newPosition = createVector(0, 0); this.counter = 0; this.update = function() { this.counter++; this.randomVector = p5.Vector.random2D(); this.randomVector.normalize(); this.randomVector.limit(0.80); this.cirX = cos(this.randomVector.heading()) * 20 + this.position.x; this.cirY = sin(this.randomVector.heading()) * 20 + this.position.y; this.moveForce.x = 1.0; this.moveForce.y = 0; this.moveForce.normalize(); this.moveForce.mult(0.70); this.waveForce.x = this.waveAmount; this.waveForce.y = 0; this.waveForce.mult(0.13); this.moveForce.add(this.waveForce); this.moveForce.limit(0.80); this.position.add(this.randomVector); this.position.add(this.moveForce); } this.wave = function(count) { var w = count; var ww = sin(w); this.waveAmount = ww; } this.addLink = function() { var linkDistance = createVector(2, 0); linkDistance.setMag(2); linkDistance.rotate(4.7124); this.newPosition = linkDistance.add(this.position); return new Link_L(this.newPosition); } this.render = function(colorFlag) { strokeWeight(1); if(this.colorFlag === true) { stroke(this.randColor); } else { if(random(100) > 4) { stroke(235); } else { stroke(0); } } line(this.position.x, this.position.y, this.cirX, this.cirY); } } function Link_R(pos) { this.randColor = color(random(100), random(100), random(240)); this.colorFlag; this.position = pos; this.cirX = 0; this.cirY = 0; this.waveAmount = 0; this.waveForce = createVector(0, 0); this.randomVector = createVector(0, 0); this.moveForce = createVector(0, 0); this.newPosition = createVector(0, 0); this.counter = 0; this.update = function() { this.counter++; this.randomVector = p5.Vector.random2D(); this.randomVector.normalize(); this.randomVector.limit(0.80); this.cirX = cos(this.randomVector.heading()) * 20 + this.position.x; this.cirY = sin(this.randomVector.heading()) * 20 + this.position.y; this.moveForce.x = -1.0; this.moveForce.y = 0; this.moveForce.normalize(); this.moveForce.mult(0.70); this.waveForce.x = this.waveAmount; this.waveForce.y = 0; this.waveForce.mult(0.13); this.moveForce.add(this.waveForce); this.moveForce.limit(0.80); this.position.add(this.randomVector); this.position.add(this.moveForce); } this.wave = function(count) { var w = count; var ww = sin(w); this.waveAmount = ww; } this.addLink = function() { var linkDistance = createVector(2, 0); linkDistance.setMag(2); linkDistance.rotate(4.7124); this.newPosition = linkDistance.add(this.position); return new Link_R(this.newPosition); } this.render = function(colorFlag) { strokeWeight(1); if(this.colorFlag === true) { stroke(this.randColor); } else { if(random(100) > 4) { stroke(235); } else { stroke(0); } } line(this.position.x, this.position.y, this.cirX, this.cirY); } }