Mandelbrot Pictures

I remember being fascinated with Mandelbrot pictures ( and their theory ) in the early 1980s before venturing into fractals and later developing a 3D Terrain Modelling application, using fractals to “roughen up” the surface, as part of the Medusa CAD system portfolio.

On a recent TV program there was mention of a series of Mandelbrot pictures which rekindled my interest.

What’s it all about ?

The Mandelbrot set is an example of a fractal. It is a structure with an infinite amount of fine detail, you can zoom in on the edge of the fractal forever, and it will continue to reveal ever-smaller details.

The Mandelbrot calculation is to iterate the equation

z = z*z + c

where z and c are complex numbers,

z is initially zero,

c is the coordinate of the point being tested.

Here’s an example program from

http://newt.phys.unsw.edu.au/~mcba/phys2020/notes/mandelbrot.html
written in C++:


/*
  A program to generate an image of the Mandelbrot set. 

  Usage: ./mandelbrot > output
         where "output" will be a binary image, 1 byte per pixel
         The program will print instructions on stderr as to how to
         process the output to produce a JPG file.

  Michael Ashley / UNSW / 13-Mar-2003
*/

const double xCentre = -0.75;
const double yCentre = +0.0;
const int    nx      = 400;
const int    ny      = 400;

const double dxy     = 0.005;

#include <stdio.h>
#include <limits.h>

int main() {

    double cx, cy;
    double zx, zy, new_zx;
    unsigned char n;
    int i, j;

    // The Mandelbrot calculation is to iterate the equation
    // z = z*z + c, where z and c are complex numbers, z is initially
    // zero, and c is the coordinate of the point being tested. If
    // the magnitude of z remains less than 2 for ever, then the point
    // c is in the Mandelbrot set. We write out the number of iterations
    // before the magnitude of z exceeds 2, or UCHAR_MAX, whichever is
    // smaller.

    for (j = 0; j < ny; j++) {
      cy = yCentre + (j - ny/2)*dxy;
      for (i = 0; i < nx; i++) {
        cx = xCentre + (i - nx/2)*dxy;
        zx = 0.0; 
        zy = 0.0; 
        n = 0;
        while ((zx*zx + zy*zy < 4.0) && (n != UCHAR_MAX)) {
          new_zx = zx*zx - zy*zy + cx;
          zy = 2.0*zx*zy + cy;
          zx = new_zx;
          n++;
        }
        write (1, &n, sizeof(n)); // Write the result to stdout
      }
    }

    fprintf (stderr, "To process the image: convert -depth 8 -size %dx%d gray:output out.jpg\n",
       nx, ny);
    return 0;
}



If the magnitude of z remains less than 2 for ever, then the point c is in the Mandelbrot set. We write out the number of iterations before the magnitude of z exceeds 2, or UCHAR_MAX, whichever is smaller.

(UCHAR_MAX is the maximum value that an unsigned char object can store, which is 255, although the actual value depends on the compiler architecture or library implementation.)

A list of different algorithms is available at

Plotting algorithms for the Mandelbrot set (Wikipedia)

Here’s a simple implementation in Javascript


<html>
<body>
        <canvas id="myCanvas" width="800" height="800"></canvas>
        <script>
        var canvas = document.getElementById('myCanvas');
        var context = canvas.getContext('2d');
 
        for(var x=0;x<200;x++)
        {
                for(var y=0;y<200;y++)
                {
                        var i=0;
                        var cx=-2+x/50;
                        var cy=-2+y/50;
                        var zx=0;
                        var zy=0;                        
 
                        do
                        {
                                var xt=zx*zy;
                                zx=zx*zx-zy*zy+cx;
                                zy=2*xt+cy;
                                i++;
                        }
                        while(i<255&&(zx*zx+zy*zy)<4);
 
                        var color=i.toString(16);
                        context.beginPath();
                        context.rect(x*4, y*4, 4, 4);
                        context.fillStyle ='#'+color+color+color;
                        context.fill();
                }
        }
        </script>
</body>
</html>

Details at

The Mandelbrot set in JavaScript

Many other ready-built programs are also available to create images.
Here’s some samples

Useful Links

Here are some useful links I came across

https://sciencedemos.org.uk/mandelbrot.php

Mandelbrot Explorer