Mandelbrot Set

The Mandelbrot set is an interesting mathematical concept because it is so amazingly simple yet it leads to such astonishingly complex results. I thought it would be interesting to have a go at making my own Mandelbrot set generator. I chose php because I was also interested in looking at optimisation techniques and a Mandelbrot set generator definitely needs all of the optimisations it can get.

Having made the program I have now realised that php is just not cut out for doing stuff like this so it would be a better idea to make it in something like c++ so that I could add more features like being able to smoothly zoom into the picture perhaps, or at least cut down the 25 second rendering time for a 500×500 picture.

First, since the Mandelbrot set uses complex numbers, I logically looked towards procuring a complex number class for php. I found a class pretty quickly but fortunately (yes it isn’t a typo) it wasn’t documented. This meant that I didn’t feel the urge to thoroughly scan the code to work out how to use the thing so I gave up on it saving myself time and bother. Later on in the script when I was optimising I realised that classes are a lot slower than normal variables, I think it would have been over 4 times slower at the lower estimate.

For those of you who don’t know the formula for the Mandelbrot set is z -> z^2 + c. Where z is a complex number which starts as 0 + 0i and c is the point of the complex plane that you are looking at. The formula is repeated large numbers of times and in my program I allowed this value to be changed. If z goes off to infinity (as soon as |z| > 2 you know it will just keep increasing) then the point is coloured white otherwise it is coloured black.

You will now look at my program or many other Mandelbrot set pictures and point out that they are all in pretty colours. The reason for this is that we programmers like pretty pictures, since the Mandelbrot set programs don’t produce anything else, so we look at how many iterations it takes until |z| > 2 and then work out a colour based on this value. My program works in the range from bright yellow to red since I was too lazy to program in a whole spectrum gradient but many other programs will use the full spectrum or something even more exotic.

There isn’t really much more to it than that and the program is pretty short. My PC takes about 25 seconds to render the 500×500 with 100 iterations and I have a p4 3.2 Ghz so everybody should be able to have a go at rendering it if they want to and they have php installed. I haven’t put the php script on my website because I want to keep on good terms with my host and I don’t think locking up the cpu for about 20 seconds will please them.

To use the script put it on your server (preferably on your own PC) and then open the web page. If you want to change the things you are looking at just adjust the constants and variables at the top of the script. They are pretty self explanatory. The iteration number will probably need to be increased if you decide to zoom in but when you aren’t zoomed in it should be kept low to speed things up.

The Code

An image generated my my mandelbrot set.

Leave a Reply