[note on 07 April 2018: I wrote this in late September 2017 but never published it. I think it’s not finished but useful up until that point so I’m publishing it now]

This is a deeper dive into the Mandelbrot exercises in Chapter 3 of The Go Programming Language (Addison-Wesley Professional Computing Series) (that’s an affiliate link because I was going to link to it anyway so why not). Chapter 3 in the book deals with data types and the examples illustrate the various effects of using different types but I’m not going to go into those, instead I’ll work through the details of generating an image of the Mandelbrot set point by point.

First: what is the Mandelbrot set? Here’s a good primer from Numberphile:

In short, it’s the set of complex numbers c which does not diverge for x_{n+1} = x_{n}^{2} + c when iterated from x=0. That is from the Wikipedia article which is good and gives some visualisations. The set makes a lovely image and is fun to play with which is why it was included, I’m sure.

Let’s think about plotting the Mandelbrot set on the complex coordinate plane first. If a number c is in the Mandelbrot set then we’ll colour it black and if it’s not, then we’ll colour it white. The resulting plot looks like this:

*By Connelly (talk · contribs), Public Domain, Link*

So those are all the complex numbers c which do not diverge in z_{n+1} = z_{n}^{2} + c when iterated from z=0.

But what if we wanted to do a minimum of iterations, only until we knew for sure that a point was not in the Mandelbrot set? And what if we decided to keep track the number of iterations it took? Here it helps to know that once |z_{n+1}|> 2, then c can no longer be in the Mandelbrot set (n here is the number of iterations and this is a proof).|z| is the absolute value of z and can be calculated from a complex number, z = x + iy like this: |z| = **√** (x^{2} + y^{2}). It is the distance to the point from the origin x,y, ie the hypotenuse of that triangle:

For points not in the Mandelbrot set, we’ll make them a colour which is dependent on the number of iterations required to know that that point is not in the Mandelbrot set. You can see what it looks like here where it was generated from a Python script which looks fun to try out and maybe get that book? Hmm. Anyway. The number of iterations colour scale is at the side. As you might expect, anything >2 takes zero iterations to make it out of the space. It’s the closer-in bits that get interesting and lead down beautiful rabbit holes.

OK now that’s the Mandelbrot set and some visualisations. How do we make an image out of it?

The way I did it was simple: copy and paste @torbiak’s code solution to GOPL ex3.5. That worked beautifully! However I wanted to understand it, so this is that process. Here is what the code does and how I’ve ended up thinking about it, step by step:

- define the height and width of the image
- define the max and min of x and y (in the image above, x
_{max}and y_{max}= 2 and x_{min}and y_{min}= -2) - make an image and iterate over every single pixel
- map each pixel to a coordinate on complex plane (transform the image dimensions into the dimensions of the section of the complex plane we’re using)
- calculate whether the coordinate is in the Mandelbrot set using a set number of iterations
- colour the pixel according to whether it is in the set and, if not, how many iterations it took to determine it is not in the set

For 1 and 2, xmin, xmax, ymin, ymax are used to define which bit of the complex plane we’re going to look at and width and height are the image dimensions.

For 3, ok, make an image from the width and height we’ve set (see here for the image package) and add a couple of for loops to iterate over every pixel in the image:

Step 4 requires a little bit of math. What we want is the function which transforms py into y and px into x. With a tiny bit of linear algebra we come up with this for py (px is similar):

Putting this into our for loops (which iterate over every single pixel, remember) give us:

nb: Chapter 3 in the book is all about data types, so you should read that to understand them. ☺️

Now that we have x and y, we can test whether or not x + iy is in the Mandelbrot set.

## Leave a Reply