This is a simple Python program to display the whole Mandelbrot set. Well, not every point from the infinite set, just a few corresponding to pixels…

The Mandelbrot set is a mathematical set of points whose boundary is a distinctive and easily recognizable two-dimensional fractal shape. The set is closely related to Julia sets (which include similarly complex shapes), and is named after the mathematician Benoit Mandelbrot, who studied and popularized it.

Mandelbrot set images are made by sampling complex numbers and determining for each whether the result tends towards infinity when a particular mathematical operation is iterated on it. Treating the real and imaginary parts of each number as image coordinates, pixels are colored according to how rapidly the sequence diverges, if at all.

The Mandelbrot Set is a mathematical fractal defined by the recursive formula z = z^2 + c, where z and c are complex numbers.

Mandelbrot Set in Python:

This Python program plots the whole Mandelbrot set, making use of some optimisations. MIT License. Available on GitHub.

  • lower half is a reflection of the upper half;
  • the large cardioid and period 2 bulb can be omitted from further tests
__author__ = 'Alan Richmond'
'''
    Mandelbrot.py   Copyright (C) 2014 Alan Richmond (Tuxar.uk)

    Full mandelbrot set with a couple of optimizations:

    1 compute only top half, mirror it.
    2 don't compute set inside the big circle and a couple of smaller ones.
    See http://en.wikipedia.org/wiki/Mandelbrot_set#Cardioid_.2F_bulb_checking
'''
from pygame.locals import *
import pygame

def main():
    width, height = 1000,1000
    screen = pygame.display.set_mode((width,height),DOUBLEBUF)
    xaxis = width/1.5+140
    yaxis = height/2
    scale = 400
    iterations = 50

    for iy in range(height/2+1):
        for ix in range(width):
            z = 0+0j
            c = complex(float(ix-xaxis)/scale, float(iy-yaxis)/scale)
            x=c.real
            y=c.imag
            y2=y*y
            q=(x-0.25)**2+y2
            if not(q*(q+(x-0.25))<y2/4.0 or (x+1.0)**2 + y2 <0.0625):
                 for i in range(iterations):
                     z = z**2+c
                     if abs(z) > 2:
                        v = 765*i/iterations
                        if v > 510:
                            color = (255, 255, v%255)
                        elif v > 255:
                            color = (255, v%255, 0)
                        else:
                            color = (v%255, 0, 0)
                        break
                    else:
                        color = (0, 0, 0)

            screen.set_at((ix, iy), color)
            screen.set_at((ix, height-iy), color)

    pygame.display.update()

    while True:
        event = pygame.event.poll()
        if (event.type == QUIT or
            (event.type == KEYDOWN and event.key == K_ESCAPE)):
            break

if __name__ == "__main__":
    main()
Wikipedia:

Cardioid / bulb checking

One way to improve calculations is to find out beforehand whether the given point lies within the cardioid or in the period-2 bulb. Before passing the complex value through the escape time algorithm, first check that:

,
,
,

where x represents the real value of the point and y the imaginary value. The first two equations determine that the point is within the cardioid, the last the period-2 bulb.

The cardioid test can equivalently be performed without the square root:

3rd- and higher-order buds do not have equivalent tests, because they are not perfectly circular.[1] However, it is possible to find whether the points are within circles inscribed within these higher-order bulbs, preventing many, though not all, of the points in the bulb from being iterated.

  1. ^ "Mandelbrot Bud Maths". 

Leave a Reply