# The Mandelbrot set visualised in golang

[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 xn+1 = xn2 + 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:

So those are all the complex numbers c which do not diverge in zn+1 = zn2 + 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 |zn+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| =  (x2 + y2). 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:

1. define the height and width of the image
2. define the max and min of x and y (in the image above, xmax and ymax = 2 and xmin and ymin = -2)
3. make an image and iterate over every single pixel
4. 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)
5. calculate whether the coordinate is in the Mandelbrot set using a set number of iterations
6. 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.

By submitting this comment, you are agreeing to the use of Akismet which helps reduce spam. You can view Akismet’s privacy policy here. Your email, website and name are also stored on this site.