# A simple introduction to the world of fractals using python

Before you proceed with the rest of the article let me warn you that you are entering an extremely addictive zone. When I started I planned to create a single most famous fractal, but it turned out that once you start creating fractals you will crave creating more because fractals are not just beautiful patterns but also geometric figures with mindblowing math concepts.

So proceed only if you are interested in the beauty of maths, coding, and have no problem getting addicted to creating patterns.

Before coding these amazing fractals, it is important to know what a fractal is. Google’s definition of fractals is — “ a fractal is a self-similar subset of Euclidean space whose fractal dimension strictly exceeds its topological dimension”.

WOAH!! sounds complicated, isn’t it? Well, the truth is fractals are complex but simply, a fractal can be defined as a geometric figure which is the same no matter how many times you zoom it. In other words, it is a repetition of a simple geometric figure. This property is called self-similarity. what about the dimensions stuff mentioned in the definition?

For that let me define what a Hausdorff dimension is. Hausdorff dimension is a measure of roughness or fractal dimension. it was introduced by mathematician Felix Hausdorff. For a single point, the Hausdorff dimension is 0, for a line segment it is 2 and for a cube, it is 3. But for a fractal Hausdorff dimension is a non-integer.

NERD!!!!!

Fine, let’s end our discussion on dimension stuff and start the fun part. If you are thinking what I am thinking, good you are a genius. No? don’t worry you are going to become a genius.

**Let’s start CODING!!!**

There are different kinds of fractals. But firstly let’s learn how to create complex number fractals. One of the most famous and basic complex number fractals is a Mandelbrot fractal.

**Mandelbrot set:**

Mandelbrot set can be defined as a set of complex numbers which are generated by repeating the following equation again and again —

Zn+1 = Zn*Zn + C

A complex number C is a part of Mandelbrot set if, the calculated value of z remains bounded no matter how bigger the value n gets.

To create a Mandelbrot fractal we are going to repeat the above-mentioned equation repeatedly, till the absolute value or the magnitude of Z exceeds or equals to 2.

First of all the required libraries are imported. For this fractal NumPy and matplotlib libraries are imported. Numpy is a linear algebra library capable of doing complex mathematical operations and matplotlib is used for plotting the results.

Then a function is defined which will do the required calculations.

As mentioned above, the value of c is changed and added until the magnitude of Z exceeds 2. That is what the for loop does n number of times, where n is the number of iterations.

Finally, the output is plotted as an image using matplotlib. cmap or colormap is used to represent an image in a more amusing way. I have used cmap hot. There are different types of cmap available, you can choose any one of them.

After running the code the fractal is generated and it looks like this —

It looks amazing, isn’t it???????😍

If you like what you just did right now, let’s go-ahead create even more amazingly beautiful complex number fractal named — Julia fractal.

**Julia fractal**

A Julia fractal can be defined as a subset of Mandelbrot fractal. While generating a Mandelbrot fractal, we changed the values of C and with Z0 = 0. But for generating a Julia fractal the value of C is kept constant throughout the execution and Z is varied. For different values of C, different Julia fractals are formed.

For generating Julia fractals, like mandelbrot, only NumPy and matplotlib libraries are enough. The only difference between mandelbrot code and Julia is that instead of changing the value of C, the value of Z is changed and C is kept constant.

After the function is done, the generated fractal can be plotted in the same way as we did in the Mandelbrot fractal. Run code when you completed it.

and **VOILA!!!!!!!!!!!!**

This magnificent Julia fractal is generated when the value of C is -0.42+0.6j and iterated 120 times.

Go on, code for the Julia fractal and change the values of C. Results will be a treat to your eyes. Also, experiment with different colormaps

**Iterated Function System fractals:**

So, these are the basic complex number fractals. Now its time to change the type of fractal to — iterated function system fractals or IFS fractals. The main concepts behind generating these IFS fractals are recursion and the Lindenmayer system.

Recursion —

A recursive function is a function which calls itself during execution and this process is called recursion. You will understand how this concept is used once I post the code.

Lindenmayer system —

Lindenmayer system or l-system is a representation of recursive fractal in the form of a string.

For example, let’ s see how a Koch snowflake is represented in l-system —

axiom = “F- -F- -F”

rules = {“F”:”F+F- -FF+F””}

iterations = 4

angle = 6

So, first of all, an axiom is the starting geometrical figure. Rules are the directions in which the starting has to be changed in order to get the required fractal and angle is the angle at which it should be rotated. Iterations are the number of times the code has to loop in order to get the final fractal.

What’s all the F stuff then???

those are called as variables

F means move forward

“+” means turn right by the given angle

“-” means move left by the given angle

Now imagine the drawing linse as mentioned in the axiom above. What is the end figure?

Yes, you are absolutely correct, it’s a triangle!

And this is how Koch snowflake is formed —

Let’s code now!

For depicting the generated fractal we are going to use turtle library.

This is how recursion and the rules are coded for the Koch snowflake. F in the rules is replaced by the function call in order to get the repetition required for the fractal.

The above function is used for final execution. I have added the pencolors so that the resulting curve will be more attractive. You can remove it or add your own colors.

Result of the Koch snowflake will look something like this

let’s generate another recursive fractal called — Levy -C curve —

L-system defines this curve as -

axiom = “F”

rules = {“F”:”+F- -F+”}

iterations = 10

angle = 45

code —

here I ran the recursive loop 2 times to create a more complex curve. Running the recursive function once is enough to get the original curve.

This is the output I got —

Similarly using the rules of L-system different recursive fractals can be drawn

So far we have seen how to code if the l-system has a single variable. But there are complex fractals that are described using two variables in the l-system.

One of such famous fractal is a dragon fractal.

oooo sounds exotic right??

l-system for dragon curve —

axiom = “FX”

rules = {“X”:”X+YF+”, “Y”:”-FX-Y”)

iterations = 8

angle = 90

to understand this consider X as f and Y as h —

code —

from the l system explanation above it can be said that as the code iterates, the variable is replaced by its respective rules. A list is created and is appended with the pattern to be drawn.

After this, as usual, a function is created to execute the fractal —

The reason why I defined different execution functions is that every fractal has its own scaling issues. By trial and error, I found how to adjust the turtle in order to get a full fractal on the screen.

After running the above code —

Another interesting IFS fractal is Sierpinski arrowhead and Sierpinski sieve.

This is coded in a similar way as the dragon curve. I am not gonna add the code for this in this article, but I will mention my GitHub repository on fractal at the end of this story, in which you can find codes for other IFS fractals.

Tree fractal -

Tree fractals are formed by using the concept of recursion to branch the main figure to form the tree’s branches.

As you can see from the animation the tree fractal is formed by splitting each branch into two other branches.

Result of the code —

By changing the branching length of the main branches a fern can be generated, and by changing the angles pentagon nested tree can be made.

Okay, I know you are tired but hey, I warned you!! there’s still a lot to discuss lol!

Just kidding! I gonna end this article with one last fractal and its called Barnsley fern.

the math behind Barnsley fern —

p is probability

this is how I coded for the fern —

It may seem a bigger code, but the concept is simple. First, the probability is calculated and the parameters are given as input to a function that performs matrix multiplication. Then the array is appended with new x and y points which are then converted into pixel coordinates by another function so that an image can be formed which can be plotted using matplotlib.

result —

If you find my code confusing, I will leave geeks for geeks tutorial below that has a simple implementation.

As mentioned this is just an introduction to various fractals. You can generate your own fractals by changing the values the way you want.

Keep experimenting!!!!!

**My GitHub repository —**

**References —**