On February 7th, 2009 milburn wrote…

Exploring Fractals

Fractals are some of the most intriguing shapes. The Mandelbrot set is one of the most intriguing fractal sets. To be honest with you I don’t really get what it represents and why it has such a weird shape but I know that it is intriguing to see.

For those mathematical boffins the Mandelbrot set can be defined as the set of complex values of c for which the orbit of 0 under iteration of the complex quadratic polynomial zn+1 = zn2 + c remains bounded. That is, a complex number, c, is in the Mandelbrot set if, when starting with z0=0 and applying the iteration repeatedly, the absolute value of zn never exceeds a certain number (that number depends on c) however large n gets.

I decided to create a PHP script which could visualise the Mandelbrot set for me. With a bit of research, I found some basic code which I modified to make the script. You are free to use and distribute it as you wish. The script does work but It is isn’t perfect so use at your own risk.

class Mandelbrot
var $im;
function colour($value){
return imagecolorallocate($this->im, $r, $g, $b);
function Mandelbrot()
$this->im = @imagecreatetruecolor(200, 200);
$this->its=(is_numeric($_GET['its']) and $_GET['its']<=1200 and $_GET['its']>0)?$_GET['its']:100;
$d1 = microtime(1);
for ($y = -100; $y < 100; $y++) {
for ($x = -100; $x < 100; $x++) {
imagesetpixel($this->im, $x+100, $y+100, $this->colour($val));
$d2 = microtime(1);
$diff = $d2 - $d1;
header("Elapsed: $diff");
header('Content-Type: image/png');
function iterate($x,$y)
$cr = $y+$this->y1;
$ci = $x+$this->x1;
$zi = 0.0;
$zr = 0.0;
$i = 0;
while ($i < $this->its and $zi2 + $zr2 < 4) {
$temp = $zr * $zi;
$zr2 = $zr * $zr;
$zi2 = $zi * $zi;
$zr = $zr2 - $zi2 + $cr;
$zi = $temp + $temp + $ci;
return $i;
$m = new Mandelbrot();

The script works in a simple way. It creates a blank canvas of 200×200 pixels. For each pixel it uses the escape time algorithm to check if a point is within the set or not. A value is then returned for how long it took a point to reach the escape condition. Some points take millions of cycles to reach the escape condition so we set a maximum iteration limit. This defines how much detail we see in the mandelbrot set.

This value is used to define the colour of the point. Instead of using value to define the brightness of the point I use the value to define the hue of the point as there are many more possible hues than possible greyscale values. To convert a value into an RGB value needed to plot the point I use a special algorithm. The image above roughly shows how the RGB values are worked out.

I have made the script take 4 input parameters, the x and y coordinates the amount of zoom and the maximum number of iterations allowed. By messing around with these paarmeters you can explore the mandelbrot set in stunning detail.

The outputted images are simply amazing. The image to the left is found at a magnification of 200,000. Its a miniature replica of the Mandelbrot set.

To show you just how much magnification you can get watch the following animation. It goes from a magnification of 1 to 1 billion. With a little more processing time you could easily go much deeper to many billions of billions of times magnification!

I would put the Mandelbrot explorer script online however it is very computationally intense. It takes around 5 seconds to generate 1 image.

Important Update: You can now explorer the mandelbrot set!

You can leave a comment, or trackback from your own site.

2 Responses to “Exploring Fractals”

  1. On March 25th, 2009 dan said…

    Tom – I got to your site from your Drop-Caps plug-in, I’ll try it for a while, it offers a nice touch. While here, I noticed the Fractals! I may still have the Scientific American article I read way back when, I remember writing a Pascal program to display them on the screen. That was with an early IBM PC (actually a Heathkit, which I built). I like your three color rendering scheme, a simple three phase construction, elegant. I wonder what shifting them to sine waves instead of the straight ramps would do to the 1. processing time & 2. the rendition of the image.

    Thanks, for sharing!

  2. On March 25th, 2009 milburn said…

    Hi Dan,
    Thanks for your comment! Yeh I came across the idea of using a false colour spectrum from a wikipedia article. I’m not sure what the appearance of using sine waves would look like but I know that the processing time would increase quite significantly (at least in the way I have implemented it). Performance could certainly be improved if the colours were precomputed and placed in a lookup table. Really I should also plot the number of iterations on a logarithmic scale so that there is a better colour gradient.

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Proudly powered by WordPress with plane theme by me! | © 2007 - 2023 Thomas Milburn | Valid CSS and HTML