fractal 
{
  mapping 
  {
    (-2, -2, 2, 2) => (180, 160)
  }

  colors 
  {
    define 
    {
      gradient(361) 
      {
        [255 255 255] [000 000 000] [255 000 255]
      }
    }
  }

  formula 
  {
    c = current;
    z = current;

    // z    = z^3   + c*z^2 - 1;  // the function
    // z'   = 3*z^2 + c*2*z;      // 1st derivative
    // z''  = 6*z   + c*2;            // 2nd derivative
    // 6*z + 2*c = 0;
    // z     = -c/3;

    z = -z/3;

    z1 = 1;
    maxit = [64, 0];
    z2 = [0,0];
    xtot = [0, 0];
    ytot = [0, 0];

    xtmp = [0, 0];
    ytmp = [0, 0];
    trap = [0.1, 0];
    
    while ($count < real(maxit) && sum_of_squares(z1-z) > 1e-24)
    {
      xtmp = [abs(real(z*z)), 0];
      xtmp = xtmp;
      if (real(xtmp) < real(trap))
      {
        xtot = xtot + [(1 - real(xtmp)/real(trap)), 0];   
      }

      ytmp = [abs(imaginary(z*z)), 0];
      ytmp = ytmp;
      if (real(ytmp) < real(trap))
      {
        ytot = ytot + [(1 - real(ytmp)/real(trap)), 0];   
      }
  
      z1 = z;      
      z2 = z*z;
      //z = z - (z2*z - 1)/(3*z2);
      z = z - (z*z2 + c*z2 - 1)/(3*z2 + 2*c*z);
    }

    if(real(xtot) > real(ytot))
    {
      $color = 100+40*sqrt(real(xtot+ytot));
    }
    else
    {
      $color = 100-40*sqrt(real(ytot+xtot));
    }

   set_color($color);
  }
}



