Program to draw the Mandelbrot set

Remember the definition:
The Mandelbrot set is the set M of complex numbers c=x+iy for which the series
{ z0=0 , z1=c , ... , zn+1=(zn)2 + c , ... } is bounded.

The Borland/Turbo Pascal program below shows how this is used to generate those well-known colourful images of fractals!

Take every point (x,y) in a defined rectangle (the display screen);
calculate successive iterations: z1=x+iy, ..., zn+1=(zn)2+z1, ... until either:

In the first case, the series diverges, the point (x,y) is outside of the set;
in the second case, the series is probably bounded (the test prevents the program from looping forever for points inside the set).

In both cases, the point (x,y) is plotted with colour number n.
program fractal;
uses graph;
var
   grdriver,grmode,xs,ys,iter:integer;
   xc,yc,x,y,x0,mag:single;
begin
   grdriver:=detect;
   initgraph(grdriver,grmode,'bgi');
   for ys:=239 downto 0 do begin
      yc:=ys/160;
      for xs:=0 to 639 do begin
         xc:=xs/160-2.5;
         x:=xc;y:=yc;iter:=0;
         repeat
            x0:=x;
            x:=sqr(x)-sqr(y)+xc;
            y:=2*x0*y+yc;
            mag:=sqr(x)+sqr(y);
            inc(iter)
         until ((mag>4) or (iter=16));
         putpixel(xs,240-ys,iter);
         putpixel(xs,240+ys,iter)
      end
   end;
   readln;closegraph
end.

Note that the program expects to find a graphic-driver (vga.bgi) in the subdirectory 'bgi'.

There are many more sophisticated programs available, the most popular is FRACTINT.
That program does not only allow to generate images of the Mandelbrot set, but all possible types of fractals.


1996-08
Your comments are welcome! Mail to: Patrick Hahn (phahn@vo.lu)
Homepage: http://www2.vo.lu/homepages/phahn/default.htm