1

I have problems using the smoot 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

Henry
  • 65
  • 6

1 Answers1

3

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.)

Mat
  • 202,337
  • 40
  • 393
  • 406
  • I've now logged the variables on a Memo. I added them after every calculation. The error appears at Point(2|320), where n jumps from 7 to 300 and zi turns zo 0. Why does zi turn to 0 suddenly? – Henry Sep 03 '11 at 09:02
  • You're dealing with a fractal function. Things change without a lot of predictability, so "why" it goes to 0 doesn't really have an answer (when you take limited precision arithmetic into account). It's also unimportant here, either `zr` or `zi` can be zero with no issue, but you can't put a complex with a norm < 1 in your smoothing function. – Mat Sep 03 '11 at 09:09
  • Well, but how do I get smooth colors now? – Henry Sep 03 '11 at 09:16
  • That's an entirely different question. The [Mandelbrot](http://en.wikipedia.org/wiki/Mandelbrot_set#Continuous_.28smooth.29_coloring) page on Wikipedia has some algorithms for that. – Mat Sep 03 '11 at 09:18
  • This actually was my question, because I fail on implementing these smooth coloring algorithms... – Henry Sep 03 '11 at 09:21
  • I don't know _what_ smoothing algorithm you're trying to implement, so I can only tell you what is wrong with the code you posted. – Mat Sep 03 '11 at 09:24
  • This site describes nearly the algorithm I've used: http://linas.org/art-gallery/escape/escape.html (Chapter 'The Revised Algorithm') – Henry Sep 03 '11 at 09:45
  • You just need to handle the case where you reached the maximum iterations without reaching the escape radius (i.e. your point is "in the set" or close to that). In that case, you should use a fixed color (black in the page you linked to). The formula used is only valid if you have reached (or exceeded) the escape radius. – Mat Sep 03 '11 at 09:54
  • Thanks, the error disappeared. Though the colors seems to be equal: http://www.imagebanana.com/view/ty8dvgkt/Clipboard01.png I have changed the code to 'if (n – Henry Sep 03 '11 at 10:59
  • No, Henry, that wasn't actually your question. You neglected to ask a question, and when you do that, everyone else just assumes you want to solve the error you mention. If you want to know how to get smooth colors, then post a new question and ask that specifically: *How do I get smooth colors in my Mandelbrot display?* – Rob Kennedy Sep 03 '11 at 21:05
  • I don't know why, but after a few hours of trying the color gradients are all smooth :) – Henry Sep 04 '11 at 10:53