How Fractals can make Pretty Pictures

Written by Andrew Crowell

Hello! Today I'll give you a brief breakdown on how Math can be used to some fairly generate nifty images.

For starters, it helps to know that all pictures on a computer are formed from tiny colored squares called pixels (which stands for picture element). Each pixel is square in a large rectangular grid. Using this knowledge know, you can map every location on the screen to a pixel on the grid at (x, y).

And next, is the general idea behind fractals, which are these funky shapes split between dimensions. They branch and split repeatedly into smaller pieces at the edges, in what is known as recursive behaviour. Because of their odd branching characteristics, these can be used to mimic complex shapes that occur in real life: such as trees, plant life, mountains, lightning, snowflakes.

Sadly, I'm not quite that artistic when it comes to fractals, so I present to you, the very famous Mandelbrot set.

Some pictures!

Nifty looking, right? The Mandelbrot set, was a discovered by a French mathematician, Benoit B. Mandelbrot, in the year 1975. As far as math goes, fractals are a fairly new thing.

Let's zoom in a bit...

Here's an even deeper zoom...

Now for the super close-up:

...As you can see, some pretty psychedelic images can be formed from fractals, too. Now let's jump into the details about how the image was constructed!

A bit of Color Theory

Firstly, the pictures use the HSV (Hue-Saturation-Value) Color Model to create those nifty thermal effects.

Let me explain:

These fractals were generated by using a simple coloring scheme using the above concept. The closer to the Mandelbrot Set, the greater the Hue (and it cycles when it goes over 360). Any part INSIDE the Mandelbrot Set is colored blue.

Now, the details...

The actual steps taken to generate the picture are a little more... elaborate. Yet, they are here for interest and completeness.

Let n = 100
(The above is the "escape" condition for the image. If it can't find out if a pixel is in the Mandelbrot set after making n attempts, it calls it quits, and says the point must be in the Mandelbrot set.

For more accurate pictures at the cost of speed, you could make this number bigger. For more quickly generated pictures at the sacrifice of quality, you could make this number smaller)

For every pixel within the image, do the following:
Let xpixel = x-coordinate location of the pixel in the image
Let ypixel = y-coordinate location of the pixel in the image
Let xstart = xpixel mapped onto the range [-2, 2]
Let ystart = ypixel on mapped onto the range [-2, 2]
(Note: It doesn't necessarily need to be in the range [-2, 2], but that's where all the Mandelbrot action happens, so you're gonna end up with some pretty boring pictures otherwise.)
i := 0
x := xstart
y := ystart
(These define the starting conditions for the loop that appears on the next line.)
While x2 + y2 <= 4, and i < n, repeat the following steps:
(Note that the <= 4 is the same as <=22, the base of which is the number representing the "interesting" parts of the Mandelbrot set)
xprevious := x
x := x2 - y2 + xstart
y := 2 * xprevious * y + ystart
(The above three lines recursively step through the equation for a complex quadratic equation, which represents Mandelbrot fractals)
End of loop to check if point is in the Mandelbrot Set.

Let the color c = HSV(i * 6, 255, 255)
(This is the code to generate the bright, thermal rainbow glow I have in the pictures. Uses the number of steps multiplied by 6 to figure out what hue to use. This is fairly arbitrary, but it's how most fractal colorizers work to an extent.)

Plot the color c at the location (xpixel, ypixel).
End of loop over pixels.

Phew! That was a mouthful.

With a bit of clever work, you can translate these steps to a computer programming language that has a graphics package. It's a fairly fun exercise to try.

One thing to note though: Drawing fractals can be slow. You need to be patient with your computer if you want to try this, especially if it's not a top-notch one.

So there you go, I've shown you how you can make funky art with a bit of math and a computer.

...So, isn't math exciting? Come on, admit it, part of you thinks that was pretty cool!

There's more?

Well, just one more thing. I wanted to paste my code here, the reasons being that...

  1. ...I'm into programming as much as math, so I spent a bit of time on it, and
  2. ...Someone might be interested!

** You can download this program to play with: http://www.bananattack.com/stuff/FractalMaker.zip

If you're not into code, you can skip over this.

So here's the source code written in Lua, with free game engine called Verge. It essentially does the same as the description above, but it's a runnable script,

function autoexec()
	canvas = v3.NewImage(v3.ImageWidth(v3.screen), v3.ImageHeight(v3.screen))
	v3.RectFill(0, 0, v3.ImageWidth(canvas), v3.ImageHeight(canvas), 0, canvas)
	v3.SetLucent(60)
	Mandelbrot(canvas, 100)
	v3.CopyImageToClipboard(canvas)
	while true do
		v3.Blit(0, 0, canvas, v3.screen)
		v3.ShowPage()
	end
end

function Mandelbrot(dest, max_iterations)
	local width = v3.ImageWidth(dest)
	local height = v3.ImageWidth(dest)
	local color = 0
	local iteration = 0
	local startX, startY
	local x, y, xtemp
	for i = 0, width - 1, 2 do
		for j = 0, height - 1, 2 do
			color = 0
			-- Uncomment these other ones to see them in action!
			--[[ Standard Mandelbrot pic
			startX = -2 + (i / width * 4)
			startY = -2 + (j / height * 4)]]
			--[[ Intesting thing #1
			startX = -1.5 + (i / width * 1)
			startY = -0.5 + (j / height * 2)]]
			--[[ Intesting thing #2
			startX = -0.05 + (i / width * 1)
			startY = -0.015 + (j / height * 1)]]

			--[[ Intesting thing #3]]
			startX = -0.73 + (i / width * 0.01)
			startY = -0.25 + (j / height * 0.01)

			x = startX
			y = startY
			iteration = 0
			while x^2 + y^2 <= 4 and iteration < max_iterations do
				xtemp = x^2 - y^2 + startX
				y = 2 * x * y + startY
				x = xtemp

				iteration = iteration + 1
			end
			color = v3.HSV(iteration * 6, 255, 255)
			color2 = v3.HSV(iteration * 6, 255, 255)
			Plot(i, j, color, color2, dest)
		end
	end
end

function Plot(x, y, color, color2, dest)
	v3.CircleFill(x, y, v3.Random(2, 8), v3.Random(2, 8), v3.MixColor(color, color2, v3.Random(0, 255)), dest)
end

So that's it!

Math is full of intriguing things...