xxxxxxxxxx
82
var cellWidth = 10;
var N;
var cellHeight = 2;
var offset;
var deltaH = 5;
var pruneThreshold = 30;
let heights = [];
function setup() {
createCanvas(500, 500);
background(0);
N = width/cellWidth;
offset = width/2;
for (i=0; i<N; i++) {
heights[i] = 0;
}
}
function draw() {
background(0);
stroke(255);
strokeWeight(2);
// now loop through
for (i=0; i<N; i++) {
line(i*cellWidth, offset+(heights[i]*cellHeight),(i+1)*cellWidth,offset+(heights[i]*cellHeight)); // horizontal line
if (i>=1)
line(i*cellWidth,offset+(heights[i-1]*cellHeight),i*cellWidth,offset+(heights[i]*cellHeight)) // vertical line
}
grow();
pruneNew();
}
function grow() {
// here's the growing process
x = floor(random()*N);
heights[x]-=deltaH;
if (x>0)
heights[x-1]+=deltaH/4;
if (x<N)
heights[x+1]+=deltaH/4;
}
function prune() {
// look for high gradients and prune them with some probability
for (i=1; i<N; i++) {
/*if (abs(heights[i]-heights[i-1])>pruneThreshold) {
if (random() < 0.5) {
heights[i] = (2*heights[i]+heights[i-1])/3;
heights[i-1] = (heights[i]+2*heights[i-1])/3;
}
}*/
if (abs(heights[i])>(pruneThreshold)) {
if (random() < 0.5) {
heights[i] = 0
}
}
}
}
function pruneNew() {
pruneWidth = 10;
if (random() < 0.1) {
// randomly choose the center of the pruning.
print("Prune");
x = floor(random()*N);
for (i = max(x,0); i<min(x+pruneWidth,N); i++) {
heights[i]=0;
}
stroke(255,0,0);
//line(i*cellWidth,offset,(i+pruneWidth)*cellWidth,offset);
}
}