Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Created December 24, 2011 05:22
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atduskgreg/1516424 to your computer and use it in GitHub Desktop.
Save atduskgreg/1516424 to your computer and use it in GitHub Desktop.
Controlling the center of rotation in Processing
// Rotation Around a Point
// ***********************
// To acheive rotation around a given point
// - translate to that point
// - rotate
// - then translate back
// (note: in Processing this last step is implicit
// since draw() resets all transformations
// each time it runs.)
void setup() {
size(200, 200);
smooth();
}
void draw() {
background(0);
// move the center of rotation
// to the center of the sketch
translate(width/2, height/2);
// rotate around the center of the sketch
rotate(radians(frameCount));
// draw a red dot at
// the center of the sketch
fill(255, 0, 0);
ellipse(0, 0, 20, 20);
// draw a rectangle with
// its top-left corner
// at the center of rotation
fill(255);
rect(0, 0, 50, 50);
}
@rdsilver
Copy link

Nice.

@willydlw
Copy link

Thank you. This is enormously helpful. Now I have an example that has allowed me to make incremental changes and have a much clearer understanding of translate and rotate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment