Ideas in the Night

Tag Archives: Mandelbrot set

The Mandelbrot Set is one of the most famous fractals. This fractal carries the name of the French mathematician Benoit Mandelbrot (1924 – 2010). The first ones how implemented the Mandelbrot Set with computer where mathematicians Robert Brooks and Peter Matelski.

The mathematical definition of the Mandelbrot Set

The Mandelbrot Set is set M, where belong all the points c, to which zn is finite, when n approaches infinity, where

z0 = c

zn+1 = zn2 + c

The initial point is (0,0i). Here both z and c are complex numbers. The c is held constant and z is variable that’s value changes.

The Mandelbrot Set on the complex plane

The Mandelbrot Set is typically generated from rectangle on the complex plane from bottom left (-2.25 -1.5i) and upper right (0.75 + 1.5i). The zooming of the Mandelbrot Set is done by selecting different coordinates on this rectangle from somewhere on the border of the set. See the picture below:

The set M is the black area in the picture. The other colors represent the distance from the set. In a way when you’re looking other colors than black, you’re watching infinity.

The helpful proof

Fortunately there’s is mathematical proof, that says that is sufficient to test that is the absolute value of iterated point > 2. If the absolute value of iterated complex number is greater than 2, the iterated point is to divergence to infinity and does not belong to the set. Otherwise the point belongs to the set.

The idea of the generating the Mandelbrot Set

The recursive formula will be implemented by iterative way.

The Mandelbort set is generated to computer screen by taking each pixel point and by choosing that point as constant C (after converting this to correspond the complex plane point). When each iteration loop begins, z is first set to (0 + 0i). Next the formula z = z + c is iterated with checking if z is to divergence to infinity by calculating the absolute value of z. If the absolute value of z > 2, the point is to divergence to infinity and does not belong to the set. Otherwise the point belongs to set.

Imagine each pixel point as a point on the chosen complex plane rectangle.

Pixel’s width and height on the complex plane are calculated as follows:

dr = (maxR - minR) / SCREEN_WIDTH
di = (maxI - minI) / SCREEN_HEIGHT

Other thing to consider is the scale of the picture: The complex plane rectangle’s ratio must be scaled to same as screen area’s ratio.

This is calculated as follows:

The width of the rectangle wmaxR – minR
The height of the rectangle h = maxI – minI
Scaled width sW = h * (SCREEN_WIDTH / SCREEN_HEIGHT)
Difference of the scaled width and original width: diff = sW – w

Next we subtract half of the difference of the widths from the left side of the complex plane rectangle and add half of the difference of the widths to the right side of the complex plane rectangle:

minR = minR – diff / 2
maxR = maxR + diff / 2

Now the length of the complex plane rectangle is scaled to screen’s width. In practice SCREEN_WIDTH can refer in general to width of the screen’s pixel rectangle’s width.

Below is complete BlitzMax program that generates the Mandelbrot set and scales the complex plane rectangle to same ratio as the screen area’s ratio:

Const SCREEN_WIDTH:Int = 640
Const SCREEN_HEIGHT:Int = 480
Const MAX_ITER:Int = 32


 Global minR:Float = -2.25, maxR:Float = 0.75
 Global minI:Float = -1.5, maxI:Float = 1.5

 Global dr:Float, di:Float

 Local w:Float, h:Float, sW:Float, diff:Float
 Local shade:Int

 w = maxR - minR
 h = maxI - minI
 sW = h * (Float(SCREEN_WIDTH) / Float(SCREEN_HEIGHT))
 diff = sW - w

 minR = minR - diff / 2
 maxR = maxR + diff / 2

 dr = (maxR - minR) / SCREEN_WIDTH
 di = (maxI - minI) / SCREEN_HEIGHT

 For Local y:Int = 0 To SCREEN_HEIGHT - 1
  For Local x:Int = 0 To SCREEN_WIDTH - 1
   shade = mandelbrot(x,y)
   shade = 255 - Float(MAX_ITER - shade) / 32.0 * 255.0
   SetColor 0,0,shade
   Plot x,y


Function mandelbrot:Int(x,y)

 Local iterations:Int = 0

 Local cr:Float = minR + x * dr
 Local ci:Float = minI + y * di
 Local zr:Float = 0.0
 Local zi:Float = 0.0
 Local zr_temp:Float = 0.0
 Local zi_temp:Float = 0.0

 While (iterations < MAX_ITER)
  zr_temp = zr * zr - zi * zi + cr
  zi_temp = 2.0 * zr * zi + ci
  zr = zr_temp
  zi = zi_temp

  iterations = iterations + 1
  ' Below is same as sqrt(zr^2 + zi^2) > 2.0
  ' but faster
  If zr * zr + zi * zi > 4.0 Then Return iterations


 Return 0

End Function

With greater amount of iterations it may be the case, that a point that doesn’t diverge to infinity with lower amount of iterations, diverges to infinity with greater amount of iterations. Thus, with computers in fact every generated Mandelbrot set picture is some kind of approximation of the Mandelbrot set.

Shortly about the colors

If the point c belongs to the set, the pixel is usually colored black. The other colors are calculated from the iterations. Easiest way to calculate different shades from the iterations is the following:

shade = mandelbrot(x,y) ; The function returns the number of iterations
shade = 255 - (MAX_ITERATIONS - shade) / MAX_ITERATIONS * 255

This gives values 0…255 to variable shade.

Below is is picture from rectangle, that’s bottom left is (-0.22 – 0.70i) and upper right is (-0.21 – 0.69i) with different kind of coloring:


Here the maximum amount of iterations is 250. In this case the number of iterations has dramatic impact to the generated picture. How colorful can the infinity be!

Mandelbrot mountain

For curiosity below is a Mandelbrot mountain generated with Python program (also my own program):


The program uses the Mayavi extension, which makes it easy to do 3D visualization.

Amazon e-books on fractals

I recommend especially

If you’re interested in generating Mandelbrot or Julia set mountains, I recommend the e-book below: