Fractals are complicated images, whose parts are similar to the whole. In other words, if you zoom into a fractal, you can get a picture similar to the one you started with. Keep zooming in and again you'll get a similar picture. And so on - to infinity!
This tutorial will let you quickly (only about 16 lines of code!) create the famous fractal called the Mandelbrot set.
Here is some basic math behind the concept. Feel free to skip the gray section if you're more interested in the code than in the math.
A point in the complex plane belongs to the Mandelbrot set if the orbit of 0 under interation of the quadratic map:
zn=z2n+1
remains bounded (does not escape to infinity).
It can be plotted on a square image between -2-2i and 2+2i.
For practical reasons, we'll only iterate 255 times for each point. We will only iterate a given point as long as the absolute value of zn is lower than 2. If it's 2 or greater, we know it will eventually escape to infinity, so we don't have to continue the iterations. The number of iterations determines the point's color.
It all boils down to the following:
0. Select the point in the bottom left corner of the region (-2,-2)
1. Start with zx=0 and zy=0
2. Calculate:
xt=zx*zy
zx=zx*zx-zy*zy+cx
zy=2*xt+cy
These calculations are really the formula zn=z2n+1. The reason they look different is because i2=-1, a property of complex numbers.
3. Repeat step 2 if:
a. you haven't reached 255 iterations yet [Do Until (i = 255)]
AND
b. the absolute value of the result is lower than 2 [(zx * zx + zy * zy) >= 4]
4. change the color of the point you're calculating (ie. cx,cy) to the color corresponding to the number of iterations (the number of times you repeated steps 2 and 3)
5. Select the next point (go by columns and rows)
6. Go to step 1, until you reach the bottom right corner
Here's the actual code:
For y = 1 To 200
For x = 1 To 200
i = 0
zx = 0
zy = 0
cx = -2 + x / 50
cy = -2 + y / 50
Do Until (i = 255) Or (zx * zx + zy * zy) >= 4
xt = zx * zy
zx = zx * zx - zy * zy + cx
zy = 2 * xt + cy
i = i + 1
Loop
Cells(y, x).Interior.Color = VBA.RGB(10, 10, i * 10)
Next x
Next y
For beginners:
- copy the code above
- open Excel, press Alt+F11 to start Visual Basic
- doubleclick on Sheet1 and select "Worksheet"-"Activate"
- paste the code and hit F5 to execute it
Your screen should look like this:
If your picture does not fit on the screen, zoom out to about 10% (depending on your screen resolution).
The XLSM file is also available HERE. Download it and open with Excel. You have to enable macros.
It works and I think it looks really cool - a relatively simple math formula creates something infinitely complicated to the human eye. But it's just the beginning! The real beauty of Mandelbrot only becomes apparent as you apply colors and zoom inside them to explore its endless lakes and caves. This will be covered in a new tutorial soon.
For now you can just experiment with different values in these two lines:
cx = -2 + x / 50
cy = -2 + y / 50
For example these values:
cx = -0.68 + x / 1200
cy = -0.74 + y / 1200
result in this image:
Here's the code again, this time with more explanations (in case the above wasn't sufficient):
variables:
i - number of iterations
x,y - screen coordinates (row and column) of the point currently being iterated. (0,0) is in the upper left corner; (200,200) in the bottom right.
cx,cy - coordinates of the point currently being iterated, on the complex plane - corresponding to x and y.(-2,-2) is in the bottom left corner; (2,2) in the upper right.
For y = 1 To 200
For x = 1 To 200
The two main loops - we divide our region into 200 rows and 200 columns and work on each point.
i = 0
zx = 0
zy = 0
cx = -2 + x / 50
cy = -2 + y / 50
Convert screen coordinates into a complex number.
Do Until (i = 255) Or (zx * zx + zy * zy) >= 4
Iterate the formula until you reach 255 iterations or the absolute value is greater than 2.
xt = zx * zy
zx = zx * zx - zy * zy + cx
zy = 2 * xt + cy
i = i + 1
Loop
Cells(y, x).Interior.Color = VBA.RGB(10, 10, i * 10)
Set the 'blue' component of the cell color to the number of interations (times ten for visual emphasis).