Search code examples
delphicolorssmoothingmandelbrot

Smooth Coloring Algorithm for the Mandelbrot set on Delphi


I have problems using the smooth coloring algorithm. I just don't get them implemented in my Code. This is the main code which causes an error after some calculated pixel rows:

g:=StrToInt(Edit3.Text); //maximum iteration count
for x:=0 to Width do
begin
  for y:=0 to Height do
  begin
    zr:=x*(br-ar)/Width+ar;
    zi:=y*(bi-ai)/Height+ai;
    n:=1;
    zr0:=zr;
    zi0:=zi;
    while (n<g) and (zr*zr+zi*zi<4) do                                      
    begin
      zrh:=zr;
      zr:=zr*zr-zi*zi+zr0;
      zi:=zrh*zi+zi*zrh+zi0;
      Inc(n) //iterations
    end;
    n:=Round(n+1-(log2(log2(sqrt(zr*zr+zi*zi))/log2(4)))); //<-- this should smoothen the iterations
    Draw_Pixels(n,g,x,y,Image1.Canvas)
    end
  end;
end;

Henry


Solution

  • If you ever end up with zr == zi == 0, you'll be trying to take log2(0), which is not defined (-inf as a limit).

    If zr*zr+zi*zi is ever equal to or less than one, the inner log2 will return 0 or a negative value, which will break the outer log2 (can't take the log of a negative number as long as you're dealing with reals).

    (And I don't think that will scale smoothly for values of zr*zr+zi*zi slightly over 1.)