Recursive patterns – the Koch curve (ii)

The last example was a simple iterative program to calculate the first degree Koch curve. The problem is that in order to create a higher order Koch curve we would have to create some sort of deep iterative structure. That is not warranted of course, because it is much easier to do using recursion.

The easiest way is to transform the portions of the previous code which dealt with drawing the lines into recursive calls. This means modifying the way things are processed algorithmically. First we calculate the length of the line, If the length of the line is less than 5, then a line is drawn, otherwise the coordinates needed to divide the line are calculated. Each of the four line segments are then used as input to four recursive calls. Here is the code in Processing:

Code for recursive Koch curve

This produces the following Koch curve (3rd degree):

3rd degree Koch curve

The function drawKochCurveR() is called 85 times to draw this curve. It might be more meaningful to visualize this process as an infographic, depicting how one of the  four piece segments in this curve  is created (it takes 7 recursive calls).

Infographic Koch curve recursion

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.