Table of contents
1.
Introduction
2.
Approach to plot Fibonacci spiral Fractal
3.
Plot Fibonacci Spiral Fractal using Turtle
4.
Code
4.1.
What is the use of the fillcolor() function?
4.2.
How to create a new turtle object?
4.3.
What is the use of the begin_fill() function in turtle?
4.4.
What is the best way to stop a turtle from drawing?
4.5.
What is the purpose of the turtle module?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Plot Fibonacci Spiral Fractal using Turtle in Python

Author Saurabh Anand
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Fractal geometry is a type of graphical depiction of mathematical functions or sets of numbers produced by mathematical functions. It is a recursive, comprehensive, and indefinitely self-similar mathematical system. "Unfolding Symmetry" can be seen in these geometrical renderings. The property of geometrical constructions to preserve a self-similar pattern at an infinitesimally small size is known as unfolding symmetry.

The Fibonacci Series is known to have a recursive relationship g(x) = g(x-1) + g(x-2) where x is the xth term in the series. Once again, g(0) = 0 and g(1) = 1.

Now let's see the approach to plot Fibonacci Spiral Fractal using Turtle in Python.

Approach to plot Fibonacci spiral Fractal

Each number in the plot Fibonacci spiral fractal is the series that represents the length of a square's sides. There is no such thing exist as a square with a side length of zero. As a result, we begin with a square with a side length of one. The next square has a side length of one as well.

  • First, build two squares of dimensions, one side by side, as illustrated in the image below.
  • Then, using the joint length of the two squares, we build a third square beneath the two squares of dimension 1. The square now has a dimension of 2.
  • Using the two squares of dimensions 1 and 2, we construct the fourth square of dimension 3.
  • Although we just went through a few iterations of this procedure, it continues indefinitely.

 

Plot Fibonacci Spiral Fractal using turtle

Source: https://pythonturtle.academy/fibonacci-spiral/

Now let's look at the code to plot Fibonacci Spiral Fractal using Turtle in Python.

You can also read about Palindrome Number in Python here.

Plot Fibonacci Spiral Fractal using Turtle

In this section, we will learn how to plot Fibonacci Spiral Fractal using Turtle in Python.

Code

# Python program for plot Fibonacci Spiral Fractal using Turtle
import turtle
import math

def fiboPlot(n):
   a = 0
   b = 1
   sqr_a = a
   sqr_b = b

   # Setting the color of the plotting pen to orange
   y.pencolor("orange")

   # Drawing the first sqr
   y.forward(b * fac)
   y.left(90)
   y.forward(b * fac)
   y.left(90)
   y.forward(b * fac)
   y.left(90)
   y.forward(b * fac)

   # Proceeding in the Fibonacci Series
   temp = sqr_b
   sqr_b = sqr_b + sqr_a
   sqr_a = temp
  
   # Drawing the rest of the sqrs
   for i in range(1, n):
       y.backward(sqr_a * fac)
       y.right(90)
       y.forward(sqr_b * fac)
       y.left(90)
       y.forward(sqr_b * fac)
       y.left(90)
       y.forward(sqr_b * fac)

       # Proceeding in the Fibonacci Series
       temp = sqr_b
       sqr_b = sqr_b + sqr_a
       sqr_a = temp

   # Bringing the pen to starting point of the spiral plot
   y.penup()
   y.setposition(fac, 0)
   y.seth(0)
   y.pendown()

   # Set the color of the plotting pen to red
   y.pencolor("red")

   # Fibonacci Spiral Plot
   y.left(90)
   for i in range(n):
       print(b)
       fdwd = math.pi * b * fac / 2
       fdwd /= 90
       for j in range(90):
           y.forward(fdwd)
           y.left(1)
       temp = a
       a = b
       b = temp + b

fac = 5

m = int(input('Enter the no. of iterations (must be > 1): '))

if m > 0:
   print("Fibonacci series for", m, "elements :")
   y = turtle.Turtle()
   y.speed(100)
   fiboPlot(m)
   turtle.done()
else:
   print("No. of iterations must be > 0")
You can also try this code with Online Python Compiler
Run Code


Input: 

Enter the no. of iterations (must be > 1): 10
Fibonacci series for 10 elements 
1
1
2
3
5
8
13
21
34
55


Output:

You can practice by yourself with the help of online python compiler for better understanding.
Frequently Asked Questions

What is the use of the fillcolor() function?

The fillcolor() function sets the fillcolor. If the shape is a polygon, the interior of that polygon is drawn with the newly set fillcolor

How to create a new turtle object?

The Turtle() method creates and returns a new turtle object in Python.

What is the use of the begin_fill() function in turtle?

The begin_fill() function in turtle is used to remember the starting point for a filled polygon.

What is the best way to stop a turtle from drawing?

Without the turtle, drawing orders are still executed, and lines are still produced when the turtle is moved. Toggle drawing on and off with up and down, or move without drawing with the setx, sety, or goto functions.

What is the purpose of the turtle module?

The turtle module provides object-oriented and procedure-oriented turtle graphics primitives. It requires a version of Python with Tk support because it utilizes Tkinter for the underlying graphics.

Conclusion

In this article, we have extensively discussed how to plot Fibonacci Spiral Fractal using turtle in Python, along with the steps and code.

 We hope that this blog has helped you enhance your knowledge of the plot Fibonacci Spiral Fractal using turtle in Python. If you would like to learn more, check out our articles Panda Using TurtleStar using Turtle In PythonChess Board using Turtle, and Clock using Turtle. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass