Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <omp.h>
  5. int main()
  6. {
  7.        /* screen ( integer) coordinate */
  8.        int iX,iY;
  9.        const int iXmax = 3000;
  10.        const int iYmax = 3000;
  11.        /* world ( double) coordinate = parameter plane*/
  12.        double Cx,Cy;
  13.        const double CxMin=-2.5;
  14.        const double CxMax=1.5;
  15.        const double CyMin=-2.0;
  16.        const double CyMax=2.0;
  17.        /* */
  18.        double PixelWidth=(CxMax-CxMin)/iXmax;
  19.        double PixelHeight=(CyMax-CyMin)/iYmax;
  20.        /* color component ( R or G or B) is coded from 0 to 255 */
  21.        /* it is 24 bit color RGB file */
  22.        const int MaxColorComponentValue=255;
  23.        FILE * fp;
  24.        char *filename="new1.ppm";
  25.        char *comment="# ";/* comment should start with # */
  26.        static unsigned char color[iYmax][iXmax][3];
  27.        /* Z=Zx+Zy*i  ;   Z0 = 0 */
  28.        double Zx, Zy;
  29.        double Zx2, Zy2; /* Zx2=Zx*Zx;  Zy2=Zy*Zy  */
  30.        /*  */
  31.        int Iteration;
  32.        const int IterationMax=200;
  33.        /* bail-out value , radius of circle ;  */
  34.        const double EscapeRadius=2;
  35.        double ER2=EscapeRadius*EscapeRadius;
  36.        /*create new file,give it a name and open it in binary mode  */
  37.        fp= fopen(filename,"wb"); /* b -  binary mode */
  38.        /*write ASCII header to the file*/
  39.        fprintf(fp,"P6\n %s\n %d\n %d\n %d\n",comment,iXmax,iYmax,MaxColorComponentValue);
  40.        /* compute and write image data bytes to the file*/
  41.  
  42.        int tab[4] = {0, 0, 0, 0};
  43.        int id;
  44.       double startTime = omp_get_wtime();  
  45.        #pragma omp parallel private(Zx, Zy, Zx2, Zy2, iX, iY, Cy, Cx,  Iteration, id) num_threads(4)
  46.       {
  47.         id = omp_get_thread_num();
  48.         #pragma omp for schedule(static, 60)
  49.          for(iY=0;iY<iYmax;iY++)
  50.         {
  51.             Cy=CyMin + iY*PixelHeight;
  52.             if (fabs(Cy)< PixelHeight/2) Cy=0.0; /* Main antenna */
  53.             for(iX=0;iX<iXmax;iX++)
  54.             {
  55.                        Cx=CxMin + iX*PixelWidth;
  56.                        /* initial value of orbit = critical point Z= 0 */
  57.                        Zx=0.0;
  58.                        Zy=0.0;
  59.                        Zx2=Zx*Zx;
  60.                        Zy2=Zy*Zy;
  61.                        /* */
  62.                        for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++)
  63.                        {
  64.                            Zy=2*Zx*Zy + Cy;
  65.                            Zx=Zx2-Zy2 +Cx;
  66.                            Zx2=Zx*Zx;
  67.                            Zy2=Zy*Zy;
  68.                        };
  69.                //tab[id] += Iteration;
  70.                        /* compute  pixel color (24 bit = 3 bytes) */
  71.                        if (Iteration==IterationMax)
  72.                        { /*  interior of Mandelbrot set = black */
  73.                           color[iY][iX][0]=0;
  74.                           color[iY][iX][1]=0;
  75.                           color[iY][iX][2]=0;
  76.                        }
  77.                     else
  78.                        { /* exterior of Mandelbrot set = white */
  79.                           color[iY][iX][0]=id*60;
  80.                           color[iY][iX][1]=id*60;
  81.                           color[iY][iX][2]=id*60;
  82.                        };
  83.                        /*write color to the file*/
  84.                }
  85.        }
  86.       }
  87.        double endTime = omp_get_wtime() - startTime;
  88.     printf("Time : %f\n t1: %d\n, t2: %d\n, t3: %d\n, t4: %d\n" , endTime, tab[0], tab[1], tab[2],  tab[3]);
  89.        fwrite(color,1,3*iXmax*iYmax,fp);
  90.        
  91.  
  92.        fclose(fp);
  93.        return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement