Advertisement
Chris_M_Thomasson

Reverse Julia^2 Iteration Mutation...

May 26th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. complex_t
  2. ct_reversex(
  3.     complex_t o, // center
  4.     xfloat_t r, // radius
  5.     complex_t z, // start point
  6.     complex_t c, // const point
  7.     xfloat_t p, // prob factor wrt roots
  8.     glm::vec4 const& color, // color
  9.     unsigned int imax // iterations
  10. ){
  11.     xfloat_t smax = 0.0;
  12.     complex_t cx = { -c.real(), c.imag() };
  13.     xfloat_t P = 0.2;
  14.  
  15.     for (unsigned int i = 0; i < imax; ++i)
  16.     {
  17.         complex_t d = z - c; // the master difference
  18.  
  19.         xfloat_t rn0 = RAND_FLOAT(); // [0...1] prob range
  20.         xfloat_t rn1 = RAND_FLOAT(); // [0...1] prob range
  21.  
  22.         xfloat_t l = std::abs(d); // distance
  23.         xfloat_t s = std::sqrt(l); // sqrt of distance
  24.  
  25.         // Buddha like mutation
  26.         if (rn0 > 0.97) // slim prob factor... ;^)
  27.         {
  28.             // The log of distance extends the fractal
  29.             // internal and external structures! Wow.
  30.             s = std::log(l);
  31.         }
  32.  
  33.         xfloat_t a = std::atan2(d.imag(), d.real()) / 2.0; // angle
  34.         xfloat_t sgn = -1.0f;
  35.  
  36.         smax = std::max(smax, s); // gain largest radius
  37.  
  38.  
  39.         // notice the (a + PI) here... Simple, but powerful indeed ;^)
  40.         // gain both roots
  41.         complex_t nz_0 = { s * std::cos(a), s * std::sin(a + PI) };
  42.         complex_t nz_1 = { -1.0 * s * std::cos(a), -1.0 * s * std::sin(a) };
  43.  
  44.         // project roots onto the canvas (unsigned integer plane)
  45.         complex_t nz_p0 = {
  46.             std::floor(nz_0.real() * r + o.real()),
  47.             std::floor(nz_0.imag() * r + o.imag())
  48.         };
  49.  
  50.         complex_t nz_p1 = {
  51.             std::floor(nz_1.real() * r + o.real()),
  52.             std::floor(nz_1.imag() * r + o.imag())
  53.         };
  54.  
  55.         // plot the damn thing in the hit list
  56.         if (nz_p0.real() > -1 && nz_p0.real() < m_gc.m_width &&
  57.             nz_p0.imag() > -1 && nz_p0.imag() < m_gc.m_height)
  58.         {
  59.             unsigned int ihit = nz_p0.real() + m_gc.m_width * nz_p0.imag();
  60.             if (ihit < m_hits.size()) m_hits[ihit] += 1.0f;
  61.         }
  62.  
  63.         if (nz_p1.real() > -1 && nz_p1.real() < m_gc.m_width &&
  64.             nz_p1.imag() > -1 && nz_p1.imag() < m_gc.m_height)
  65.         {
  66.             unsigned int ihit = nz_p1.real() + m_gc.m_width * nz_p1.imag();
  67.             if (ihit < m_hits.size()) m_hits[ihit] += 1.0f;
  68.         }
  69.  
  70.         // choose random root with probability factor p
  71.         z = (rn1 > p) ? nz_0 : nz_1;
  72.  
  73.         if (!(i % 1000000))
  74.         {
  75.             std::printf("iterated: %u of %u\r", i, imax);
  76.         }
  77.     }
  78.  
  79.     std::printf("\r\nComplete!\r\n");
  80.  
  81.     return z;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement