Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- c program:
- --------------------------------
- 1. draws Mandelbrot set for Fc(z)=z*z +c
- using Mandelbrot algorithm ( boolean escape time )
- -------------------------------
- 2. technique of creating ppm file is based on the code of Claudio Rocchini
- http://en.wikipedia.org/wiki/Image:Color_complex_plot.jpg
- create 24 bit color graphic file , portable pixmap file = PPM
- see http://en.wikipedia.org/wiki/Portable_pixmap
- to see the file use external application ( graphic viewer)
- */
- #define _CRT_SECURE_NO_DEPRECATE
- #include <stdio.h>
- #include <math.h>
- #include <thread>
- #include <vector>
- #include <iostream>
- const int iXmax = 800;
- const int iYmax = 800;
- unsigned char color[iXmax][iYmax][3] ;
- const int iloscWatkow = 11;
- int suma[iloscWatkow] = {};
- /* screen ( integer) coordinate */
- /* world ( double) coordinate = parameter plane*/
- const double CxMin = -2.5;
- const double CxMax = 1.5;
- const double CyMin = -2.0;
- const double CyMax = 2.0;
- const int IterationMax = 200;
- double PixelWidth = (CxMax - CxMin) / iXmax;
- double PixelHeight = (CyMax - CyMin) / iYmax;
- const int MaxColorComponentValue = 255;
- FILE* fp;
- const char* filename = "new1.ppm";
- const char* comment = "# ";/* comment should start with # */
- //static unsigned char color[3];
- /* Z=Zx+Zy*i ; Z0 = 0 */
- /* Zx2=Zx*Zx; Zy2=Zy*Zy */
- /* */
- //int Iteration;
- /* bail-out value , radius of circle ; */
- const double EscapeRadius = 2;
- double ER2 = EscapeRadius * EscapeRadius;
- void licz(int watek)
- {
- double Zx, Zy;
- double Zx2, Zy2;
- double Cx, Cy;
- int iX, iY;
- int Iteration = 0;
- for (iY = 0; iY < iYmax; iY++)
- {
- if ((iY % iloscWatkow+1) == watek)
- {
- Cy = CyMin + iY * PixelHeight;
- if (fabs(Cy) < PixelHeight / 2) Cy = 0.0; /* Main antenna */
- for (iX = 0; iX < iXmax; iX++)
- {
- Cx = CxMin + iX * PixelWidth;
- /* initial value of orbit = critical point Z= 0 */
- Zx = 0.0;
- Zy = 0.0;
- Zx2 = Zx * Zx;
- Zy2 = Zy * Zy;
- /* */
- for (Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
- {
- Zy = 2 * Zx * Zy + Cy;
- Zx = Zx2 - Zy2 + Cx;
- Zx2 = Zx * Zx;
- Zy2 = Zy * Zy;
- };
- suma[watek - 1] += Iteration;
- /* compute pixel color (24 bit = 3 bytes) */
- if (Iteration == IterationMax)
- { /* interior of Mandelbrot set = black */
- color[iY][iX][0] = 0;
- color[iY][iX][1] = 0;
- color[iY][iX][2] = 0;
- }
- else
- { /* exterior of Mandelbrot set = white */
- color[iY][iX][0] = watek * 30; /* Red*/
- color[iY][iX][1] = watek * 30; /* Green */
- color[iY][iX][2] = watek * 30;/* Blue */
- };
- }
- }
- }
- }
- int main()
- {
- /* */
- /* color component ( R or G or B) is coded from 0 to 255 */
- /* it is 24 bit color RGB file */
- /*create new file,give it a name and open it in binary mode */
- fp = fopen(filename, "wb"); /* b - binary mode */
- /*write ASCII header to the file*/
- fprintf(fp, "P6\n %s\n %d\n %d\n %d\n", comment, iXmax, iYmax, MaxColorComponentValue);
- /* compute and write image data bytes to the file*/
- std::vector<std::thread> v;
- for (int i = 0; i < iloscWatkow; i++)
- {
- v.push_back(std::thread(licz, i+1 ));
- }
- for (int i = 0; i < iloscWatkow; i++)
- {
- v[i].join();
- }
- /*write color to the file*/
- for (int i = 0; i < iYmax; i++)
- {
- for (int j = 0; j < iXmax; j++)
- fwrite(color[i][j], 1, 3, fp);
- }
- for (int i = 0; i < iloscWatkow; i++)
- {
- std::cout<<"watek nr= "<<i<<" wykonal " << suma[i] << "\n";
- }
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement