# The Intrigue

In his 1988 textbook “Fractals Everywhere”, Michael Barnsley, a British mathematician, and researcher introduced an algorithm for generating fractals through a procedure which he called the chaos game.

I initially came across this algorithm on Reddit where a video about it was posted. It had a ton of upvotes and awards, so I was intrigued. After I finished watching it, I was very skeptical about what I had just seen, so I had decided to test it out for myself. These are my findings and results, as I conclude my research impressed and in awe.

# Getting Started

Let’s start experimenting by first placing 3 points on a blank canvas. These points form a pretty standard triangle, but they could be positioned differently. Those are going to be our vertices (red). Somewhere between those vertices, let’s randomly position another point. This is going to be our origin point (blue).

# Rolling The Dice

So far so good. Now, we grab a simple game dice and set some rules:

As we roll the dice, we get a random number between 1 and 6. Based on the number we roll, we are going to move from our origin point (blue) to one of the vertices.

- If we roll 1 or 2, we are going to move towards the top-center vertex.
- If we roll 3 or 4, we are to move towards the bottom-left vertex.
- If we roll 5 or 6, we are going to move towards the bottom-right vertex.

However, we are not going to move all the way from the starting point to the vertex, but exactly *halfway.* Then we roll the dice again and repeat the whole procedure.

So for example, we roll the dice for the first time and land on 2. We move halfway from the origin point to the top-center vertex and create a new point (purple).
Then we roll the dice again, and we land on 1. We move halfway from the latest point we created to the top vertex again. We create a point there.
We roll the dice once more and land on 4. We move halfway from the last point we made all the way to the bottom-left vertex. We create a point there.

We continue to follow these rules by throwing the dice and repeating the process ten, a hundred, a thousand, and even ten thousand times more.
After 10000 throws, the picture becomes quite clear.

We end up with a cool triangle shape, with multiple empty triangles inside it. Keep in mind that we’ve been throwing the dice completely at random, and we could do it all again from scratch as many times as we wanted, we could even reposition the vertices, and we would always end up with this shape (known as The Sierpinski triangle).

# It Comes In Different Shapes And Sizes

The triangle was impressive enough, but what about other shapes? What would happen if I positioned the vertices in a shape of a square, and then apply the same procedure? Only one way to find out —

Now, instead of three vertices, we will have four of them and will be positioned in a shape of a square. The origin point’s position will remained unchanged, in the center.
The dice starts to roll, over and over again, but the result is unfortunately a complete mess. Don’t bother trying to detect any patterns in this, there aren’t any.

As we dig to find some answers to this, it turns out that some shapes require additional rules in the application of Chaos Game.
A simple rule that would generate a much more satisfying result, for example, would be that we cannot move towards the same vertex two times in a row.

Combining different shapes and rules leads to infinite possibilities in Chaos Game. Other possible rules would be to only move to one of the neighbouring vertices of the last throw, or to move only to the ones opposite. You could even concatenate some rules! Each modification can lead to a completely different result.
The most impressive fractal I’ve encountered is called The Barnsley Fern, which eerily resembles an actual existing plant.

# Explore The Playground

I used Swift Playgrounds for purposes of testing and exploration. You can find a simple demo of the Sierpinski Triangle here — the code is documented and fully runnable with a simple copy-paste.
It’s also easily extendable so feel free to define your own shapes and rules to come up with various fractals.

## Why Low-Code Development will not be the end of developers or code

Get the Medium app