At My Fingertips

Rapid Playground

Documentation

Fibonacci

In this activity we will implement the fib_spiral function to generate a spiral representing the first n fibonacci numbers, like in the following graphic.

fibonacci

The Tiles

The fibonacci spiral is composed of square tiles with a circular sector going from corner to corner, rotated and increasing in size.

The first thing we will have to do is to implement the tile function that just returns a square of the given color and size, with a black circular sector of 90 degrees (whose radius is equal to the square's side) on top of it, and a smaller circular sector of the square's color covering part of the black one, to make the illusion of a black line. The difference between the black circular sector and the covering circular sector should be the given line_thickness

To achieve the wanted effect you will need to use PyTamaro iconpin and PyTamaro iconcompose to pin everything in the bottom left corner.

Loading...

Orienting the tiles

Each tile should be rotated by 90 degrees with respect to the previous one. This means that the first tile should stay the same, the second tile should be rotated by 90 degrees, the third should be rotated by 180 degrees and the fourth by 270 degrees, then the fifth should stay the same and so on.

Let's implement an orient function that returns the given tile rotated according to the n parameter. If n == 0, then the tile should not be rotated, if n == 1 it should be rotated by 90 degerees and so on (same rules as before but the first tile is for n=0, second tile is for n=1 etc.).

Use the modulo function %.

Loading...

The Spiral

Now we are ready to implement the fib_spiral function. This function takes the number of tiles of your final spiral as the parameter n. The spiral becomes very large very quickly, so feel free to adapt the given MIN_SIZE constant when you put a bigger n.

The MIN_SIZE is the size that the smallest two tiles should have (the first two tiles should have the same size, since fibonacci's sequence goes 1, 1, 2, 3, 5, ...).

Depending on how you implemented the orient function, the first two tiles should be placed one PyTamaro iconabove the other or one PyTamaro iconbeside the other, so that the black lines align.

Then, the third tile should be as wide or as tall as the first two tiles together (in a way where the black lines align) and so on. Use the pytamaro PyTamaro icongraphic_width and PyTamaro icongraphic_height functions to do this.

The given THICKNESS should also be adapted according to the wanted n, since the final result can become very large.

There are multiple ways to achieve the wanted result but we will guide you through the implementation of the fib_spiral function as a recursive function.

A recursive function should have a base case and a recursive step.

For our function:

  • the base case should return the first tile
  • the recursive step should generate a new tile oriented according to the given n, and combine it with the result of a call to the fib_spiral function itself with value n - 1 for parameter n.

This way, the base case fib_spiral(n=0) will finish execution first, then fib_spiral(n=1) will generate a new tile and combine it with the first one, then fib_spiral(n=2) will generate a new tile and combine it with the result of fib_spiral(n=1), until fib_spiral(n=n) returns the final result.

Loading...

This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Fibonacci

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version 96857c4 (Sat, 04 May 2024 09:49:57 GMT)