Learn creative coding writing simple programs

140. Recursive graphics

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.

Tags: recursion, recursive, lerpColor

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
  Do `float` and `int` smell similar?
To highlight code blocks, use ```processing and ``` like this:
  void setup() {
    size(600, 600);