/****************************************************************************
 * 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 {
      (0.35405447172983978000,
       -0.00099872471148845657,
       0.35570717612540947000,
       0.00099971958037741847) => (749,960)
   }






   formula {
     
      z = current;
      zold = [0, 0];

      $xmax = 0; $xmin = 0; $xtot = 0;
      $ymax = 0; $ymin = 0; $ytot = 0;
      $mmax = 0; $mmin = 0; $mtot = 0;

     
      $stop = 0;
      $distance = 0;
      $trap = 0;
      $tolerance = 1e-6;
      $maxiter = 6;
      $trapdist = 1;

      while($stop == 0 && $count < $maxiter)
      {
         if(ssq(z - zold) < $tolerance)
         {
            $stop = 1;
         }
         else
         {

            if($count > 2 && fmod(deg(z), 10) < $trapdist)
            {
               $stop = 1;
               $distance = abs(fmod(deg(z), 10) - $trapdist);
               $trap = 1;
            }
            else
            {            
               f_z  = z * log(sin(z));
               fp_z = log(sin(z)) + z * cot(z);

               zold = z;
               z    = z - fp_z / f_z;

               $x = real(z);
               $y = imag(z);
               $m = mag(z);

               $xtot = $xtot + $x;
               $ytot = $ytot + $y;
               $mtot = $mtot + $m;

               $xmax = max($xmax, $x);
               $xmin = min($xmin, $x);

               $ymax = max($ymax, $y);
               $ymin = min($ymin, $y);

               $mmax = max($mmax, $m);
               $mmin = min($mmin, $m);
            }
         }
      }

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


      if($count > 0)
      {   

         if($trap == 1)
         {
            $r = 1/($trapdist/2) * abs($distance - $trapdist/2) * 255;
            $r = 255 - $r;
            $g = $r;
            $b = $r;
         }

         else
         {  
         $r = (($xtot / $count) - $xmin) / ($xmax - $xmin);
         $g = (($mtot / $count) - $mmin) / ($mmax - $mmin);
         $b = (($ytot / $count) - $ymin) / ($ymax - $ymin);

         $r = get_sin_color($r, 255, 1);
         $g = get_sin_color($g, 205, 1);
         $b = get_sin_color($b, 205, 1);
         }
      }


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









