At My Fingertips
Rapid Playground
A Sierpiński triangle is a fractal composition of equilateral triangles, where each triangle is subdivided into smaller equilateral triangles.
The Sierpiński triangle is a recursive construction. At the base case of the recursion, we want to draw an equilateral triangle.
If you already have an equilateral_triangle
function in your toolbox, just from toolbox import equilateral_triangle
instead.
Now let's implement the recursive sierpinski_triangle
function.
It takes two parameters:
the side length of the Sierpiński 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 Sierpiński triangles into a bigger Sierpiński triangle.
Let's visualize the evolution from a normal equilateral triangle to Sierpiński triangles with growing recursion depths (smaller and smaller limit
values).
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 Sierpiński triangle is related to Pascal's triangle: try to color the cells of Pascal's triangle with 2n rows if they are odd, and leave the even cells white.
A fractal that is closely related to the Sierpiński triangle is Cantor dust. Check out the Cantor dust activity to discover more!
You discovered a beautiful fractal, and you learned how fractals are recursive compositions.
You learned to implement this composition by writing a recursive function (sierpinski_triangle
, which calls itself).
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 1cd5229 (Tue, 05 Nov 2024 16:55:57 GMT)