Finch Fractals

Fractals are geometric shapes that are defined recursively. For example, the base case (order 0) for a Koch fractal of size L is a straight line of length L, and a Koch fractal of order n and size L is defined as shown below. This algorithm is given in How to Think Like a Computer Scientist: Learning with Python 3 by Wentworth, Elkner, Downey, and Meyers.

  • Draw Koch fractal of order n - 1 and size L/3.
  • Turn left 60°.
  • Draw Koch fractal of order n - 1 and size L/3.
  • Turn right 120°.
  • Draw Koch fractal of order n - 1 and size L/3.
  • Turn left 60°.
  • Draw Koch fractal of order n - 1 and size L/3. 

For example, a Koch fractal with order 2 and size 27 is composed of 4 Koch fractals of order 1 and size 9. Each of those, in turn, is composed of 4 Koch fractals of order 0 and size 3. Thus, the Koch fractal of order 2 is made up of 16 line segments of size 3.

Use this algorithm to write a program that uses the Finch to draw a Koch fractal given the order (greater than or equal to 0) and size. For the Finch, the length of a line is roughly proportional to the time period for which the wheels move, so you can assume that the “size” of the fractal is a time period in milliseconds. To get a good picture of your fractal, make sure that only the left wheel moves when the Finch is turning; the right wheel should always have a speed of 0 for turns.

Hint: The Finch moves and turns more reliably at low speeds.

When you complete your program, you will want to see the Finch draw fractals of different orders. It is possible to attach a marker to the Finch, but it is hard to get a good picture of your fractal this way. Instead, attach a CR2302 battery to a small LED and tape it to the Finch over the right wheel (the wheel that is stationary during turns).

Now you can use a long exposure photograph follow the “light trail” of the LED as the Finch moves. The pictures below were taken with the Slow Shutter Cam app, but other methods of taking long exposure photographs can also be used (LongExpo is a free app). You will need to make sure that the camera is very still during the exposure, so a tripod is a good idea if you have access to one. The light trail can also be a good way to measure the angle that the Finch is turning as you test and debug your program.

Assignment Category: