Recursive Patterns
Last updated
Last updated
With Recursion, we can often think of a set of rules being executed for each function call. This can be used to create interesting patterns, because we can often decompose graphic patterns to be expressed as a set of rules. In the artwork below, a simple pattern of concentric rectangles is created wherever the mouse is pressed.
When writing recursive functions there are several factors to consider:
Identify the base-case or stopping condition
Insure that the variable that controls the stopping condition will be modified in the recursive function so that it will eventually reach the stopping condition.
Locate the conditional test for the stopping condition before the recursive function call, to prevent execution of the recursive call if the stopping condition has been met.
Determine whether the function task should be performed before or after the recursive call.
Insure that input parameters to the recursive function provide all information needed at each step, and be careful when modifying values passed as arguments.
Be aware that each instance of a recursive function call causes a unique instance of the function's code to be placed on the call-stack
, this can potentially cause stack-overflow errors if the program runs out of available program-execution memory space.
The recursive function: recursivePattern(length, level)
takes 2 input parameters: length
controls the size of the rectangle and level
, which controls the number of concentric rectangles drawn. By adding slight random variation in the rotation angle, rectangle size, and hueValue of the fill and stroke for each drawn rectangle, the user can create a unique artwork each time they run the program. The hueValue can be mapped to the x
position of the mouse. Using the Hue, Saturation, Brightness: colorMode(HSB)
allows for slight random variations of the hueValue to give slight variations in the hue of the drawn pattern.
If the user is pressing the mouse
Draw a Pattern (drawn at origin): move the canvas origin to the current mouse position.
Draw some pattern based on length
parameter, by calling the recursive function: recursivePattern( );
Move Origin back to upper left corner
Here we define a recursive function: recursivePattern
so that it calls itself for level
number of times, with a length
parameter that determines the pattern size:
We need to add a test for the termination condition as the first task in the function:
We will draw a shape at the origin using the length
parameter
Recursive function must call itself, within the function , make sure to modify the function parameters:
change length: length * 0.8
change level: level - 1
Below is a slight variation on the recursive function, here we're using a global variable: lenMin to determine when to terminate the recursion. In addition, we're using global variables to determine the fill values for Hue and Brightness, where we'll set colorMode in the setup function: colorMode(HSB, 360, 100, 100);
Here's a simple program that defines and uses a recursive function to create a pattern. Notice that in this case we are using global variable: lenMax as the range value for the Map( ) function which is determining the fill for the vertexShape.
Recursive Pattern with VertexShape In the code below, we're calling our vertexShape function inside the recursivePattern. Each time we call recursivePattern, we have reduced the size of the length parameter.