excessive translate and anti-translate

edited February 2016 in Questions about Code

I have a functioning bit of code, but I feel like I must be doing it very inefficiently. I have 4 rectangles that need to rotate in different locations. Each one rotates around its own center. Like I said, the code works, but it calls the translate and rotate command a lot. This is because every translate and rotate needs to be anti-translated and anti-rotated before the next one can be done. Does anybody have any better ideas? I could minimize translates by translating to the new location from the current location instead of reseting it, but that seems only trivially simpler.

translate((456+3.465/2),(639+14.571/2));
rotate(rot_circ);
rect(-3.465/2,-14.571/2,3.465,14.571);
rotate(-rot_circ);
translate(-456-3.465/2,-639-14.571/2);

//coolant pump
translate((449.441+3.465/2),(362.833+14.571/2));
rotate(rot_cool);
rect(-3.465/2,-14.571/2,3.465,14.571);
rotate(-rot_cool);
translate(-(449.441+3.465/2),-(362.833+14.571/2));

//thermal recovery pump
translate((595.862+3.465/2),(301.785+14.571/2));
rotate(rot_tr);
rect(-3.465/2,-14.571/2,3.465,14.571);
rotate(-rot_tr);
translate(-(595.862+3.465/2),-(301.785+14.571/2));

//FPS extraction pump
translate((911+14.571/2),(659.5+3.465/2));
rotate(-rot_FPS);
rect(-14.571/2,-3.465/2,14.571,3.465);
rotate(rot_FPS);
translate(-(911+14.571/2),-(659.5+3.465/2));

//water extraction pump
translate((1178+3.465/2),(672.775+14.571/2));
rotate(rot_water);
rect(-3.465/2,-14.571/2,3.465,14.571);
rotate(-rot_water);
translate(-(1178+3.465/2),-(672.775+14.571/2));

Answers

  • Try using pushMatrix() and popMatrix().

  • Answer ✓
    pushMatrix(); // save current translation matrix
    translate((456+3.465/2),(639+14.571/2));
    rotate(rot_circ);
    rect(-3.465/2,-14.571/2,3.465,14.571);
    popMatrix(); // restore current translation matrix
    
    //coolant pump
    pushMatrix(); // save current translation matrix
    translate((449.441+3.465/2),(362.833+14.571/2));
    rotate(rot_cool);
    rect(-3.465/2,-14.571/2,3.465,14.571);
    popMatrix(); // restore current translation matrix
    
    // and so on ...
    
  • Uh... so you both realize that is the exact same number of lines.... I will just stick with the code as-is I guess.

  • Same number of lines, sure, but less math. So faster program. :-@

  • edited February 2016 Answer ✓

    Never judge a program by the number of lines of code and don't stick to your original code because it is very inefficient to reverse the transformations. Also it is dependent on the programmer not making an error.

    The proper way to do this is use pushMatrix() and popMatrix as shown.

  • you could even write a function rectMy that takes 2 values for translate

    1 for rotate and

    4 for the rect

  • also with rectMode(center); you could say rect(0,0, w, h); iirc

  • Thanks everybody! I guess what I should have written in my previous post was "Why is that any better". I was looking for the broader context. Now I have it and will implement it!

    Thanks for the help!

Sign In or Register to comment.