# Mandelbrot

The next demonstration program shows the Mandelbrot set, an extensively studied example of a fractal. A small amount of code with a lot of iteration produces an intricate, fascinating result. According to Wikipedia, Mandelbrot himself defined a fractal as "a rough or fragmented geometric shape that can be split into parts, each of which is (at least approximately) a reduced-size copy of the whole." The mathematics behind the Mandelbrot set is not trivial and is covered in some detail in another page of Wikipedia. If you can understand it all without too much difficulty then you have a future as a mathematician! In the section entitled For programmers, an algorithm similar to that used by program Mandelbrot is explained. Each pixel in the image is examined to see if it is in the Mandelbrot set. If a pixel is not in the set, its colour depends on the number of iterations required to rule it out of the set. The self-similarity of parts of the Mandelbrot set is demonstrated impressively by zooming, as shown on the Wikipedia page.

In Windows Vista, the following result was obtained until the window was minimised then maximised. You are strongly advised to copy the program into Lazarus and run it for yourself to see the complete image in detail. Incomplete image of Mandelbrot Set

We have adapted the code for use in applets using Oxygene for Java.

```program Mandelbrot;
{\$mode objfpc}{\$H+}
{Based on Merlin's Delphi Forge
http://www.delphifaq.net/how-to-draw-a-mandelbrot-fractal-on-the-forms-canvas/}
uses
Graph;

procedure DrawMandelbrot;
var
iNewColor, iMaxX, iMaxY  : integer;
Gd, Gm, iI, iJ  : smallint;
rU, rV, rX, rY, rZ : real;
begin
Gd := Detect; //Graph driver becomes zero
Gm := 0;      //Graph mode will be set automatically because Gd = 0;
InitGraph(Gd, Gm, '');
iMaxX := GetMaxX;
iMaxY := GetMaxY;
for iI := 0 to iMaxX - 2 do
begin
for iJ := 0 to iMaxY - 2 do
begin
//Centre and scale along real (x) axis
rX := -0.85 + 3 * iI / iMaxX;
//Centre and scale along imaginary (y) axis
rY := -1.2 + 2.4 * iJ / iMaxY;
iNewColor := 0;
rU := 0;
rV := 0;
repeat
rZ := Sqr(rU) - Sqr(rV) - rX;
rV := 2 * rU * rV - rY;
rU := rZ;
inc(iNewColor);
until (Sqr(rU) + Sqr(rV) > 4) or (iNewColor = 1000);
if iNewColor = 1000 then
iNewColor := black;
PutPixel(iI + 1, iJ + 1, iNewColor);
end;
end;
end;

begin
writeln('When the graphics window opens, you may need to restore down or' +
' minimise'#13#10'the window then maximise it ' +