### Unit 40

by golan @ 2:06 pm September 26, 2011

Assignments: Due Wednesday: Looking Outwards (3); Readings.

Transforms; Arrays.

Read: Getting Started with Processing, 141-157 ( Arrays)
Read: Getting Started with Processing, 104-113 ( Transforms)

A Rotating Square: Two ways, different advantages.

```int drawState = 0; void setup() { size (300, 300); smooth(); }   void keyPressed() { drawState = (drawState+1)%3; }     //========================================================= void draw() {   fill(255); text("Draw State = " + drawState, 15, 20);   switch (drawState) { case 0: draw0(); break; case 1: draw1(); break; case 2: draw2(); break; }   fill(100, 64); rect(0, 0, width, height); }     //========================================================= void draw0() { // Use rotate and translate to compute the vertices for us. pushMatrix(); translate(width/2, height/2); rotate (millis()/400.0); rect (-50, -50, 100, 100); popMatrix(); }     //========================================================= void draw1() { // Compute the vertices mathematically, ourselves. float cx = width/2; float cy = height/2; float T = millis()/400.0 - QUARTER_PI; float r = 50.0 * sqrt(2.0);   beginShape(); vertex(cx + r*cos(T ), cy + r*sin(T )); vertex(cx + r*cos(T+HALF_PI*1), cy + r*sin(T+HALF_PI*1)); vertex(cx + r*cos(T+HALF_PI*2), cy + r*sin(T+HALF_PI*2)); vertex(cx + r*cos(T+HALF_PI*3), cy + r*sin(T+HALF_PI*3)); endShape(CLOSE); }       //========================================================= void draw2() { // Compute the vertices mathematically, ourselves. float cx = width/2; float cy = height/2; float T = millis()/400.0 - QUARTER_PI; float r = 50.0 * sqrt(2.0);   float dx = 15*sin(millis()/50.0); float dy = 15*cos(millis()/50.0); float dT = 0.4 * sin(millis()/80.0);   beginShape(); vertex(cx + r*cos(T+dT ), cy + r*sin(T+dT )); vertex(cx + r*cos(T+HALF_PI*1), cy + r*sin(T+HALF_PI*1)); vertex(cx + r*cos(T+HALF_PI*2) + dx, cy + r*sin(T+HALF_PI*2) + dy); vertex(cx + r*cos(T+HALF_PI*3), cy + r*sin(T+HALF_PI*3)); endShape(CLOSE); }```

Limbs: Here’s an Arm

```void setup() { size(500, 500); }     void draw() { background(100); fill(255,255,255, 128); smooth();   pushMatrix();   translate(150, 150); ellipse(0,0, 10,10); rect(0,0, 100, 100);   translate(75,75); ellipse(0,0, 10,10); rect(0,0, 50,150);   translate(25,125); ellipse(0,0, 10,10); rect(0,0, 50,50);   popMatrix(); }```

Limbs: Here’s an Arm, Moving

```void setup() { size(500, 500); }     void draw() { background(100); fill(255,255,255, 128); smooth();   float ang1 = 0.5 * (sin(millis()/2000.0)); float ang2 = 0.7 * (sin(millis()/1111.0)); float ang3 = 1.2 * (sin(millis()/ 313.0));   pushMatrix();   translate(150, 150); rotate( ang1 ); ellipse(0,0, 10,10); rect(0,0, 100, 100);   translate(75,75); rotate( ang2 ); ellipse(0,0, 10,10); rect(0,0, 50,150);   translate(25,125); rotate( ang3 ); ellipse(0,0, 10,10); rect(0,0, 50,50);   popMatrix(); }```

Noisy Limb:

```void setup() { size(500, 500); }   void draw() { background(100); fill(255,255,255, 128); smooth();   float ang1 = 0.5 * (noise(millis()/2000.0) - 0.5); float ang2 = 0.7 * (noise(millis()/1111.0) - 0.5); float ang3 = 1.2 * (noise(millis()/ 313.0) - 0.5);   pushMatrix();   translate(150, 150); rotate( ang1 ); ellipse(0,0, 10,10); rect(0,0, 100, 100);   translate(75,75); rotate( ang2 ); ellipse(0,0, 10,10); rect(0,0, 50,150);   translate(25,125); rotate( ang3 ); ellipse(0,0, 10,10); rect(0,0, 50,50);   popMatrix(); }```

A Point Recorder:

```ArrayList xPoints; ArrayList yPoints;   void setup() { size(400, 400); xPoints = new ArrayList(); yPoints = new ArrayList(); }   void draw() { background(200); noFill();   int n = xPoints.size(); beginShape(); for (int i=0; i<n; i++) { float px = ((Float)(xPoints.get(i))); float py = ((Float)(yPoints.get(i))); vertex(px, py); } endShape(); }   void mousePressed() { xPoints.add((float)mouseX); yPoints.add((float)mouseY); }       void keyPressed() { print("float xPoints[] = {"); int nx = xPoints.size(); for (int i=0; i<nx; i++) { float px = ((Float)(xPoints.get(i))); if (i != (nx-1)) { print (px + ", "); } else { print (px); } } println("};");   print("float yPoints[] = {"); int ny = yPoints.size(); for (int i=0; i<ny; i++) { float py = ((Float)(yPoints.get(i))); if (i != (ny-1)) { print (py + ", "); } else { print (py); } } println("};"); }```