home/ atoms/ processing-translation-matrix

translate() shifts Processing's coordinate origin, and pushMatrix/popMatrix save and restore transform state

Instead of computing absolute screen positions for every shape element, Processing’s translate(x,y) moves the coordinate origin. All shapes drawn afterwards are offset by that amount. translate() calls accumulate — two calls of translate(10,10) produce a total offset of (20,20). To prevent accumulation from breaking later code, pushMatrix() saves the current transform state and popMatrix() restores it. They must always appear in matched pairs. This stack-based system allows isolated transformations: push, transform, draw, pop, without affecting surrounding code.

Examples

pushMatrix(); translate(50, 50); rotate(PI/4); rect(-10, -10, 20, 20); popMatrix(); // coordinate origin is restored here

Assessment

Write code that draws 5 rotated rectangles arranged in a circle around the centre of the canvas using translate and rotate, with pushMatrix/popMatrix for each.

“The pushMatrix() function records the current state of all transformations so that a program can return to it later. To return to the previous state, use popMatrix().”
corpus · processing-handbook-no-login-mirror-pdf-reas-and-fry · chunk 35