At My Fingertips
Cantor dust is a fractal composition of squares, where each square is recursively subdivided into a three-by-three grid containing four squares in its corners.
Cantor dust is a recursive construction. At the base case of the recursion, we want to draw a square.
If you already have a square
function in your toolbox, feel free to instead from toolbox import square
.
Let's create a function that can compose a list of lists of graphics into a two-dimensional matrix. The outer list represents the rows, the inner lists represent the cells in each row.
Using that function, let's create the following graphic:
Now let's implement the recursive cantor_dust
function.
It takes two parameters:
the side
length of the Cantor dust to be drawn,
and the iterations
to denote the depth of the recursion.
Here is the result of cantor_dust(256, 3)
:
Each recursive function invocation
composes four smaller Cantor dust graphics into a bigger Cantor dust graphic;
the base case (iterations == 0
) just draws a square.
Let's visualize the evolution from a normal square to Cantor dust with growing recursion depths (larger and larger iteration
counts).
Cantor dust is related to other fractals. Feel free to explore them. Here are some ideas:
Instead of keeping the four corners of the three-by-three grid, you also could keep other grid cells. For example, you could generate a SierpiĹski carpet (similar to the SierpiĹski triangle), by removing only the middle cell of the three-by-three grid.
The Cantor dust we produced here is a two-dimensional variant of the Cantor set. You could visualize a one-dimensional Cantor set, and you could visualize the evolution of the one-dimensional Cantor set by placing Cantor sets with gradually larger iteration counts above each other.
In this activity you learned about a fractal named "Cantor dust".
To compose that fractal, you used a recursive function (cantor_dust
, which calls itself).
To terminate the recursion you used an if-statement,
and to compose the three-by-three matrix, you practiced nested for-loops.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Cantor Dust
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)