Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "SDL/SDL.h"
  4. #include <pthread.h>
  5.  
  6. #define WIDTH 900 // divisible by THREADS
  7. #define HEIGHT 900
  8. #define DEPTH 32
  9.  
  10. #define THREADS 2
  11.  
  12. double xoff=-2, yoff=-1.5;
  13. double scale=(1/3.0);
  14. int itr=300;
  15.  
  16. struct complex {
  17.     double real;
  18.     double img;
  19.     int color[HEIGHT];
  20. };
  21.  
  22. void setpixel(SDL_Surface *screen, int x, int y, int color)
  23. {
  24.     SDL_Rect rect = {x, y, 1, 1};
  25.     SDL_FillRect(screen, &rect, color);
  26. }
  27.  
  28. void* mandelbrot(void* cP) //
  29. {
  30.     struct complex z;
  31.     struct complex *c = (struct complex *) cP;
  32.    
  33.     int i, y;
  34.     double absZ;
  35.  
  36.     for(y=0; y<HEIGHT; y++){
  37.         z.real=0; z.img=0;
  38.         c->img=(double)(y/(scale*HEIGHT))+yoff;
  39.         for(i=0; i<=itr; i++) //set iterations
  40.         {
  41.             absZ=z.real;
  42.             z.real=(z.real*z.real-z.img*z.img)+(c->real);
  43.             z.img=(absZ*z.img)*2+c->img; // find z^2 + c
  44.        
  45.        
  46.             absZ=z.real*z.real+z.img*z.img; // find abs(z)
  47.        
  48.             if( absZ>4 )
  49.                 break; // outside circle with r=2
  50.         }
  51.    
  52.         if(i>=itr)
  53.             c->color[y]=0;
  54.         else   
  55.             c->color[y]=i*log(log10(sqrt(absZ)))/log(2); // kinda nice color
  56.     }
  57.    
  58.     return NULL;
  59. }
  60.    
  61.  
  62. int main()
  63. {
  64.     SDL_Surface *screen;
  65.     SDL_Event event;
  66.    
  67.     // thread stuff
  68.     pthread_t thread[THREADS];
  69.     struct complex threadC[THREADS];
  70.     int threadReturn[THREADS];
  71.     int it;
  72.    
  73.     int quit=0;
  74.    
  75.     if( SDL_Init(SDL_INIT_VIDEO) < 0 )
  76.         return 1;
  77.    
  78.     if(!(screen = SDL_SetVideoMode( WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE)))
  79.     {
  80.         SDL_Quit();
  81.         return 1;
  82.     }
  83.    
  84.     int i,j;
  85.     int draw=1;
  86.     int zoomlevel=1;
  87.    
  88.     while(!quit)
  89.     {
  90.         while(SDL_PollEvent(&event))
  91.         {
  92.             switch(event.type)
  93.             {
  94.                 case SDL_KEYDOWN:
  95.                     switch(event.key.keysym.sym)
  96.                     {
  97.                         case SDLK_LEFT:
  98.                             xoff-=.2/scale;
  99.                             break;
  100.                         case SDLK_RIGHT:
  101.                             xoff+=.2/scale;
  102.                             break;
  103.                         case SDLK_UP:
  104.                             yoff-=.2/scale;
  105.                             break;
  106.                         case SDLK_DOWN:
  107.                             yoff+=.2/scale;
  108.                             break;
  109.                         case SDLK_SPACE:
  110.                             draw=1;
  111.                             printf("Drawing... ");
  112.                             break;
  113.                         case SDLK_1:
  114.                             scale*=2;
  115.                             zoomlevel++;
  116.                             printf("zoom: %d, %f percent of original\n",\
  117.                                  ++zoomlevel, 1.0/zoomlevel);
  118.                             break;
  119.                         case SDLK_2:
  120.                             if(scale>.4){
  121.                                 scale/=2;
  122.                                 zoomlevel--;
  123.                                 printf("zoom: %d, %f percent of original\n",\
  124.                                      --zoomlevel, 1.0/zoomlevel);
  125.                             }
  126.                             break;
  127.                         case SDLK_9:
  128.                             itr+=100;
  129.                             printf("iterations: %d\n", itr);
  130.                             break;
  131.                         case SDLK_0:
  132.                             if(itr>100)
  133.                                 itr-=100;
  134.                             printf("iterations: %d\n", itr);
  135.                             break;
  136.                         default:
  137.                             break;
  138.                     }
  139.                     break;
  140.                    
  141.                 case SDL_MOUSEBUTTONDOWN:
  142.                     if(event.button.button==SDL_BUTTON_LEFT){
  143.                         scale*=2;
  144.                         xoff=xoff+(event.button.x)/(scale*WIDTH);
  145.                         yoff=yoff+(event.button.y)/(scale*HEIGHT);
  146.                         zoomlevel++;
  147.                         printf("zoom: %d, %f percent of original\n", zoomlevel,100.0/zoomlevel);
  148.                         draw=1;
  149.                     }
  150.                     break;
  151.                 case SDL_QUIT:
  152.                     quit=1;
  153.                     break;
  154.                 default:
  155.                     break;
  156.                        
  157.             }
  158.         }
  159.        
  160.         if(draw){
  161.             for(i=0; i<WIDTH; i+=THREADS)
  162.             {
  163.                 for(it=0; it<THREADS; it++)
  164.                     threadC[it].real=(double)((i+it)/(scale*WIDTH))+xoff;
  165.                
  166.                
  167.                     for(it=0; it<THREADS; it++){ // creates threads
  168.                         threadC[it].img=(double)(j/(scale*HEIGHT))+yoff;
  169.                         pthread_create(&thread[it], NULL, mandelbrot, (void *) &threadC[it]);
  170.                         //mandelbrot(&threadC[it]);
  171.                     }
  172.                     for(it=0; it<THREADS; it++){ // wait for threads to end, draws image
  173.                         pthread_join(thread[it], NULL);
  174.                         for(j=0; j<HEIGHT; j++)
  175.                             setpixel(screen, i+it, j, (threadC[it].color[j])*0xF0FF);
  176.                     }
  177.                
  178.             }
  179.             draw=0;
  180.             printf("Done drawing!\n");
  181.         }
  182.         SDL_Flip(screen);
  183.         SDL_Delay(30); // dont use 100% cpu!!!
  184.     }
  185.    
  186.     SDL_Quit();
  187.    
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement