At My Fingertips
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.
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 pin and compose to pin everything in the bottom left corner.
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 %
.
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 above the other or one beside 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 graphic_width and graphic_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:
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.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Fibonacci
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version b744b47 (Tue, 08 Oct 2024 16:30:14 GMT)