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

// Example 14-5: Rectangle rotating around center

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

void draw() {
  background(255);
  stroke(0);
  fill(175);
  
  // Translate origin to center
  translate(width/2,height/2);
  
  // The greek letter, theta, is often used as the name of a variable to store an angle
  // The angle ranges from 0 to PI, based on the ratio of mouseX location to the sketch's width.
  float theta = PI*mouseX / width; 
  
  // Rotate by the angle theta
  rotate(theta);
  
  // Display rectangle with CENTER mode
  rectMode(CENTER);
  rect(0,0,100,100);
}
  • Vmln8r

    How would you then move the rectangle elsewhere, while still having it rotate about it’s centre?

  • shiffman

    you would need variables instead of width/2, height/2, i.e.

    translate(x,y);

  • Arnab Sarkar

    Hi Daniel,
    Thanks for your tutorials.
    I was wondering that whether it’s possible to use the function rotate() to rotate 2 objects(say rectangles) in their own axis.

  • shiffman