program mset; {Produces a full-sized M set in Turbo Pascal}
uses crt, graph;

var     xcorner, y2corner, x2corner, ycorner: real;
        xsize, ysize, k, i, xgap, ygap, max: integer;
        ca, cb, za1, za2, zb: real;
        gd, gm: integer;
        count: word;

procedure iterate;  {The heart of the program - performs the calculations
                    necessary to determine the colour}
begin
count := 0;
za1 := 0;
za2 := 0;
zb := 0;
while 1 = 1 do
begin
za1 := za2;
za2 := (za1 * za1 - zb * zb) + ca;
zb := za1 * zb * 2 + cb;
iF za2 * za2 + zb * zb > 4 THEN EXIT;
count := count + 1;
if count > max then EXIT;
end;

END;





procedure main;
begin
gd := Detect;
InitGraph(gd, gm, 'd:\tp\bgi');
xcorner := -2.2;    {corners of the full-sized M set}
y2corner := 1.4;
x2corner := 0.6;
ycorner := -1.4;
xsize := 400;       {pixel size}
ysize := 400;
xgap := 0;
ygap := 0;
max := 100;   {maximum count before stopping iteration -
              a high count is better for high magnifictions but for the
              full sized M set it can be quite small}


FOR k := ysize downTO 1 do
FOR i := 1 TO xsize do
begin
ca := (i / xsize) * (x2corner - xcorner) + xcorner;
cb := (k / ysize) * (y2corner - ycorner) + ycorner;
iterate;
if count > max then count := 0;
setcolor(count);
LINE (xgap + i, ygap +ysize- k, xgap + i, ygap +ysize- k);
end;
repeat until keypressed;


closegraph;
END;


begin
main;
end.
