// ifs int x,y; float f,fctr; float angint; int mode, modecnt; ifs(int x, int y, float ang, float scle) { int nx, ny,a,n; float r; nx=x+cos(ang)*scle; ny=y+sin(ang)*scle; scle = scle / fctr; line(1,x,y,nx,ny); if(scle>0.8){ if(mode==0){ // tree ifs(nx,ny,ang-angint-(rand()-0.5),scle*((rand()*1.2))); ifs(nx,ny,ang+angint+(rand()-0.5),scle*((rand()*1.2))); } else{ if(mode == 1){ //fern ifs(nx,ny,ang-0.5,scle); ifs(nx-(nx-x)*0.4,ny-(ny-y)*0.4,ang+0.5,scle*((rand()*0.6+0.3))); ifs(nx-(nx-x)*0.9,ny-(ny-y)*0.9,ang+0.5,scle*((rand()*0.6+0.3))); ifs(nx-(nx-x)*0.6,ny-(ny-y)*0.6,ang-0.5,scle*((rand()*0.6+0.3))); //ifs(nx-(nx-x)*0.2,ny-(ny-y)*0.2,ang-0.5,scle*((rand()*0.6+0.3))); }else{ if(mode == 2){ // ifs(nx,ny,ang-angint - (rand()-0.5), scle*((rand()*1.0 + 0.2))); ifs(nx,ny,ang+angint + (rand()-0.5), scle*((rand() * 1.0 + 0.2))); r=rand()*0.6; ifs(nx-(nx-x)*r,ny-(ny-y)*r,ang-0.5,scle*((rand()*0.6+0.3))); r=rand()*0.6; ifs(nx-(nx-x)*r,ny-(ny-y)*r,ang+0.5,scle*((rand()*0.6+0.3))); } } } } } draw() { int e; float iang, ilen; int xx, yy; // Get initial position x = penx(); y = peny(); // Draw lines until we stop receiving penMoves do { xx = penx(); yy = peny(); } while (event(1)==4); ilen = sqrt(pow((x - xx),2) + pow((y - yy),2)); if(ilen == 0 || (xx - x) == 0){return;} iang = atan((yy - y)/((float)xx - x)); if((xx-x)<0){ iang = iang + 3.141592; } ifs(x, y, iang, ilen); } main() { int e; mode=0; modecnt=3; angint = 3.141592 / 5; fctr = 1.7; graph_on(); title("IFS"); while (1) { e = event(1); if (e==5){ mode++; mode = mode % modecnt; }else{ if (e==6){ clear(); }else{ if (e==2) draw(); } } } }