Fun programming 140: Recursive graphics

Share it with your friends Like

Thanks! Share it with your friends!

Close

Using recursion in computer programs allows us to generate beautiful and complex images with simple programs. Recursion is simply achieved by writing a function that calls itself one or more times.

To avoid giving a headache to our computer, it’s important to set a limit in how many times the function can call itself. Otherwise it will attempt to call itself infinite times, and the computer will stop liking you. There are different ways to set a limit. You can use a variable to count the recursion depth, and stop when the depth is enough for you. You could also set a limit by drawing shapes that get smaller and smaller, and then stop when they are small enough.

In this episode, instead of coming up with random colors for the program, I use a color picker called Gcolor2 to grab colors from a photo. In Windows you can use picpick.org, and in Mac “DigitalColor Meter”, already installed by default.

Check episodes 67 and 68 if the sin() / cos() part is confusing.