Advertisement
Chris_M_Thomasson

Complex Iteration Example

Nov 25th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. /* (Z^2 + ((RE(Z) + IM(Z)) / 2.1)) - 0.83
  2.    Very Crude and Quick Example by Chris M. Thomasson for Sparky...
  3.    :^D
  4. __________________________________________________________________
  5. */
  6.  
  7.  
  8. #include <iostream>
  9. #include <EasyBMP.h>
  10. #include <complex>
  11.  
  12.  
  13. typedef std::complex<double> complex_t;
  14.  
  15.  
  16. void iterate_point(BMP& bmp, complex_t c, unsigned int x, unsigned int y)
  17. {
  18.     complex_t z = c;
  19.     complex_t d = z;
  20.     double g = 500.0;
  21.     unsigned int imax = 168;
  22.  
  23.  
  24.     for (unsigned int i = 0; i < imax; ++i)
  25.     {
  26.         //(Z^2 + ((RE(Z) + IM(Z)) / 2.1)) - 0.83
  27.  
  28.         z = (z*z + ((z.real() + z.imag()) / 2.1)) - 0.83;
  29.  
  30.         double e = std::abs(z);
  31.  
  32.         //if (e > 10)
  33.         if (z.real() / d.real() > g && z.imag() / d.imag() > g)
  34.         {
  35.             RGBApixel color;
  36.             color.Alpha = 255;
  37.             color.Red = std::abs(std::sin((z.real() * z.imag()) / 7) * 255);
  38.             color.Green = (i * 3) % 256;
  39.             color.Blue = std::abs(std::cos((z.real() * z.imag()) / 13) * 255);
  40.  
  41.             bmp.SetPixel(x, y, color);
  42.  
  43.             return;
  44.         }
  45.  
  46.         d = z;
  47.     }
  48.  
  49.     RGBApixel color;
  50.     color.Alpha = 255;
  51.     color.Red = 0;
  52.     color.Green = 0;
  53.     color.Blue = 255;
  54.  
  55.     bmp.SetPixel(x, y, color);
  56. }
  57.  
  58.  
  59. void iterate_plane(BMP& bmp, unsigned int width, unsigned height)
  60. {
  61.     double xmin = -1.7;
  62.     double xmax = 1.3;
  63.     double ymin = -1.3;
  64.     double ymax = 1.7;
  65.  
  66.     double xstep = (xmax - xmin) / (width - 1.0);
  67.     double ystep = (ymax - ymin) / (height - 1.0);
  68.  
  69.     for (unsigned int y = 0; y < height; ++y)
  70.     {
  71.         for (unsigned int x = 0; x < width; ++x)
  72.         {
  73.             complex_t c(xmin + x * xstep, ymin + y * ystep);
  74.  
  75.             iterate_point(bmp, c, x, y);
  76.         }
  77.  
  78.         std::cout << "y = " << y << "\r";
  79.     }
  80.  
  81.     std::cout << "\r\n";
  82. }
  83.  
  84.  
  85.  
  86. int main()
  87. {
  88.     {
  89.         BMP this_bmp;
  90.  
  91.         unsigned int bmp_width = 340;
  92.         unsigned int bmp_height = 340;
  93.  
  94.         this_bmp.SetSize(bmp_width, bmp_height);
  95.         this_bmp.SetBitDepth(24);
  96.  
  97.         iterate_plane(this_bmp, bmp_width, bmp_height);
  98.  
  99.         this_bmp.WriteToFile("C:\\Users\\Chris\\Desktop\\hello_fractal.bmp");
  100.     }
  101.  
  102.     std::cout << "Done\r\n";
  103.     std::cin.get();
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement