Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <thread>
  5.  
  6. #define thread_count 8
  7.  
  8. using namespace std;
  9.  
  10. const int iXmax = 800;
  11. const int iYmax = 800;
  12.  
  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.  
  21. const int MaxColorComponentValue=255;
  22.  
  23. FILE * fp;
  24. char const *filename="new1.ppm";
  25. char const *comment="# ";
  26.  
  27. static unsigned char color[iXmax][iYmax][3];
  28.  
  29. const int IterationMax=200;
  30.  
  31. const double EscapeRadius=2;
  32. double ER2=EscapeRadius*EscapeRadius;
  33.  
  34. void mandelBrotDraw(int min, int max, int i)
  35. {
  36.     double Cx, Cy, Zx, Zy, Zx2, Zy2;
  37.     int iX, iY, Iteration;
  38.     for(iY=0;iY<iYmax;iY++)
  39.     {
  40.         Cy=CyMin + iY*PixelHeight;
  41.         if (fabs(Cy)< PixelHeight/2) Cy=0.0; /* Main antenna */
  42.         for(iX=0;iX<iXmax;iX++)
  43.         {
  44.             Cx=CxMin + iX*PixelWidth;
  45.             /* initial value of orbit = critical point Z= 0 */
  46.             Zx=0.0;
  47.             Zy=0.0;
  48.            
  49.             Zx2=Zx*Zx;
  50.             Zy2=Zy*Zy;
  51.  
  52.             for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++)
  53.             {
  54.                 Zy=2*Zx*Zy + Cy;
  55.                 Zx=Zx2-Zy2 +Cx;
  56.                 Zx2=Zx*Zx;
  57.                 Zy2=Zy*Zy;
  58.             }
  59.            
  60.             /* compute  pixel color (24 bit = 3 bytes) */
  61.             if (Iteration==IterationMax)
  62.             {
  63.                 color[iX][iY][0]=(i*255)/(thread_count - 1);
  64.                 color[iX][iY][1]=(i*255)/(thread_count - 1);
  65.                 color[iX][iY][2]=(i*255)/(thread_count - 1);
  66.             }
  67.             else
  68.             {
  69.                 color[iX][iY][0]=255 - (i*255)/(thread_count - 1);  /* Red*/
  70.                 color[iX][iY][1]=255 - (i*255)/(thread_count - 1);  /* Green */
  71.                 color[iX][iY][2]=255 - (i*255)/(thread_count - 1);  /* Blue */
  72.             };
  73.         }
  74.     }
  75. }
  76.  
  77. int main()
  78. {
  79.     fp= fopen(filename,"wb"); /* b -  binary mode */
  80.     fprintf(fp,"P6\n %s\n %d\n %d\n %d\n",comment,iXmax,iYmax,MaxColorComponentValue);
  81.    
  82.     thread magazynWatkow[thread_count];
  83.    
  84.     for(int i=0; i<thread_count; i++)
  85.         magazynWatkow[i] = thread(mandelBrotDraw, i*(iYmax / thread_count), (i + 1)*(iYmax / thread_count), i);
  86.    
  87.     for(int i=0; i<thread_count; i++)
  88.         magazynWatkow[i].join();
  89.  
  90.     for(int iY=0; iY<iYmax;iY++)
  91.         for(int iX=0; iX<iXmax;iX++)
  92.             fwrite(color[iX][iY],1,3,fp);
  93.    
  94.     fclose(fp);
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement