Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 14-17: Nested push and pop

// Global angle for rotation
float theta = 0;

void setup() {
  size(200, 200);
  smooth();
}

void draw() {
  background(255);
  stroke(0);
  
  // Translate to center of window
  translate(width/2,height/2);
  
  // Loop from 0 to 360 degrees (2*PI radians)
  for(float i = 0; i < TWO_PI; i += 0.2) {
    
    // Push, rotate and draw a line!
    // The transformation state is saved at the beginning of each cycle through the for loop and restored at the end. 
    // Try commenting out these lines to see the difference!
    pushMatrix(); 
    rotate(theta + i);
    line(0,0,100,0);
    
    // Loop from 0 to 360 degrees (2*PI radians)
    for(float j = 0; j < TWO_PI; j += 0.5) {
      // Push, translate, rotate and draw a line!
      pushMatrix();
      translate(100,0);
      rotate(-theta-j);
      line(0,0,50,0);
      // We're done with the inside loop, pop!
      popMatrix();
    }
    
    // We're done with the outside loop, pop!
    popMatrix();
  }
  
  // Increment theta
  theta += 0.01;
}

2 Comments

»

  1. Not sure what the purpose of endShape() is here. According to Processing Reference it “..may only be called after beginShape().” Commenting it out seems to make no difference.

    Comment by Kit — August 5, 2009 @ 7:26 am

  2. Whoops, this is an error. Not sure where it came from, must be leftover from an earlier verison of this example that used beginShape()? I’ve removed it now, thanks!

    Comment by admin — August 5, 2009 @ 1:26 pm

Leave a comment