EARTH 801
Computation and Visualization in the Earth Sciences

Lesson 5: Another example of rotation

PrintPrint

Probably the most important thing to remember about rotation is the following: First translate, then rotate. I will say it again. First translate, then rotate. When you get unexpected results from a rotate() command, it is usually because you forgot to translate first, so your shape flies off the display window where you can't see it anymore.

Here's another example of a program that uses scale(), translate(), and rotate().

Example 5.11 Gear program

//interlocking gears

float x = 100;
float y = 100;
float gearAngle1 = 0;
float gearAngle2 = 0;
float gearAngle3 = 0;

void setup(){
size(400,300);

}

void draw(){
  background(10,10,247); 
  smooth();
  
  pushMatrix();
  translate(x,y);
  rotate(gearAngle1);
  gear(0,0);
  popMatrix();
  gearAngle1 += .01;
 
  pushMatrix();
  translate(x+125,y+20);
  rotate(gearAngle2);
  scale(2);
  gear(0,0);
  popMatrix();
  gearAngle2 -=.005;

  pushMatrix();
  translate(x+133,y+135);
  rotate(gearAngle3);
  scale(0.75);
  gear(0,0);
  popMatrix();
  gearAngle3 +=.015;
}


void gear(float x, float y) {
  pushMatrix();
  translate(x,y);
  stroke(247,117,10,120);
  fill(247,200,10);
  ellipse(0,0,30,30);
  for (int i = 0; i < 12; i++) {
    strokeWeight(5);
    rotate(PI/6);
    line(0,0,30,30);
}
popMatrix();
}
orange gears rotate around their own centers
Screenshot of gear program above.
E. Richardson