xxxxxxxxxx
84
var cellWidth = 10;
var N;
var cellHeight = 2;
var offset;
var deltaH = 40;
var pruneThreshold = 30;
var frame = 0;
let heights = [];
function setup() {
createCanvas(500, 500);
background(0);
N = width/cellWidth;
offset = width-100;
for (i=0; i<N; i++) {
heights[i] = 0;
}
//heights[N/2]=40;
}
function draw() {
background(0);
stroke(255);
strokeWeight(2);
frameRate(5);
// the slow rate seems to be needed for the file saving to work right.
// 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();
prune();
if (frame < 1500) {
print(nf(frame,4));
saveCanvas(nf(frame,4),'png');
}
frame += 1;
}
function grow() {
// here's the growing process
x = floor(random()*N);
if (random()<0.1) {
heights[x]-=deltaH;
//heights[x-1]+=deltaH/4;
//heights[x+1]+=deltaH/4;
}
}
function prune() {
// average things out with some time
//print(heights[height/2]);
for (i = 0; i< N; i++) {
heights[i]+=(heights[min(N-1,i+1)]+heights[max(0,i-1)]-2*heights[i])/(2*20);
}
}
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);
}
}