Porchy Web Development

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:

Mandelset hires.png
By Connelly (talk · contribs), Public Domain, Link

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:

IMG_5001

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.mandelbrotimg

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.

Screen Shot 2017-09-24 at 15.49.32.png

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:

Screen Shot 2017-09-24 at 16.25.09

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):

IMG_4999

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

Screen Shot 2017-09-26 at 16.28.45.png

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.

Join the newsletter

Subscribe to get the latest content by email.

Powered by ConvertKit. If you give me your email address, you may receive emails from me (JJ) about posts on this site. You can unsubscribe at any time.

Porchy Ltd is a company registered in England, no. 12035925

VAT Registration no. 331196421

Built with WPGraphQL and Gatsby

© 2019 - 2021