RosettaCodeData/Task/Brownian-tree/Zkl/brownian-tree.zkl
2017-09-25 22:28:19 +02:00

33 lines
1.2 KiB
Text

w:=h:=400; numParticles:=20000;
bitmap:=PPM(w+2,h+2,0); // add borders as clip regions
bitmap[w/2,h/2]=0xff|ff|ff; // plant seed
bitmap.circle(w/2,h/2,h/2,0x0f|0f|0f); // plant seeds
fcn touching(x,y,bitmap){ // is (x,y) touching another pixel?
// (x,y) isn't on the border/edge of bitmap so no edge conditions
var [const] box=T(T(-1,-1),T(0,-1),T(1,-1),
T(-1, 0), T(1, 0),
T(-1, 1),T(0, 1),T(1, 1));
box.filter1('wrap([(a,b)]){ bitmap[a+x,b+y] }); //-->False: not touching, (a,b) if is
}
while(numParticles){
c:=(0x1|00|00).random(0x1|00|00|00) + (0x1|00).random(0x1|00|00) + (0x1).random(0x1|00);
reg x,y;
do{ x=(1).random(w); y=(1).random(h); }while(bitmap[x,y]); // find empty spot
while(1){ // stagger around until bump into a particle, then attach barnicle
if(touching(x,y,bitmap)){
bitmap[x,y]=c;
bitmap.write(f:=File("foo.ppm","wb")); // tell ImageViewer to update image
numParticles-=1;
break;
}
x+=(-1).random(2); y+=(-1).random(2); // [-1,0,1]
if( not ((0<x<w) and (0<y<h)) ){ // next to border --> color border
bitmap[x,y]=c;
break;
}
}
}
bitmap.write(File("foo.ppm","wb")); // the final image