Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "SDL/SDL.h"
  4.  
  5. #define WIDTH 900
  6. #define HEIGHT 900
  7. #define DEPTH 32
  8.  
  9. struct complex {
  10.     double real;
  11.     double img;
  12. };
  13.  
  14. void setpixel(SDL_Surface *screen, int x, int y, int color)
  15. {
  16.     SDL_Rect rect = {x, y, 1, 1};
  17.     SDL_FillRect(screen, &rect, color);
  18. }
  19.  
  20. int mandelbrot(struct complex c, int itr) // returns the iterations performed
  21. {
  22.     struct complex z;
  23.     z.real=0; z.img=0;
  24.     int i;
  25.     double absZ;
  26.    
  27.     for(i=0; i<=itr; i++) //set iterations
  28.     {
  29.         absZ=z.real;
  30.         z.real=(z.real*z.real-z.img*z.img)+(c.real);
  31.         z.img=(absZ*z.img)*2+c.img; // find z^2 + c
  32.        
  33.        
  34.         absZ=z.real*z.real+z.img*z.img; // find abs(z)
  35.        
  36.         if( absZ>=4 )
  37.             break; // outside circle with r=2
  38.     }
  39.     if(i>=itr-1)
  40.         return 0;
  41.     return i+1-log(log(sqrt(absZ*2000)))/log(2);
  42. }
  43.    
  44.  
  45. int main()
  46. {
  47.     SDL_Surface *screen;
  48.     SDL_Event event;
  49.    
  50.     int quit=0;
  51.    
  52.     if( SDL_Init(SDL_INIT_VIDEO) < 0 )
  53.         return 1;
  54.    
  55.     if(!(screen = SDL_SetVideoMode( WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE)))
  56.     {
  57.         SDL_Quit();
  58.         return 1;
  59.     }
  60.     int i,j,iter, itr=300;
  61.     double yoff=-1.5, xoff=-2;
  62.     double scale=(1/3.0);
  63.     int draw=1;
  64.     struct complex c;
  65.    
  66.     while(!quit)
  67.     {
  68.         while(SDL_PollEvent(&event))
  69.         {
  70.             if(event.type==SDL_QUIT)
  71.                 quit=1;
  72.             switch(event.type)
  73.             {
  74.                 case SDL_KEYDOWN:
  75.                     switch(event.key.keysym.sym)
  76.                     {
  77.                         case SDLK_LEFT:
  78.                             xoff-=.2/scale;
  79.                             break;
  80.                         case SDLK_RIGHT:
  81.                             xoff+=.2/scale;
  82.                             break;
  83.                         case SDLK_UP:
  84.                             yoff+=.2/scale;
  85.                             break;
  86.                         case SDLK_DOWN:
  87.                             yoff-=.2/scale;
  88.                             break;
  89.                         case SDLK_SPACE:
  90.                             draw=1;
  91.                             printf("Drawing... ");
  92.                             break;
  93.                         case SDLK_1:
  94.                             scale*=2;
  95.                             printf("scale: %f\n", scale);
  96.                             break;
  97.                         case SDLK_2:
  98.                             if(scale>.4)
  99.                                 scale/=2;
  100.                             printf("scale: %f\n", scale);
  101.                             break;
  102.                         case SDLK_9:
  103.                             itr+=1000;
  104.                             printf("iterations: %d\n", itr);
  105.                             break;
  106.                         case SDLK_0:
  107.                             if(itr>1000)
  108.                                 itr-=100;
  109.                             printf("iterations: %d\n", itr);
  110.                             break;
  111.                         default:
  112.                             break;
  113.                     }
  114.                     break;
  115.                 case SDL_MOUSEBUTTONDOWN:
  116.                     if(event.button.button==SDL_BUTTON_LEFT){;
  117.                         scale*=2;
  118.                         xoff=xoff+(event.button.x)/(scale*WIDTH);
  119.                         yoff=yoff+(event.button.y)/(scale*HEIGHT);
  120.                         draw=1;
  121.                     }
  122.                     break;
  123.                 default:
  124.                     break;
  125.                        
  126.             }
  127.         }
  128.         if(draw){
  129.             for(i=0; i<WIDTH; i++)
  130.             {
  131.                 c.real=(double)(i/(scale*WIDTH))+xoff;
  132.                 for(j=0; j<HEIGHT; j++)
  133.                 {
  134.                     c.img=(double)(j/(scale*HEIGHT))+yoff;
  135.                     iter=mandelbrot(c, itr);
  136.                     setpixel(screen, i, j, iter*0xF0FF);
  137.                 }
  138.             }
  139.             draw=0;
  140.             printf("Done drawing!\n");
  141.         }
  142.         SDL_Flip(screen);
  143.         SDL_Delay(30); // dont use 100% cpu!!!
  144.     }
  145.    
  146.     SDL_Quit();
  147.    
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement