Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include "SDL/SDL.h"
- #define WIDTH 900
- #define HEIGHT 900
- #define DEPTH 32
- struct complex {
- double real;
- double img;
- };
- void setpixel(SDL_Surface *screen, int x, int y, int color)
- {
- SDL_Rect rect = {x, y, 1, 1};
- SDL_FillRect(screen, &rect, color);
- }
- int mandelbrot(struct complex c, int itr) // returns the iterations performed
- {
- struct complex z;
- z.real=0; z.img=0;
- int i;
- double absZ;
- for(i=0; i<=itr; i++) //set iterations
- {
- absZ=z.real;
- z.real=(z.real*z.real-z.img*z.img)+(c.real);
- z.img=(absZ*z.img)*2+c.img; // find z^2 + c
- absZ=z.real*z.real+z.img*z.img; // find abs(z)
- if( absZ>=4 )
- break; // outside circle with r=2
- }
- if(i>=itr-1)
- return 0;
- return i+1-log(log(sqrt(absZ*2000)))/log(2);
- }
- int main()
- {
- SDL_Surface *screen;
- SDL_Event event;
- int quit=0;
- if( SDL_Init(SDL_INIT_VIDEO) < 0 )
- return 1;
- if(!(screen = SDL_SetVideoMode( WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE)))
- {
- SDL_Quit();
- return 1;
- }
- int i,j,iter, itr=300;
- double yoff=-1.5, xoff=-2;
- double scale=(1/3.0);
- int draw=1;
- struct complex c;
- while(!quit)
- {
- while(SDL_PollEvent(&event))
- {
- if(event.type==SDL_QUIT)
- quit=1;
- switch(event.type)
- {
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_LEFT:
- xoff-=.2/scale;
- break;
- case SDLK_RIGHT:
- xoff+=.2/scale;
- break;
- case SDLK_UP:
- yoff+=.2/scale;
- break;
- case SDLK_DOWN:
- yoff-=.2/scale;
- break;
- case SDLK_SPACE:
- draw=1;
- printf("Drawing... ");
- break;
- case SDLK_1:
- scale*=2;
- printf("scale: %f\n", scale);
- break;
- case SDLK_2:
- if(scale>.4)
- scale/=2;
- printf("scale: %f\n", scale);
- break;
- case SDLK_9:
- itr+=1000;
- printf("iterations: %d\n", itr);
- break;
- case SDLK_0:
- if(itr>1000)
- itr-=100;
- printf("iterations: %d\n", itr);
- break;
- default:
- break;
- }
- break;
- case SDL_MOUSEBUTTONDOWN:
- if(event.button.button==SDL_BUTTON_LEFT){;
- scale*=2;
- xoff=xoff+(event.button.x)/(scale*WIDTH);
- yoff=yoff+(event.button.y)/(scale*HEIGHT);
- draw=1;
- }
- break;
- default:
- break;
- }
- }
- if(draw){
- for(i=0; i<WIDTH; i++)
- {
- c.real=(double)(i/(scale*WIDTH))+xoff;
- for(j=0; j<HEIGHT; j++)
- {
- c.img=(double)(j/(scale*HEIGHT))+yoff;
- iter=mandelbrot(c, itr);
- setpixel(screen, i, j, iter*0xF0FF);
- }
- }
- draw=0;
- printf("Done drawing!\n");
- }
- SDL_Flip(screen);
- SDL_Delay(30); // dont use 100% cpu!!!
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement