/****************************************************************************
 * 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 {
      (-1.20930635838150291050,
       -1.24120000000000008100,
       1.20468208092485551397,
       1.23819999999999996732) => (935,960)
   }

   formula {

      z = current;
 
      $xavg = 0;
      $yavg = 0;
      $mavg = 0;
 
      $xmax = 0;
      $ymax = 0;
      $mmax = 0;
 
      $xtot = 0;
      $ytot = 0;
      $mtot = 0;

      $limit = 20;

      while($count < $limit && ssq(z) < $limit)
      {
         z = z ^ 2 + [0.238498, 0.519198];

         value  = sec(z);

         if(inside(z^z, r_and(r_spoly([0, 0], 5, 2, 0), r_not(r_spoly([0, 0], 5, .5, 0)))))
         {
            value = log(z);
         }
 
         $x = real(value);
         $y = imag(value);
         $m = mag(value);
 
         $xavg = ($x + $xavg * $count) / ($count + 1);
         $yavg = ($y + $yavg * $count) / ($count + 1);
         $mavg = ($m + $mavg * $count) / ($count + 1);
 
         $xdev = sqrt(($x - $xavg) * ($x - $xavg));
         $ydev = sqrt(($y - $yavg) * ($y - $yavg));
         $mdev = sqrt(($m - $mavg) * ($m - $mavg));
 
         $xmax = max($xmax, $xdev);
         $ymax = max($ymax, $ydev);
         $mmax = max($mmax, $mdev);
 
         $xtot += $xdev;
         $ytot += $ydev;
         $mtot += $mdev;
      }

      $r = 0; $g = 0; $b = 0;
 
      if($count > 0)
      {
         // Here we divide the average standard deviation by the
         // largest standard deviation
 
         $r = ($xtot/$count) / $xmax;
         $g = ($ytot/$count) / $ymax;
         $b = ($mtot/$count) / $mmax;
 
         $r = get_sin_color($r, 255, 1);
         $g = get_sin_color($g, 255, 1);
         $b = get_sin_color($b, 255, 1);
      }

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



















