Mandelbrot set

You are encouraged to solve this task according to the task description, using any language you may know.


Task

Generate and draw the Mandelbrot set.



Note that there are many algorithms to draw Mandelbrot set and there are many functions which generate it .




MODULE Mandelbrot EXPORTS Main;
 
IMPORT Wr, Stdio, Fmt, Word;
 
CONST m = 50;
      limit2 = 4.0;
 
TYPE UByte = BITS 8 FOR [0..16_FF];
 
VAR width := 200;
    height := 200;
    bitnum: CARDINAL := 0;
    byteacc: UByte := 0;
    isOverLimit: BOOLEAN;
    Zr, Zi, Cr, Ci, Tr, Ti: REAL;
 
BEGIN
 
  Wr.PutText(Stdio.stdout, "P4\n" & Fmt.Int(width) & " " & Fmt.Int(height) & "\n");
 
  FOR y := 0 TO height - 1 DO
    FOR x := 0 TO width - 1 DO
      Zr := 0.0; Zi := 0.0;
      Cr := 2.0 * FLOAT(x) / FLOAT(width) - 1.5;
      Ci := 2.0 * FLOAT(y) / FLOAT(height) - 1.0;
 
      FOR i := 1 TO m + 1 DO
        Tr := Zr*Zr - Zi*Zi + Cr;
        Ti := 2.0*Zr*Zi + Ci;
        Zr := Tr; Zi := Ti;
        isOverLimit := Zr*Zr + Zi*Zi > limit2;
        IF isOverLimit THEN EXIT; END;
      END;
 
      IF isOverLimit THEN
        byteacc := Word.Xor(Word.LeftShift(byteacc, 1), 16_00);
      ELSE
        byteacc := Word.Xor(Word.LeftShift(byteacc, 1), 16_01);
      END;
 
      INC(bitnum);
 
      IF bitnum = 8 THEN
        Wr.PutChar(Stdio.stdout, VAL(byteacc, CHAR));
        byteacc := 0;
        bitnum := 0;
      ELSIF x = width - 1 THEN
        byteacc := Word.LeftShift(byteacc, 8 - (width MOD 8));
        Wr.PutChar(Stdio.stdout, VAL(byteacc, CHAR));
        byteacc := 0;
        bitnum := 0
      END;
      Wr.Flush(Stdio.stdout);
    END;
  END;
END Mandelbrot.