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 .




BEGIN
    REAL XCENTRE, YCENTRE, WIDTH, RMAX, XOFFSET, YOFFSET, PIXELSIZE;
    INTEGER N, IMAX, JMAX, COLOURMAX;
    TEXT FILENAME;
 
    CLASS COMPLEX(RE,IM); REAL RE,IM;;
 
    REF(COMPLEX) PROCEDURE ADD(A,B); REF(COMPLEX) A,B;
        ADD :- NEW COMPLEX(A.RE + B.RE, A.IM + B.IM);
 
    REF(COMPLEX) PROCEDURE SUB(A,B); REF(COMPLEX) A,B;
        SUB :- NEW COMPLEX(A.RE - B.RE, A.IM - B.IM);
 
    REF(COMPLEX) PROCEDURE MUL(A,B); REF(COMPLEX) A,B;
        MUL :- NEW COMPLEX(A.RE * B.RE - A.IM * B.IM,
                           A.RE * B.IM + A.IM * B.RE);
 
    REF(COMPLEX) PROCEDURE DIV(A,B); REF(COMPLEX) A,B;
    BEGIN
        REAL TMP;
        TMP := B.RE * B.RE + B.IM * B.IM;
        DIV :- NEW COMPLEX((A.RE * B.RE + A.IM * B.IM) / TMP,
                           (A.IM * B.RE - A.RE * B.IM) / TMP);
    END DIV;
 
    REF(COMPLEX) PROCEDURE RECTANGULAR(RE,IM); REAL RE,IM;
        RECTANGULAR :- NEW COMPLEX(RE,IM);
 
    REAL PROCEDURE MAGNITUDE(CX); REF(COMPLEX) CX;
        MAGNITUDE := SQRT(CX.RE**2 + CX.IM**2);
 
    BOOLEAN PROCEDURE INSIDEP(Z); REF(COMPLEX) Z;
    BEGIN
        BOOLEAN PROCEDURE INSIDE(Z0,Z,N); REAL N; REF(COMPLEX) Z,Z0;
            INSIDE := MAGNITUDE(Z) < RMAX
                AND THEN N = 0 OR ELSE INSIDE(Z0, ADD(Z0,MUL(Z,Z)), N-1);
        INSIDEP := INSIDE(Z, NEW COMPLEX(0,0), N);
    END INSIDEP;
 
    INTEGER PROCEDURE BOOL2INT(B); BOOLEAN B;
        BOOL2INT := IF B THEN COLOURMAX ELSE 0;
 
    INTEGER PROCEDURE PIXEL(I,J); INTEGER I,J;
        PIXEL := BOOL2INT(INSIDEP(RECTANGULAR(XOFFSET + PIXELSIZE * I,
                                              YOFFSET - PIXELSIZE * J)));
    PROCEDURE PLOT;
    BEGIN
       REF (OUTFILE) OUTF;
       INTEGER J,I;
       OUTF :- NEW OUTFILE(FILENAME);
       OUTF.OPEN(BLANKS(132));
       OUTF.OUTTEXT("P2");        OUTF.OUTIMAGE;
       OUTF.OUTINT(IMAX,0);       OUTF.OUTIMAGE;
       OUTF.OUTINT(JMAX,0);       OUTF.OUTIMAGE;
       OUTF.OUTINT(COLOURMAX,0);  OUTF.OUTIMAGE;
       FOR J := 1 STEP 1 UNTIL JMAX DO
       BEGIN
           FOR I := 1 STEP 1 UNTIL IMAX DO
           BEGIN
               OUTF.OUTINT(PIXEL(I,J),0);
               OUTF.OUTIMAGE;
           END;
       END;
       OUTF.CLOSE;
    END PLOT;
 
    XCENTRE := -0.5;
    YCENTRE := 0.0;
    WIDTH := 4.0;
    IMAX := 800;
    JMAX := 600;
    N := 100;
    RMAX := 2.0;
    FILENAME :- "out.pgm";
    COLOURMAX := 255;
    PIXELSIZE := WIDTH / IMAX;
    XOFFSET := XCENTRE - (0.5 * PIXELSIZE * (IMAX + 1));
    YOFFSET := YCENTRE + (0.5 * PIXELSIZE * (JMAX + 1));
 
    OUTTEXT("OUTPUT WILL BE WRITTEN TO ");
    OUTTEXT(FILENAME);
    OUTIMAGE;
 
    PLOT;
END;