# 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: This produces the following Koch curve (3rd degree): 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). This site uses Akismet to reduce spam. Learn how your comment data is processed.