Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. /*
  2.  c program:
  3.  --------------------------------
  4.   1. draws Mandelbrot set for Fc(z)=z*z +c
  5.   using Mandelbrot algorithm ( boolean escape time )
  6.  -------------------------------        
  7.  2. technique of creating ppm file is  based on the code of Claudio Rocchini
  8.  http://en.wikipedia.org/wiki/Image:Color_complex_plot.jpg
  9.  create 24 bit color graphic file ,  portable pixmap file = PPM
  10.  see http://en.wikipedia.org/wiki/Portable_pixmap
  11.  to see the file use external application ( graphic viewer)
  12.   */
  13.  #include <stdio.h>
  14.  #include <math.h>
  15. #include <omp.h>
  16.  
  17. const int iXmax = 3000;
  18. const int iYmax = 3000;
  19.  
  20. unsigned char color[iYmax][iXmax][3];
  21. int stat[8] = { 0 };
  22.  
  23.  int main()
  24.  {
  25.           /* screen ( integer) coordinate */
  26.         int iX,iY;
  27.         // const int iXmax = 800;
  28.         // const int iYmax = 800;
  29.         /* world ( double) coordinate = parameter plane*/
  30.         double Cx,Cy;
  31.         const double CxMin=-2.5;
  32.         const double CxMax=1.5;
  33.         const double CyMin=-2.0;
  34.         const double CyMax=2.0;
  35.         /* */
  36.         double PixelWidth=(CxMax-CxMin)/iXmax;
  37.         double PixelHeight=(CyMax-CyMin)/iYmax;
  38.         /* color component ( R or G or B) is coded from 0 to 255 */
  39.         /* it is 24 bit color RGB file */
  40.         const int MaxColorComponentValue=255;
  41.         FILE * fp;
  42.         char *filename="new1.ppm";
  43.         char *comment="# ";/* comment should start with # */
  44.  
  45.         /* Z=Zx+Zy*i  ;   Z0 = 0 */
  46.         double Zx, Zy;
  47.         double Zx2, Zy2; /* Zx2=Zx*Zx;  Zy2=Zy*Zy  */
  48.         /*  */
  49.         int Iteration;
  50.         const int IterationMax=200;
  51.         /* bail-out value , radius of circle ;  */
  52.         const double EscapeRadius=2;
  53.         double ER2=EscapeRadius*EscapeRadius;
  54.         /*create new file,give it a name and open it in binary mode  */
  55.         fp= fopen(filename,"wb"); /* b -  binary mode */
  56.         /*write ASCII header to the file*/
  57.         fprintf(fp,"P6\n %s\n %d\n %d\n %d\n",comment,iXmax,iYmax,MaxColorComponentValue);
  58.         /* compute and write image data bytes to the file*/
  59.  
  60.     double start_time, end_time;
  61.  
  62.     start_time = omp_get_wtime();
  63.     int id = 0;
  64.    
  65.     #pragma omp parallel private (id) num_threads(8)
  66.     {
  67.     #pragma omp for private (iY, iX, Zx, Zy, Zx2, Zy2, Cy, Cx, id) schedule(guided, 50)
  68.         for(iY=0;iY<iYmax;iY++)
  69.         {
  70.          id = omp_get_thread_num();
  71.              Cy=CyMin + iY*PixelHeight;
  72.              if (fabs(Cy)< PixelHeight/2) Cy=0.0; /* Main antenna */
  73.          
  74.              for(iX=0;iX<iXmax;iX++)
  75.              {        
  76.                         Cx=CxMin + iX*PixelWidth;
  77.                         /* initial value of orbit = critical point Z= 0 */
  78.                         Zx=0.0;
  79.                         Zy=0.0;
  80.                         Zx2=Zx*Zx;
  81.                         Zy2=Zy*Zy;
  82.                         /* */
  83.                         for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++)
  84.                         {
  85.                             Zy=2*Zx*Zy + Cy;
  86.                             Zx=Zx2-Zy2 +Cx;
  87.                             Zx2=Zx*Zx;
  88.                             Zy2=Zy*Zy;
  89.                         };
  90.             stat[id] += Iteration;
  91.                         /* compute  pixel color (24 bit = 3 bytes) */
  92.                         if (Iteration==IterationMax)
  93.                         { /*  interior of Mandelbrot set = black */
  94.                            color[iY][iX][0]=0;
  95.                            color[iY][iX][1]=0;
  96.                            color[iY][iX][2]=0;                          
  97.                         }
  98.                      else
  99.                         { /* exterior of Mandelbrot set = white */
  100.                              color[iY][iX][0]=(id) * 30; /* Red*/
  101.                              color[iY][iX][1]=(id) * 30; /* Green */
  102.                              color[iY][iX][2]=(id) * 30;/* Blue */
  103.                         };
  104.                         /*write color to the file*/
  105.                 }
  106.         }
  107.     }
  108.         fwrite(color,1,3*iXmax*iYmax,fp);
  109.     end_time = omp_get_wtime();
  110.         fclose(fp);
  111.     printf("Work took %f seconds\n", end_time - start_time);
  112.      for (int i=0; i<8; i++)
  113.         printf("stat[%d] = %d\n", i, stat[i]);
  114.  
  115.         return 0;
  116.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement