Ronnie Atuhaire
Ronnie Atuhaire's Blog 🤓

Ronnie Atuhaire's Blog 🤓

Sierpiński Triangle With Python Turtle

Sierpiński Triangle With Python Turtle

Ronnie Atuhaire's photo
Ronnie Atuhaire
·Apr 1, 2022·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • 🔸 Sierpinski Triangle
  • 🔸 Pascal's Triangle
  • 🔸 The Tower of Hanoi
  • 🔸 Python Sierpiński
  • 🔸Sierpiński Use Cases
  • 🔸 Conclusion

In Mathematics, a triangle is a 3-sided polygon sometimes (but not very commonly) called the trigon and many Mathematicians have come up with their own versions with some solving real-world problems.

🔸 Sierpinski Triangle

The Sierpiński triangle (sometimes spelt Sierpinski), also called the Sierpiński gasket or Sierpiński sieve is a fractal attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller equilateral triangles.

SierTriangle

The Sierpinski triangle is a fractal described in 1915 by Waclaw Sierpinski. It is a self-similar structure that occurs at different levels of iterations or magnifications.

We can use Geometer's Sketchpad to construct these types of triangles, and then compare them to the pattern of Pascal's Triangles.

In mathematics, a fractal is any class of complex geometric shapes that commonly have a “fractional dimension”.

🔸 Pascal's Triangle

In mathematics, Pascal's triangle is a triangular array of the binomial coefficients that arise in probability theory, combinatorics, and algebra.

Pascals Triangle

Pascal's Triangle is a triangle of numbers where each number is the two numbers directly above it added together (except for the edges, which are all "1").

Pascal's triangle is basically a triangular array constructed by summing adjacent elements in preceding rows.

🔸 The Tower of Hanoi

The Tower of Hanoi is a mathematical game or puzzle consisting of three rods and a number of disks of various diameters, which can slide onto any rod.

image.png

🔸 Python Sierpiński

Turtle graphics is a popular way of introducing programming to kids. In Python, we have a module known as turtle in the standard library meaning it comes pre-installed.

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

Tkinter comes pre-bundled in Python but if you can also do a manual pip install tk.

Turtle has various methods that we can use to achieve the Sierpiński Gasket in just a few steps.

from turtle import *

The above line of code imports everything from the turtle module.

We shall be using a recursive function. Recursion is a programming technique that involves creating functions that recall themselves.

Now let's add this

pu()
ht()
right(30)
speed(100)
shape("triangle")

🚀pu() is pen up: Pull the pen up -- no drawing when moving.
🚀ht() hideturtle or ht means hide the turtle, so you can admire your drawing.
🚀right() Turns the turtle right by angle.
🚀speed() sets the speed.
🚀shape() sets the shape we want.

Now create are going to create a recursive function that will help us create the desired triangle with some new movement turtle methods.

def sier(size, order):
    if order == 0:
        stamp()
    else:
        for i in range(0,3):         
            fillcolor("red") 
            forward(size) 

            sier(size/2,order-1)

            backward(size)
            left(120)

Our function takes in order and size as parameters, order can be the depth and size can be our required length.

🚀 stamp() This method is used to stamp a copy of the turtle shape onto the canvas.
🚀fillcolor() sets the colour.
🚀forward() will always take in the current size from the recursive call which is called before backward and left().

The order == 0: line helps us to control the recursion depth and be able to jump out of the loop since we decrease the order and divide the current size in the subsequent recursive callsier(size/2,order-1).

So if we call our function with a size of 100 and an order of 4 and set the screen not to exit until we actually click or want to.

sier(100,4)  
window = Screen()      
window.exitonclick()

And if we run our entire code, we shall get this as our output.

image.png

Try experimenting with the code with different sizes, orders, shapes, and colours and you will love it.

For example, if I used a square as the shape, this would be the output;

image.png

Obviously, it's not a Sierpiński but just for fun.

GitHub Repo

Watch video instead;

🔸Sierpiński Use Cases

Fractals are used to model soil erosion and to analyze seismic patterns as well. Seeing that so many facets of mother nature exhibit fractal properties, maybe the whole world around us is a fractal after all! Actually, the most useful use of fractals in computer science is fractal image compression.

The Aquila Digital Project;
This project intends to explore one of the relationships between mathematics and music, specifically through the Sierpiński fractals. Read more.

Mobile phone and Wi-Fi fractal antennas have been produced in the form of a few iterations of the Sierpiński Carpet.

The Sierpinski triangle also appears in certain cellular automata (such as Rule 90), including those relating to Conway's Game of Life.

📌 Encyclopædia Britannica
📌 Wikipedia Sierpiński triangle
📌 Python Turtle Official Docs
📌 Python Extension Workshop: Fractal Geometry with Python Turtle
📌 Jake Shams' Solution

🔸 Conclusion

Once again, hope you learned something today from my little closet.

Please consider subscribing or following me for related content, especially about Tech, Python & General Programming.

You can show extra love by buying me a coffee to support this free content and I am also open to partnerships, technical writing roles, collaborations and Python-related training or roles.

Buy Ronnie A Coffee 📢 You can also follow me on Twitter : ♥ ♥ Waiting for you! 🙂
 
Share this