Rotate multiple elements on it's center - celullar automata

Hi! i have something similar to an celullar automata, multiple shapes with separation on x and y coordinate. The generation of the shape of each object is random (3 possible shapes) and i want them to be random rotated (this will be static, will not change).

This is the draw method where I create the shapes, if I translate and rotate the elements I get some weird results and also I lost the x and y position given in the constructor, wich i use on a distributor class to get access to each object by a mouse click.

void draw() {

int w = 30;

if (emerger) {
} else {


translate(x, y);
if (type== 1) { 
  rect(x, y, w*escala, w*scale);
} else if (type== 2) {
  rect(x, y, w+10*scale, w*scale);
} else if (type== 3) {
  rect(x, y, w+10*scale, w*scale);
  rect(x, y, w-20*scale, w-20*scale);


How can i create a rotated(0,90,180,270) shape without changing its position?


Original not rotated Rotated but losing coordinates




  • When you apply translate and rotate, you usually draw at 0, 0. Drawing at x, y is duplicate of translate(x, y).

    To rotate on a point (usually the center), you translate this point to 0, 0 (ie. translate(-hw, -hh); half-width, half-height for the center), do the rotation, then translate the coordinates to x, y.

  • Hi PhiLho i don't get it yet, what the half-w and half-h in this case? i have to do something like this? This obviously doesnt work

    //w and h are the particle size

    pushMatrix(); translate(x-w, y-h); rotate(radians(90)); translate(x,y) if (type== 1) { rect(x, y, wescala, wscale); } else if (type== 2) { rect(x, y, w+10scale, wscale); } else if (type== 3) { rect(x, y, w+10scale, wscale); rect(x, y, w-20scale, w-20scale); } popMatrix();

  • Answer ✓

    Instead of drawing a rectangle with rect(x,y,w,h), do:

    // rotate() here if you want it to rotate by some amount.
  • TfGuy44 thanks for your answer, it worked! PhiLho thank you too for your help. Have a good day!!

Sign In or Register to comment.