Beauty of Recursion
Nilay Shrivastava
Is there something visually beautiful that can come out of Recursion? Industry stalwarts hate it (recursion is a big NO in production code!) and undergrads (like me) sweat while doing time complexity analysis for algorithms employing recursion. But whichever side of the spectrum you are in the software world, you would agree that recursion has a charm of its own. During my undergraduate years as I became familiar with the Computer Science world around me, I began to appreciate the inherent idea behind recursion. Moreover I understood that it not just a fancy technique used by clever programmers, but is something that is intrinsic to nature itself. This post is a presentation of art that I learned and then coded during my leisure time.
In this sense this post is more of a showcase/gallery than an educational blog.
Fractal Trees
This is the most common example of fractals.Notice how each part of the system is constructed recursively. Use the slider to generate more patterns
Making things more natural: Stochastic Trees
Although the tree patterns were beautiful, they were too symmetric to be natural. Below is an example of naturalness can be embedded by combining randomness with recursion. The underlying algorithm doesn’t traverse all children in the recursion tree everytime, rather it choses the recursion branch randomly. To make it more natural I also added the effect of wind on our swaying tree.
On older sytems the visualisation might be slow
Using L-Systems
Another common technique for generating beautiful patterns is by using Lindenmayer system or L-System for short. It is a grammar (yes those same grammars that we study in Theory of Computation) having a set of production rules. They were invented to describe the structural growth of plants. To know more about L-Systems read The Algorithmic Beauty of Plants. L-Systems deserve a blog post of their own and I plan to write one as soon as I find time. Meanwhile enjoy recreating plant growth by clicking generate button!
Ain’t it beautiful?
Conclusion
This is the most haphazard blog I have ever written. Paradoxically, this post took around five months to complete!! Thanks to internships, tests and unplanned undergrad life. My motivation behind this post was to indulge in and showcase something which I love to call computational art . I would encourage you to experiment with recursion and create more such pieces of art. For code, checkout the github repostory of this blog.