Mandelbrot Live Implementation (alt)

Published 2012-07-31
Hey Youtube,

A user had requested me to make a mandelbröt implementation. Well actually the request was to make a video about fractal generators where the user could input a formula and the computer would render whatever it would result in. But I took the liberty to reinterpret the request and I think this video might be more entertaining.

This is me programming the mandelbröt fractal renderer. I made a few mistakes along the way, all corrected within the video. There are a few things left to be said and improved upon however. This is just a really basic implementation.

To square a pair of numbers that are added, the computation is as follows:
(a+b)^2 = a*a+a*b+b*a+b*b = a*a+2*a*b+b*b = a^2+b^2+2*a*b.
With complex numbers it's the same, only i*i = -1. bi denotes b*i:
(a+bi)² = a*a+a*bi+bi*a+bi*bi = a*a+2*a*bi+b*b*i*i = a*a+2*a*bi-b*b = a² - b² + 2*a*bi.
So as a tuple, a complex number z to be squared results in the following:
z = (a, b) makes z² = (a*a-b*b,2*a*b) = (a²-b²,2*a*b).

I chose the colour scheme because I know these images can become very monochromatic with ill formed parameters. The hsv_to_rgb takes the colour space
hue saturation value and translates it to reg green blue. By setting the hue to be the iterations indicator we get a transition through bright colours preventing this problem.

Using this code you can make any image size along with any zoom level and region you'd like, in theory. Well, not entirely. Because the speed improvement involves mirroring the image around the y=0 axis you can only render images with the y=0 center line. (any other region will be computed however, it too will be mirrored around the middle line, which may not be y=0 and therefore wrong). Removing this improvement will allow you to render other regions freely.
As I've found out, rendering 3200×2400 with 10000 iteration depth will take a loooong time. Even with the additional speed improvements of orbit detection and the initial major bulb detection.

Speaking of which, here's the bulb detection from wikipedia:
q = (x-¼)² + y²
q = q(q+x-¼)-¼y²
and then test if q is smaller than 0. If it is, the initial point z = x + yi = (x,y) is in the major bulb. This test should be added to the "compute" function before the iterations are computed. In case q is less than 0, return 0. Or 1. Depends on your colouration etc.
The condition above can also be written as testing if this:
-3 + 32x + 256(x²)² - 96y³ + 256(y²)² + 32(-3 + 16*y²)x²
is smaller than 0. I used something like this last computation in my more optimized version. Why this instead of the wikipedia one? Well this one doesn't involve division. Simple as that. In python it doesn't actually matter all that much I think but this one is, I think, more aesthetically pleasing.

I also pointed something out about how if |z| is larger than 2 it would go to infinity or something to that extent. The most obvious test would be to see if sqrt(x²+y²) is less than 2 if z = x + yi. Instead, test if x² + y² - 2² is less than 0. It takes away the need for the sqrt, which takes waay more effort than simple multiplications.

To be fair, I want to say that I've not looked up any information on mandelbröt fractals immediately prior to making this video. The last time I got info on this was in 2006 when it was part of a course to write something like this program. I've done it all form memory and experience and I'm quite pleased with the results.

With the new additional info I've written a more efficient program, though I don't really care lol.

Also, the youtube description doesn't allow me to use the "less than" and "greater than" symbols. Hence the textual versions.

Fracfap, I hope you'll enjoy this video. I enjoyed making it :D

All Comments (3)