Recursive fractals
Off lately recursion has become much more fascinating than ever it was to me. And the obvious catalyst in this has been computer graphics. Without visualization its impossible to appreciate the beauty of recursion, trees, sorting algorithms, etc!
Having admired the Sierpinski triangles I wondered how such an experimentation would look upon squares. And I came up with the following written in devCPP & Allegro graphics library-
#include <allegro.h>
BITMAP *work;
void draw (int x,int y,int w){
int i,j,c,r,g,b;
if (x<0 || y<0 || x>639 || y>479 || w<5)
return;
int nw=w/2;
draw(x-w/2-1,y-w/2-1,nw);
draw(x+w/2+1,y+w/2+1,nw);
draw(x-w/2-1,y+w/2+1,nw);
draw(x+w/2+1,y-w/2-1,nw);
c=w;
for (w=c;w>=1;w--){
r=((float)(sin(w)*sin(w)*c/3)/(float)c)*(float)255;
g=((float)(c-w/2)/(float)c)*(float)255;
b=((float)(w/2)/(float)c)*(float)255;
if (w<1){
r=g=b=0;
}
rect(work,x-w/2,y-w/2,x+w/2,y+w/2, makecol (r,g,b));
}
w=c;
r=((float)(c-w)/(float)c)*(float)255;
g=((float)(c-w)/(float)c)*(float)255;
b=((float)w/(float)c)*(float)255;
rect(work,x-w/2-1,y-w/2-1,x+w/2+1,y+w/2+1, makecol (r/2,g/2,b/2));
}
int main (){
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode (GFX_AUTODETECT_WINDOWED,640,480,0,0);
work = create_bitmap(640,480);
clear_to_color(work,makecol(255,255,255));
draw(320,240,200);
while (!key[KEY_ESC]){
vsync();
blit (work,screen,0,0,0,0,640,480);
}
allegro_exit();
return 0;
}
END_OF_MAIN();
Some circular modifications -
#include <allegro.h>
BITMAP *work;
void draw (int x,int y,int w){
int i,j,c,r,g,b;
if (x<0 || y<0 || x>SCREEN_W || y>SCREEN_H || w<5)
return;
int nw=(float)w*(float)(.5);
draw(x-w/2-1,y-w/2-1,nw);
draw(x+w/2+1,y+w/2+1,nw);
draw(x-w/2-1,y+w/2+1,nw);
draw(x+w/2+1,y-w/2-1,nw);
c=w;
for (w=c;w>=1;w--){
r=((float)(w/2)/(float)c)*(float)255;
g=((float)(c-w/2)/(float)c)*(float)255;
b=((float)(c-w/2)/(float)c)*(float)255;
if (w<1){
r=g=b=0;
}
circlefill (work,x,y,w/2,makecol (r,g,b));
}
}
int main (){
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode (GFX_AUTODETECT_WINDOWED,800,600,0,0);
work = create_bitmap(SCREEN_W,SCREEN_H);
clear_to_color(work,makecol(255,255,255));
draw(SCREEN_W/2,SCREEN_H/2,300);
while (!key[KEY_ESC]){
vsync();
blit (work,screen,0,0,0,0,SCREEN_W,SCREEN_H);
}
allegro_exit();
return 0;
}
END_OF_MAIN();
beautiful! what more can I say.
Labels: allegro, art, graphics, programming, recursion
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home