# De Stijl Recursion

## What is this?

An applet that generates "Mondrian style" art, using a recursive algorithm. It is exceedingly unlikely that any generated images will resemble a real Mondrian painting, but a careful selection of weights on colors and divisions can get fairly close!

The following text is from the Wikipedia entry on Piet Mondrian:
Pieter Cornelis "Piet" Mondriaan (7 March 1872 – 1 February 1944), after 1906 Mondrian, was a Dutch painter and theoretician who is regarded as one of the greatest artists of the 20th century. He is known for being one of the pioneers of 20th century abstract art, as he changed his artistic direction from figurative painting to an increasingly abstract style, until he reached a point where his artistic vocabulary was reduced to simple geometric elements.

He was a contributor to the De Stijl art movement and group, which he co-founded with Theo van Doesburg. He evolved a non-representational form which he termed Neoplasticism. This was the new 'pure plastic art' which he believed was necessary in order to create 'universal beauty'. To express this, Mondrian eventually decided to limit his formal vocabulary to the three primary colors (red, blue and yellow), the three primary values (black, white and gray) and the two primary directions (horizontal and vertical).

Mondrian's work had an enormous influence on 20th century art, influencing not only the course of abstract painting and numerous major styles and art movements (e.g. Color Field painting, Abstract Expressionism and Minimalism), but also fields outside the domain of painting, such as design, architecture and fashion.

## Generating pseudocode

The pseudocode below will generate a Mondrian style image, but treats all choices of division and color equally. The code for the applet on this page allows more control over the image, but is slightly more complicated due to the available options. For a stripped down version of the code, view the full page Mondrian generator here.

```	  	function draw( x, y, w, h ):
select random color
draw rectangle with top left corner (x,y) of width w and height h

function mondrian( x, y, w, h ):
if ( w or h is below threshold ):
escape function (break)
else
draw( x, y, w, h )
choose random value from 0, 1, or 2
if ( value is 0 ):
do nothing
else if ( value is 1 ):
neww := random value between 0 and w
mondrian( x, y, neww, h )
mondrian( x+neww, y, w-neww, h )
else if ( value is 2 ):
newh := random value between 0 and h
mondrian( x, y, w, newh )
mondrian( x, y+newh, w, h-newh )

```
An initial call of the `mondrian` function begins the recursion, usually by passing the position and size of a rectangle that would fill the canvas.

## Recursion vs Iteration

They tend to be used interchangeably, but there is a difference. Iteration involves calling a portion of code repeatedly, like printing messages in a for loop. Recursion refers to a function calling itself. Often, there are ways to write iterative statements as recursive statements and vice-versa, which is why they're frequently paired together. Iteration tends to be more intuitive, and less prone to resulting in stack overflow errors. Recursive functions are generally more efficient and elegant but harder to get used to.

For some iterative examples, go here and tread cautiously on the step buttons.