Mandrian is a Python + Pygame program to render the Mandelbrot Set  by sub-dividing square areas into sub-squares. If the square’s corners all have the same iteration count from the escape function we assume there’s no internal detail to render. We also use the iteration count to select an audio frequency and play a note. Colours are either tied to the iteration count, or are random. The squares may be bordered. The effect is a little similar to Mondrian’s paintings. The program’s name is a portmanteau of Mandelbrot and Mondrian.

The assumption that if the square’s corners all have the same iteration count from the escape function then there’s no internal detail to render is almost always OK, but I’ve seen it break down in rare cases. It can happen that the M-set sneaks in a little bit from one or more sides. I’ll fix this in a future version by adding 4 more checks, each at the sides’ mid points.

I think that my technique is called ‘quad tree’ (QT) in CS circles. The QT algorithm consists basically of subdividing an area (square in my case) into 4 equal sub-areas (squares), and then processing and evaluating them somehow, before potentially repeating the process recursively on each sub-square, if necessary. Except that recursion wasn’t ideal as it’s depth-first and I wanted breadth-first so that the image revealed top-down. So I used a queue instead.

In order to debug and validate my program I added an option to draw borders on the squares, to make it easier to see where the program was doing stuff. That in fact didn’t really help much when the squares got small so next I tried filling the squares with random colours, and that did help. But in doing these things I noticed that the effects were quite pretty, so the debugging code became a feature.


I also thought it might be fun to add some ‘music’. I did this in the obvious way, by taking the escape time iteration count and mapping it to a sound frequency. The result is quite interesting, for a short time anyway.

The program code is available on GitHub with MIT license.

There’s currently no GUI but text prompts for various options:

Enter one of the following, followed on the same line by space and new value: 
side - power to raise 2, for display square => 10 
colOff - colour offset (0-359, 360=random) => 360 
sqrtCol - square root selection of colours => True 
randCol - random selection of colours => False 
line - line thickness (0 = no lines) => 0 
sound - # of points to skip between notes (0=no sound) => 64 
loFreq - lowest frequency of notes > 261.63 
hiFreq - highest frequency of notes (if log or sqrt) => 6000 
octaves - how many octaves (if not log or sqrt) => 4 
scale - logarithmic/square root/linear sound gen => log 
h/s/v - background hue (0-360), saturation or value (0-100) => 0 0 0 
go - go and do it! 
quit - stop program

If you’re not in the USA, and you click through, will (eventually) offer to switch to your country…
Creative Commons Licence This work by Alan Richmond (‘Mandrian’) is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Permissions beyond the scope of this license may be available at

Leave a Reply