Advertisement
Guest User

Untitled

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