At My Fingertips
A Sierpiński triangle is a fractal composition of equilateral triangles, where each triangle is subdivided into smaller equilateral triangles.
The Sierpinski triangle is a recursive construction. At the base case of the recursion, we want to draw an equilateral triangle.
Let's implement a function to draw an equilateral triangle with a given size (side length) and color. Use PyTamaro's triangle function for this.
Now let's implement the recursive sierpinski_triangle
function.
It takes two parameters:
the side length of the Sierpinski triangle to be drawn (size
),
and the limit
for the side length, at which the recursion stops and it simply draws a normal equilateral triangle.
Each recursive function invocation composes three smaller Sierpinski triangles into a bigger Sierpinski triangle.
Let's visualize the evolution from a normal equilateral triangle to Sierpinski triangles with growing recursion depths (smaller and smaller limit
values).
Write a function evolving_triangles
that places evolving Sierpinski trianges side-by-side.
The shape used at the base case does not necessarily have to be a triangle. You could use a square, or a circle, or any other shape. Try it out!
The Sierpinski triangle is related to Pascal's triangle: try to color the cells of Pascal's triangle with 2^n rows if they are odd, and leave the even cells white.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Sierpinski Triangle
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)