CS21 Recursion Extra Challenge

Some Extra Challenging Fractal Patterns

There are a lot of fractal patterns you can "easily" draw using recursive functions. Here is are a few links to more information about fractals:

Here are two challenging patterns you could try:

1. Fractal Tree Recursive techniques can be used to draw trees, ferns, and other patterns. The tree below is drawn by drawing the trunk, turning slightly to the left and recursively drawing a tree, then turning slightly right and recursively drawing a tree. Experiment with recursive drawing and come up with your own patterns.

A recursive function to draw a tree requires some trigonometry and keeping track of the current location and size branch you are drawing, and at what angle and scale. A recursive function definition might look like:

drawTree(window, depth, start, end, angle, scale, split)

Angles should be in radians. You can convert between degrees and radians using the radians() function in the math library. window, scale, split do not change values in recursive calls. The other parameters do.

2. Koch curves

Below are examples of six Koch curves that have recursion depths from 0 (a straight line) at the top to 5 at the bottom.

A Koch curve is created by breaking a line segment into thirds and replacing the middle segment by two segments that are equal in length. This creates a two-sided equilateral triangle in the middle (e.g., all angles are 60 degrees).

You should start by creating a program called koch.py. You will write a function:

drawKochCurve(win, n, start, end, angle)
which draws a Koch curve from point start to a point end with recursion depth n where angle specifies the angle of the line that connects start to end.

A Koch curve of recursion depth n is formed by four Koch curves as follows:

  1. drawing a Koch curve of length length/3 with degree n-1 and angle
  2. drawing a Koch curve of length length/3 with degree n-1 and angle-radians(60)
  3. drawing a Koch curve of length length/3 with degree n-1 and angle+radians(60)
  4. drawing a Koch curve of length length/3 with degree n-1 and angle

Hint: Each one of these recursive calls is starting from a diffrent starting point. Like in the tree, you will need to know where you are and how you are oriented.