In this post I will explore using simple fractals and a bit of
randomization to generate "coastlines" like the one below. Complete,
runnable Haskell code to reproduce everything in this post can be found
In the end what we will generate will look something like this:
Fractals are geometrical structures that show a property called self-similarity, i.e. a zoomed in picture of a part of the structure is very similar to the whole structure. In this post we will take a look at a very simple type of fractals called initiator-generator fractals. These fractals are specified by two parts: an initiator and a generator. The initiator is a simple structure - like a line or a triangle - and the generator is a set of initiators which replace the original initiator.
The Koch Curve
A good example is the Koch curve, which is a very simple initiator-generator fractal. The initiator for the Koch (as we will use it) is just a straight line. The generator will be the same line with the middle replaced by two lines in a 60 degree angle to the original line.
The above line is neat in it's regularity, but what I want for a
"coastlines" is a bit of randomness. When generating fractal structures
there is no constraint to use only one generator. We can also use a set
of generators, from which we choose randomly. This is what we will do
next: we add another generator, which is just our original generator but
Each time an initiator is replaced with a generator, i.e. a straight line with an upward or downward pointing triangle, one of the two is picked with equal probability. So there is a 50% chance for each to be used.
Now all that needs to be done to get nicer coastlines is to apply the generators for few more iterations. After 6 rounds of the iterator-generator cycle the resulting picture will look similar to this:
Try it yourself!
If you want to play around with the code yourself, just clone my GitHub repository. The code is written in Haskell, I would recommend building it with the Stack build tool.
Once you have the code cloned and Stack installed, build the code with
to generate a coastline. Output will be stored in a file called fractal.pdf.
stack exec fractal-exe 6
The numeric parameter to fractal-exe controls the number of iterations for which the randomized initiator-generator cycle will be run.