"
I have the former working nicely"
Good! What is the problem, then?
"
but am stuck otherwise"
Stuck where? What is "otherwise"?
"
Is it best to"
The "best" way is the way working as you want...
Sometime, you have to do a translation, rotation then translation, particularly when you want to rotate at a given point of a shape.
Otherwise, translate() then rotate() is enough.
Demonstration:
- int shapeW = 150;
- int shapeH = 100;
- void setup()
- {
- size (600, 600);
- smooth();
- noStroke();
- ellipseMode(CENTER);
- }
- float angle;
- void draw ()
- {
- background (255);
- // Do the rotation
- rotate(radians(angle++));
- fill(#00AA00);
- // x, y always 0 since translations do the job of placing
- rect(0, 0, shapeW, shapeH);
- // Mark the rotation point
- fill(#FF0000);
- ellipse(0, 0, 8, 8);
- // Mark the bottom-rigth corner
- fill(#0000FF);
- ellipse(shapeW-1, shapeH-1, 8, 8);
- }
The rectangle rotates around its top-left corner, in the top-left corner of the sketch.
Now, let's rotate the shape in a point a third of its width / height:
- void draw ()
- {
- background (255);
-
- // Do the rotation
- rotate(radians(angle++));
- // A translation to change the rotation point in the shape
- float rotX = shapeW / 3;
- float rotY = shapeH / 3;
- translate(-rotX, -rotY);
-
- fill(#00AA00);
- // x, y always 0 since translations do the job of placing
- rect(0, 0, shapeW, shapeH);
-
- // Mark the rotation point
- fill(#FF0000);
- ellipse(rotX, rotY, 8, 8);
- // Mark the bottom-rigth corner
- fill(#0000FF);
- ellipse(shapeW-1, shapeH-1, 8, 8);
- }
You can then do the rotation anywhere in the sketch:
- void draw ()
- {
- background (255);
-
- // Move the origin to the point where we want the rotation to occur
- translate(width / 3, height / 3);
- // Do the rotation
- rotate(radians(angle++));
- // A translation to change the rotation point in the shape
- float rotX = shapeW / 3;
- float rotY = shapeH / 3;
- translate(-rotX, -rotY);
-
- fill(#00AA00);
- // x, y always 0 since translations do the job of placing
- rect(0, 0, shapeW, shapeH);
-
- // Mark the center point
- fill(#FF0000);
- ellipse(rotX, rotY, 8, 8);
-
- // Mark the bottom-rigth corner
- fill(#0000FF);
- ellipse(shapeW-1, shapeH-1, 8, 8);
- }
You can put the rotation point outside of the shape:
- float rotX = -shapeW / 3;
- float rotY = -shapeH / 3;
And so on.