On this page, you will use recursion to create abstract art in the style of Piet Mondrian.
They're like fractals in that the picture is made up of smaller pictures of the same kind. But, because of the use of randomness, the smaller pictures don't look quite like the big one. A fractal is "self-similar," which means that it looks just like a piece of itself.
Mondrian did not use a computer, but you can use recursion to model his paintings and create similar images. The base case is one rectangle with a black border around it. That's more complicated than any base case you have worked with before, so you'll use an abstraction called rectangle
to hide the drawing script for the base case.
Some code is already created for you. There is an instruction to initialize the colors variable with a list of commands.
There are partially written blocks for the base case, rectangle
, and for the complete recursive procedure, Mondrian
.
when (r) key pressed
for the rectangle and another one with when (m) key pressed
for the Mondrian. You can include a clear
and the sprite's initial position and direction.There are also the two complete block definitions below. Read them both so you understand what they do.
rectangle
block. Your code should
One way is to draw a line of color using the width or height of the rectangle as the pen size.
rectangle
set up correctly. Each time you run it, you should see a rectangle with one of the five colors inside and a black border.Mondrian
by writing its base case, which will just call rectangle
.Mondrian helper
block as an abstraction for the part in common.Mondrian
in each part.
The 0.2 to 0.8 range makes sure that each subdivision is wide enough to see.
How can you get the negative of a value?
Buggy code? Here are some things to consider.
rectangle
and Mondrian
are state invariant. The sprite should end up in the same position and direction as it started. Check for this in each piece of code (that is, in rectangle
and in each branch of the if
statement inside Mondrian
).
rectangle
or one branch of an if
) turns, the sprite should finish facing in the same direction in which it started.go to
, set x
, set y
, or point in direction
. Instead, use blocks like turn ↻
, turn ↺
, and move
so that you can undo these changes before leaving that piece of code.Here are a few tips for specific situations.
Mondrian
in the same piece of code, it might help you debug to add a pause all
just before the second one.width × fraction
, make sure the second call moves through width × (1 - fraction)
.Mondrian
, you may need to swap width and height in the recursive call. This is possible but can be confusing to think through.Mondrian
working, it should produce pictures like this:Color is complicated. When mixing different colors of light, as your computer display does, you use red, green, and blue. But when mixing paint colors, you use red, yellow, and blue as primary.
These geometric paintings came near the end of Mondrian's career. He started out painting realistic landscapes, and moved to greater and greater abstraction over time. If you want to learn more about the range of Mondrian's work, read the Wikipedia article.