/****************************************************************************
 * This formula file or any formula file derived from it cannot be used for
 * commercial purposes or to create hard copy output without the explicit
 * consent of the original author.
 ****************************************************************************/

fractal {

   // Establish the mapping between complex coordinates
   // and display coordinates

   mapping {
      (-2.26504267388057820000,
       -1.77626131468397190000,
       2.27491157130980340000,
       1.77056286346544870000) => (1228,960)
   }

   formula
   {
      // Ok, this bit is slightly tricky - the dragon fractal we want to
      // draw actually turns out to be a bit off center (0, 0) so we
      // adjust 'current' slightly (add [.5, .00008]) to move the
      // dragon back to the center.

      p = [1.646009, 0.967049];
      z = current + [.5, .00008];


      $v1total = 0; $v2total = 0; $v3total = 0;
      $v1min   = 0; $v2min   = 0; $v3min   = 0;
      $v1max   = 0; $v2max   = 0; $v3max   = 0;

      $iterations = 32;
      $stop       = 0;

      while($count < $iterations && $stop == 0)
      {
         z = p * z * (1 - z);

         value = log(z);

         $v1 = real(value);
         $v2 = imag(value);
         $v3 = mag(value);

         $v1total += $v1; $v2total += $v2; $v3total += $v3;

         $v1min = min($v1, $v1min);
         $v2min = min($v2, $v2min);
         $v3min = min($v3, $v3min);

         $v1max = max($v1, $v1max);
         $v2max = max($v2, $v2max);
         $v3max = max($v3, $v3max);


         if(ssq(z) >= 1e2) { $stop = 1; }
      }

      // Ok, if we just drew the fracta 'as is' all the points where
      // $stop was 1 turn out to be black (which looks pretty boring)
      // so instead, what we'll do is draw an entirely different fractal
      // altogether for those points where we bailed out of our upper
      // while loop prematurely (i.e. where ssq(a) >= 1e2)

      if($stop == 1)
      {
         $v1total = 0; $v2total = 0; $v3total = 0;
         $v1min   = 0; $v2min   = 0; $v3min   = 0;
         $v1max   = 0; $v2max   = 0; $v3max   = 0;

         // This fractal does center itself nicely around the origin
         // but we want it zoomed in with respect to the dragon fractal
         // image so instead of setting: z = current we use current/2
         // which effectively doubles the size of this image with respect
         // to the dragon above.

         z = [real(current) / 2, imag(current) / 2];

         for($count = 0; $count < 50 && ssq(z) < 1e2; $count ++)
         {
            z = z ^ 2 - 1;

            value = sec(z);

            $v1 = real(value);
            $v2 = imag(value);
            $v3 = mag(value);

            $v1total += $v1; $v2total += $v2; $v3total += $v3;

            $v1min = min($v1, $v1min);
            $v2min = min($v2, $v2min);
            $v3min = min($v3, $v3min);

            $v1max = max($v1, $v1max);
            $v2max = max($v2, $v2max);
            $v3max = max($v3, $v3max);
         }
      }

      $r = 0; $g = 0; $b = 0;

      if($count > 0)
      {
         $r = (($v1total / $count) - $v1min) / ($v1max - $v1min);
         $g = (($v2total / $count) - $v2min) / ($v2max - $v2min);
         $b = (($v3total / $count) - $v3min) / ($v3max - $v3min);

         $r = get_sin_color($r, 255, .8); 
         $g = get_sin_color($g, 255, 1.1);
         $b = get_sin_color($b, 255, 1);
      }

      set_color($r, $g, $b);
   }
}













