## 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 z_{n+1} = z_{n}^{2} + c remains bounded. That is, a complex number, c, is in the Mandelbrot set if, when starting with z_{0}=0 and applying the iteration repeatedly, the absolute value of z_{n} 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.

`<?php`

class Mandelbrot

{

var $im;

function colour($value){

$value=$value*ceil(1200/$this->its);

if($value<256){

$r=255;

$g=$value;

$b=0;

}elseif($value<512){

$r=511-$value;

$g=255;

$b=0;

}elseif($value<768){

$r=0;

$g=255;

$b=$value-512;

}elseif($value<1024){

$r=0;

$g=1023-$value;

$b=255;

}elseif($value<1280){

$r=$value-1024;

$g=0;

$b=255;

}elseif($value<1536){

$r=255;

$g=0;

$b=1535-$value;

}

return imagecolorallocate($this->im, $r, $g, $b);

}

function Mandelbrot()

{

$this->im = @imagecreatetruecolor(200, 200);

$this->y1=is_numeric($_GET['y'])?$_GET['y']:0;

$this->x1=is_numeric($_GET['x'])?$_GET['x']:0;

$zoom=is_numeric($_GET['zoom'])?100*$_GET['zoom']:100;

$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++) {

$val=$this->iterate($y/$zoom,$x/$zoom);

imagesetpixel($this->im, $x+100, $y+100, $this->colour($val));

}

}

$d2 = microtime(1);

$diff = $d2 - $d1;

header("Elapsed: $diff");

header('Content-Type: image/png');

imagepng($this->im);

}

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) {

$i++;

$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.

On

March 25th,2009dansaid…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!

Dan

On

March 25th,2009milburnsaid…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.