Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 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. #define _CRT_SECURE_NO_DEPRECATE
  14.  
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include <thread>
  18. #include <vector>
  19. #include <iostream>
  20.  
  21. const int iXmax = 800;
  22. const int iYmax = 800;
  23. unsigned char color[iXmax][iYmax][3] ;
  24.  
  25. const int iloscWatkow = 11;
  26. int suma[iloscWatkow] = {};
  27.  
  28. /* screen ( integer) coordinate */
  29.  
  30. /* world ( double) coordinate = parameter plane*/
  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. const int IterationMax = 200;
  36. double PixelWidth = (CxMax - CxMin) / iXmax;
  37. double PixelHeight = (CyMax - CyMin) / iYmax;
  38.  
  39. const int MaxColorComponentValue = 255;
  40. FILE* fp;
  41. const char* filename = "new1.ppm";
  42. const char* comment = "# ";/* comment should start with # */
  43. //static unsigned char color[3];
  44. /* Z=Zx+Zy*i ; Z0 = 0 */
  45.  
  46. /* Zx2=Zx*Zx; Zy2=Zy*Zy */
  47. /* */
  48. //int Iteration;
  49. /* bail-out value , radius of circle ; */
  50. const double EscapeRadius = 2;
  51. double ER2 = EscapeRadius * EscapeRadius;
  52.  
  53. void licz(int watek)
  54. {
  55. double Zx, Zy;
  56. double Zx2, Zy2;
  57. double Cx, Cy;
  58. int iX, iY;
  59. int Iteration = 0;
  60. for (iY = 0; iY < iYmax; iY++)
  61. {
  62. if ((iY % iloscWatkow+1) == watek)
  63. {
  64. Cy = CyMin + iY * PixelHeight;
  65. if (fabs(Cy) < PixelHeight / 2) Cy = 0.0; /* Main antenna */
  66. for (iX = 0; iX < iXmax; iX++)
  67. {
  68. Cx = CxMin + iX * PixelWidth;
  69. /* initial value of orbit = critical point Z= 0 */
  70. Zx = 0.0;
  71. Zy = 0.0;
  72. Zx2 = Zx * Zx;
  73. Zy2 = Zy * Zy;
  74. /* */
  75. for (Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
  76. {
  77. Zy = 2 * Zx * Zy + Cy;
  78. Zx = Zx2 - Zy2 + Cx;
  79. Zx2 = Zx * Zx;
  80. Zy2 = Zy * Zy;
  81. };
  82. suma[watek - 1] += Iteration;
  83. /* compute pixel color (24 bit = 3 bytes) */
  84. if (Iteration == IterationMax)
  85. { /* interior of Mandelbrot set = black */
  86. color[iY][iX][0] = 0;
  87. color[iY][iX][1] = 0;
  88. color[iY][iX][2] = 0;
  89. }
  90. else
  91. { /* exterior of Mandelbrot set = white */
  92. color[iY][iX][0] = watek * 30; /* Red*/
  93. color[iY][iX][1] = watek * 30; /* Green */
  94. color[iY][iX][2] = watek * 30;/* Blue */
  95. };
  96. }
  97. }
  98. }
  99. }
  100.  
  101. int main()
  102. {
  103. /* */
  104. /* color component ( R or G or B) is coded from 0 to 255 */
  105. /* it is 24 bit color RGB file */
  106. /*create new file,give it a name and open it in binary mode */
  107. fp = fopen(filename, "wb"); /* b - binary mode */
  108. /*write ASCII header to the file*/
  109. fprintf(fp, "P6\n %s\n %d\n %d\n %d\n", comment, iXmax, iYmax, MaxColorComponentValue);
  110. /* compute and write image data bytes to the file*/
  111.  
  112.  
  113.  
  114. std::vector<std::thread> v;
  115. for (int i = 0; i < iloscWatkow; i++)
  116. {
  117. v.push_back(std::thread(licz, i+1 ));
  118. }
  119. for (int i = 0; i < iloscWatkow; i++)
  120. {
  121. v[i].join();
  122. }
  123. /*write color to the file*/
  124.  
  125. for (int i = 0; i < iYmax; i++)
  126. {
  127. for (int j = 0; j < iXmax; j++)
  128. fwrite(color[i][j], 1, 3, fp);
  129. }
  130.  
  131. for (int i = 0; i < iloscWatkow; i++)
  132. {
  133. std::cout<<"watek nr= "<<i<<" wykonal " << suma[i] << "\n";
  134. }
  135.  
  136. fclose(fp);
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement