The Fibonacci sequence is a visual marvel in addition to a mathematical mystery. By sketching the Fibonacci spiral, we can observe how this series of numbers develops into a form that is similar to the spirals found in galaxies, hurricanes, and seashells. In this lesson, we’ll see the Fibonacci spiral on our screen using Python’s Turtle graphics!
What is the Fibonacci Spiral?
The Fibonacci spiral is constructed by drawing quarter-circle arcs within squares whose side lengths correspond to Fibonacci numbers. As each square builds upon the previous one, a natural spiral forms. Let’s create this spiral using Python and see how the Fibonacci sequence can generate beautiful geometry.
Step 1: Setting Up Python Turtle
We’ll use Python’s turtle library, which is great for creating drawings and simple animations.
- Installing Turtle: For most setups, Turtle is pre-installed with Python. If not, install it using:
pip install PythonTurtle
Step 2: Generating Fibonacci Numbers
First, we need a function to generate Fibonacci numbers that define the side lengths of the squares in our spiral.
def fibonacci_sequence(n):
sequence = [0, 1]
for i in range(2, n):
sequence.append(sequence[i-1] + sequence[i-2])
return sequence
Step 3: Drawing the Fibonacci Spiral
Let’s use the Turtle library to draw squares based on Fibonacci numbers, followed by arcs that connect them to create the spiral effect.
import turtle
def draw_fibonacci_spiral(n):
fib_sequence = fibonacci_sequence(n)
turtle.speed(5) # Set drawing speed
for length in fib_sequence:
turtle.forward(length * 10) # Scale up for visibility
turtle.right(90) # Right angle for each square
# Reset position for drawing arcs
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
for length in fib_sequence:
turtle.circle(-length * 10, 90) # Quarter-circle arc
turtle.done()
Step 4: Adding Colors to Enhance the Spiral
To make the spiral more visually engaging, we can add colors that change with each square.
colors = ["red", "blue", "green", "purple", "orange"]
def draw_colored_fibonacci_spiral(n):
fib_sequence = fibonacci_sequence(n)
turtle.speed(5)
for i, length in enumerate(fib_sequence):
turtle.pencolor(colors[i % len(colors)])
turtle.forward(length * 10)
turtle.right(90)
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
for i, length in enumerate(fib_sequence):
turtle.pencolor(colors[i % len(colors)])
turtle.circle(-length * 10, 90)
turtle.done()
Step 5: Running the Code
Run the code to watch your Turtle create the Fibonacci spiral, with each square and arc adding to the mesmerizing shape. Experiment with the number of terms in the Fibonacci sequence or different colors to make it your own.
Conclusion
A stunning illustration of the fusion of art and mathematics is the Fibonacci spiral. The elegance of Fibonacci’s work may be appreciated in a whole new way by using Python’s Turtle package to replicate its natural shape on our screen.