The Power of Recursion

Smruti Ranjan Badatya
4 min readDec 20, 2022

We come across patterns & symmetry all around us in our daily life. Every Symmetry has some maths inside it. Recursion can help to find out the core essence of a pattern and using this we can reflect that into code & make a computer do all the calculations and in the end, we get a beautiful pattern.

Recursion is just doing something over and over again with slight modifications.

Let’s take an example. We have a circle. Then, we draw another circle half the diameter of the previous one until the diameter is 1. Now, the end condition is important other wise the loop can go on forever and the recursion won’t stop ( although current computers limit the iterations after some fixed time ).

Well, here is what I mean.

These patterns are computer generated with a very simple recursive function.

void drawCircle(float x,float y, float d){
noFill();
strokeWeight(1);
circle(x, y, d); // draws a circle at x, y with diameter d

if(d < 1) return; // condition where to stop the recursion

drawCircle(x, y, 0.50*d); // calls itself with d = d/2
}

Well, if that didn't impress you. I have more. Now, let's do something exciting. Now, let's move the x position of the new circle with some distance each time we draw a new circle. let’s make it x + d/2 ( just be with me ) along with reducing the diameter by half ( i.e. d = d/2 ). This is what I mean.

Well, you can extend this to the Y direction as well and you will achieve something like this.

As of now, we only did individual transformations in each axis how about combining some of them? Let’s combine x = x + d/2 and x = x — d/2 ( or the same with the y direction ).

How cool is that ? Obviously, this level of precision in drawing patterns is beyond human capabilities. Computers are very good at recognizing patterns.

Wait there is more, this is the fun part. Now, let’s combine one of the Y transformations with both of the X transformations.

This is a wonder. Boys & Girls, you have just created a Sierpiensky Triangle.

The Sierpinski triangle is a self-similar fractal. It consists of an equilateral triangle, with smaller equilateral triangles recursively removed from its remaining area. , which is named after the Polish mathematician Wacław Sierpiński.

Did you ever think you can code this? In the end, recursion is what we did. From a very simple pattern, we were able to deduce this complex fractal using just recursion and some maths.

Here, is the code for the above figure.

void drawCircle(float x,float y, float d){
noFill();
circle(x, y, d);
strokeWeight(0.5);

if(d < 1) return;

drawCircle(x + d/2, y, 0.50*d);
drawCircle(x - d/2, y, d/2);
drawCircle(x, y - d/2, d/2);
// drawCircle(x, y + d/2, d/2);
}

From this point on, you can tweak the addition, subtractions, multiply, and divide if you wish to. Every time, you will get a very different pattern. This is really cool.

Achieving this through computation took years of technological advancements( although this is not developed recently ). Computers are very powerful in the case of computation. Computers can do much more sophisticated computations than this. This is just the starting point of the ocean of things we can achieve.

If this felt interesting to you, you can consider following me. This can motivate me for writing such articles. If you want what programming language, what environment is used for this, or about the codes, leave a comment below and I will reach you with another article.

See ya next time!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Smruti Ranjan Badatya
Smruti Ranjan Badatya

Responses (2)

Write a response

Nicely explained!

Interesting approach to visualise recursion. Never though of it this way! Recursion can create magical things indeed.