Sunday, August 30, 2009

Fractals

Fractals are really amazing recursive shapes that can be generated with the least programming effort! I worked on the escape time fractals and was successfully able to code the "Mandelbrot set". I used SDL to generate a program in C. What came as an output was an amazing and beautiful fractal which im never tired of admiring. Depending upon the resolution the program might take from minutes to hours to generate a good fractal. The source code uses SDL and is given below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <windows.h>
#include <math.h>
# define ITR 10000
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
/*
PLEASE NOTE: the program will require SDL.dll which is located in
dev-c++'s dll directory. You have to copy it to you
program's home directory or the path.
*/

/* The screen surface */
SDL_Surface *screen = NULL;


/* This function draws to the screen; replace this with your own code! */
static void
draw ()
{
static int direction = 0;
static int value = 0;
static int which = 0;
SDL_Rect rect;
Uint32 b,w;

/* Create a black background */
b = SDL_MapRGB (screen->format, 0, 0, 0);
w = SDL_MapRGB (screen->format,255,255,255);
int it=0,br=2;
double x0,y0,x,y,t;
Uint32 color;
for(int i=-7120;i<2820;i++)

{
for(int j=-3840;j<3840;j++)

{
x=0;
y=0;
x0=i/(5120.0/1.5);
y0=j/(5120.0/1.5);
it=0;
while(it < ITR && (x*x+y*y)<=4)

{
it++;
t=x*x - y*y + x0 ;
y=2.0*x*y +y0 ;
x=t;
}
if(it==ITR)
{// If pixel is in the mandelbrot set and has successfully completed the iterations
putpixel(screen,7120+i,3840+j,b);

}

else

{
double col= it + (log(log(2))-log(log(t)) )/log(2);
color = SDL_MapRGB (screen->format,0,10*col,0);
putpixel(screen,7120+i,3840+j,color);

}
}
}
SDL_SaveBMP (screen,"fractal.bmp");

}





int main (int argc, char *argv[])

{
char *msg;
int done;
/* Initialize SDL */
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
sprintf (msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());
MessageBox (0, msg, "Error", MB_ICONHAND);
free (msg);
exit (1);

}

atexit (SDL_Quit);
/* Set 640x480 16-bits video mode */
screen = SDL_SetVideoMode (10240,7680, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
{
sprintf (msg, "Couldn't set 640x480x16 video mode: %s\n",
SDL_GetError ());
MessageBox (0, msg, "Error", MB_ICONHAND);
free (msg);
exit (2);
}
SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);
draw ();
return 0;

}





void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)

{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {

p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}



The crux of the beauty of a fractal rests on it's coloring scheme. What i used was the Escape Time Algorithm for smooth colouring. Fractals can be made even more beautiful with a little innovation. Three dimensional effects can also be incorporated.. with only a little more effort..!

2 comments:

  1. fractals are beautiful, nonetheless. if you try seeing the practical applications of fractals, especially in chaos theory, you'll be more amazed. try reading more, knowing more about fractals this way. this way u'll feel more connected, would want to experiment a lil more with the mandelbrot sets.

    ReplyDelete
  2. thanx sir for the advice.. did a bit of searching.. specially loved the fact that colouring scheme can affect the look of a fractal to such a great degree.. i almost obtained a 3 dimensional fractal with a really slight modification in my code.. :)

    ReplyDelete