Fractals

I love fractals! The maths is is not complex* and yet simplicity can create such beauty. Fractal geometry is basically the maths of describing shapes. The essence of the idea is that you can define apparently highly complex patterns (like shapes) with relatively simple mathematical expressions. To generate images as below, typically a simple function (or formulae) is applyed repeatedly to each x,y co-ordinate of a grid of numbers (forming the canvas). After each calculation, the result is fed back in to the function. This is done a set number of times and depending on the final value, a colour is applied to that point of the canvas.

* Mathematical irony fans, that was for you.

I made these pictures with a program I wrote in Perl, basically to prove that I could. Many have gone before me and done it much better. Take, for example my mate Mat Holton who wrote a brilliant Java application for his CS BSc project. OK, enough bigging-up Mat. More about Perl and fractals… OK, how does this work? The basic algorithm used here was devised by Benoit Mandelbrot and does it’s work on a plane of complex numbers. You might start to feel a bit lost or freaky in a moment but hang in there, it really is quite simple… This plane of complex numbers is basically a big grid of sums where each ‘cell’ in the grid holds a value that is the sum of a ‘real’ number and an ‘imaginary’ number. Imaginary numbers are real numbers multiplied by i, which is the square root of -1. I know what your thinking… You can’t make a square root of a negative number! Well with a bit of jiggery-pokery, that is way beyond my understanding, you can. so there. Anyway, it turns out that these complex numbers are pretty usefull in all sorts of situations (usually engineering). What do we do with each of these complex numbers in a big grid? We bung them through Mandlebrot’s equation! Each complex number is depicted by z in this algorithm. z0 is the starting value whilst z is the current value. This current value of Z gets repeatedly run through the equation and as it does so, the current value changes. The actual equation I used in my programs was: z = z² + z0 Each cell in our grid (i.e. values of z in our plane of complex numbers) gets put through this equation a specified number of times (e.g. 300). For some starting values of z, it will change but always remain very small (less than 2). For other starting values, it will get big, small and big again untill eventually it goes larger than 2 and it heading off to become a very big number indeed. Basically, you can count the number of times (and stop somewhere around 300 itterations) the starting value for z can go through the equation before it gets big (goes to infinity) and depending on how long it lasts, plot it’s position in the grid as a colour. and there you go. The picture drawing is handled by creating a bitmap where each cell in the grid (plane of complex numbers) is a pixel on the bitmap. The program starts at the top left corner with a certain value of z corresponding to the topleft corner of the grid defined by the user. It runs the calculation and checks to see how far through the maximum number of itteratons (specified by the user) the cell got before its value of z got larger than 2. The colouring was (sometimes) as follows:

When it has done one pixel, it moves on to the next in the row and does the same for each row of cell/pixels. Ok, so have a go yourself… If you are running Unix or Linux then you should have Perl installed by default (but you may have to change the first line of the script to reflect the true path to your version). If you are a Windows user, why not download the Activestate version of perl here. If you are running anything less than Win 2000 it can be a PITA but persevere, it WILL be worth it. I also understand that there is a Mac version of the Perl interpreter but I have no experience of this and hence have never tested these programs on this distribution. If you really can’t be arsed to download and install Perl then this compiled executable may work for you (if you have Win 2000)… OK. Now if you have perl, you just need to make sure you have the GD and Math::complex module installed. This is pretty simple stuff, if you are using Unix or Linux then go to the Comprehensive Perl Archive Network and follow the instructions from there or if you are a Windows user, open a command/MSDos prompt and type ppm. You can then type “help” to get the list of commands. Something like “search GD” followed by “install GD” should do you for the GD module and similarly, “search complex” etc should do you for the Math::complex module. Also, if you want to start programming in Perl, I would recomend Learning Perl as a very good start… Enjoy. If you have Perl installed (and the gd.pm and math::complex.pm modules) you can save and run the following scripts (Right-click and save the target/link): Download script for Mandelbrot coloured according to the final value of the complex number. Download script for Mandelbrot coloured according to the proportion of itterations before the complex number is bigger than 2. When you start the program, it asks for some paramters that enable you to change the section of the mandelbrot set that is calculated and vary the resolution of the finished image. Follow the instructions carefully. The image is drawn and placed in the same directory as you run the program from… The picture below may help you to work out which bit you will be plotting. The Real numbers are the x-axis and the imaginary numbers are the y-axis. The most exciting bit (sad I know!) is between the points that don’t go to infinity (the bug) and the bits that do go to infinity (or bigger than 2 anyway).