FORM+CODE in Design, Art, and Architecture

Repeat: EMBEDDED ITERATION

All programming languages can repeat an action, such as drawing the same shape over and over. When one repetition sequence is embedded within another, the effect multiplies. For example, if the action of drawing five lines is repeated ten times, fifty lines are drawn. This simple technique can be used to explore many kinds of patterns.

Each of these images was generated from the same grid of points. Sixteen elements along the x-axis and eleven along the y-axis combine to form 176 coordinates. Changing just one line of code produces the differences between theses images.

/** * Repeat: Embedded Iteration * from Form+Code in Design, Art, and Architecture * by Casey Reas, Chandler McWilliams, and LUST * Princeton Architectural Press, 2010 * ISBN 9781568989372 * * This code was written for Processing 1.2+ * Get Processing at http://www.processing.org/download */ int option = 1; void setup() {   size(400, 300);   smooth();   noFill(); } void draw() {   background(255);   if (option == 1) {     // Option 1: Stitches     for (int x = 50; x <= width-50; x += 20) {       for (int y = 50; y <= height-50; y+=20) {         line(x-5, y-5, x+5, y+5);         line(x+5, y-5, x-5, y+5);       }     }   }    else if (option == 2) {     // Option 2: Perspective     for (int x = 50; x <= width-50; x += 20) {       for (int y = 50; y <= height-50; y+=20) {         line(x, y, width/2, height/2);       }     }   }    else if (option == 3) {     // Option 3: Overlapping circles     for (int x = 50; x <= width-50; x += 20) {       for (int y = 50; y <= height-50; y+=20) {         ellipse(x, y, 40, 40);       }     }   }    else if (option == 4) {     // Option 4: Rotating arcs     int count = 120;     for (int x = 50; x <= width-50; x += 20) {       for (int y = 50; y <= height-50; y+=20) {         float s = map(count, 120, 0, 0, TWO_PI*2);         arc(x, y, 14, 14, s, s + PI);         count--;       }     }   }    else if (option == 5) {     // Option 5: Groups of five     for (int x = 50; x < width-50; x += 20) {       for (int y = 50; y < height-50; y+=20) {         //rect(x-10, y-10, 22, 22);         for (int i = 0; i < 16; i+=4) {           line(x + i, y, x + i, y + 12);         }         line(x, y, x + 12, y + 12);       }     }   } } void mousePressed() {   option++;   if (option > 5) option = 1; }

Contributed Examples

DOWNLOAD ALL CODE EXAMPLES

We are looking for implementations of the code examples in other programming languages to post on the site. If you would like to submit a sample, or if you find a bug, please write to