C Code for Mandelbrot Set



Here is some code for generating the Mandelbrot set in C, so if you've got a C compiler, why not give it a go.


#include "stdio.h"

#define MaxIters 200
#define SIZE     80
#define BLACK    -1
#define LEFT     -2.0
#define RIGHT    1.0
#define TOP      1.0
#define BOTTOM   -1.0

main(int argc, char *argv[])
{
    short   x, y, counter;
    long double zr, zi, cr, ci
    long double rsquared, isquared;

    for (y = 0; y < SIZE; y++)
    {
        for (x = 0; x < SIZE; x++)
        {
            zr = 0.0;
            zi = 0.0;
            cr = LEFT + x * (RIGHT - LEFT) / SIZE;

            ci = TOP + y * (BOTTOM - TOP) / SIZE;
            rsquared = zr * zr;
            isquared = zi * zi;

            for (counter = 0; rsquared + isquared <= 4.0
                            && counter < MaxIters; counter++)
            {
                zi = zr * zi * 2;
                zi += ci;

                zr = rsquared - isquared;
                zr += cr;

                rsquared = zr * zr;
                isquared = zi * zi;
            }

            if (rsquared + isquared <= 4.0)
                printf("*");    /* In the set. */
            else
                printf(" ");    /* Not in the set. */
        }
        printf("\n");
    }
    return 0;


Copyright © 1997 Cygnus Software. All rights reserved.
Many thanks to the above for the code which was in the Fractal
Extreme help file.


Cygnus Software's Site



Back to Fractals Pages



Back to Home



E-Mail Me