Created
November 22, 2018 04:16
-
-
Save gonzamoiguer/bf97d8f50148d8e26207c7b7b09c0c72 to your computer and use it in GitHub Desktop.
Revisions
-
gonzamoiguer created this gist
Nov 22, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,135 @@ // Based on Shiffman's Reaction Diffusion Tut: https://youtu.be/Cl_Gjj80gPE // // Changed to simulate once, on startup // Then draw the lines of the resulting chains // Will need to update vector to Victor.js in order to use with plotter var tree = []; var walkers = []; var maxWalkers = 40; var iterations = 1000; var radius = 8; var hu = 0; var shrink = 0.995; var stuckCount = 0; var drawingComplete = false; function setup() { createCanvas(400, 400); tree[0] = new Walker(width / 2, height / 2); tree[0].isRoot = true; radius *= shrink; for (var i = 0; i < maxWalkers; i++) { walkers[i] = new Walker(); radius *= shrink; } while(!drawingComplete){ for (var n = 0; n < iterations; n++) { for (var i = walkers.length - 1; i >= 0; i--) { walkers[i].walk(); if (walkers[i].checkStuck(tree)) { stuckCount++; if(stuckCount == 414){ // por que 414 ?? drawingComplete = true; } walkers[i].setHue(hu % 360); hu += 2; tree.push(walkers[i]); walkers.splice(i, 1); } } } //var r = walkers[walkers.length - 1].r; while (walkers.length < maxWalkers && radius > 1) { radius *= shrink; walkers.push(new Walker()); } } // while !drawingComplete noFill(); stroke(0); for(let i=0; i < tree.length; i++){ DrawBranch(tree[i]); } } function DrawBranch(seed){ if(!seed.linkedTo) return; line(seed.pos.x, seed.pos.y, seed.linkedTo.pos.x, seed.linkedTo.pos.y); DrawBranch(seed.linkedTo); } // Walker Object function Walker(x, y) { this.isRoot = false; this.linkedTo = false; if (arguments.length == 2) { this.pos = createVector(x, y); this.stuck = true; } else { this.pos = randomPoint(); this.stuck = false; } this.r = radius; this.walk = function() { var vel = p5.Vector.random2D(); // var vel = createVector(random(-1, 1), random(-0.5, 1)); this.pos.add(vel); this.pos.x = constrain(this.pos.x, 0, width); this.pos.y = constrain(this.pos.y, 0, height); } this.checkStuck = function(others) { for (var i = 0; i < others.length; i++) { var d = distSq(this.pos, others[i].pos); if (d < (this.r * this.r + others[i].r * others[i].r + 2 * others[i].r * this.r)) { //if (random(1) < 0.1) { this.linkedTo = others[i]; this.stuck = true; return true; break; //} } } return false; } } function randomPoint() { var i = floor(random(4)); if (i === 0) { var x = random(width); return createVector(x, 0); } else if (i === 1) { var x = random(width); return createVector(x, height); } else if (i === 2) { var y = random(height); return createVector(0, y); } else { var y = random(height); return createVector(width, y); } } function distSq(a, b) { var dx = b.x - a.x; var dy = b.y - a.y; return dx * dx + dy * dy; }