Rotate problem with PGraphics

Can someone please help me to get rotate to work with pgraphics in the following code. This code calls a function that draws a ‘petal’ that I would like to rotate and which was working perfectly without PGraphics.

<
p.beginDraw();
p.noFill();
rt1 = rx(); // random control points
rt2 = ry();

int num = int(random(5, 21));
int angle = int(random(-360, 360));

int dir = int(random(1, 3));
if (dir == 1) dir = -360;
if (dir == 2) dir = 360;

for (int j = 0; j<num; j++) {
p.pushMatrix();
p.rotate(radians(dir) / num * j);
drawPetal(p, x, y, r, rt1, rt2 );

p.popMatrix();

}
p.endDraw();

Thank you!

Welcome to the forum.

Code looks solid, I don’t see any problem there. I had a problem with transformations and PGraphics. I applied transformation each time I draw stuff to PGraphics. But it behaves differently than draw() cycle. It doesn’t reset transformations at start of each draw cycle(), it just kept piling them up and my drawings were after couple of loops outside of drawing area.

I guess you have bumped in to same problem. For me solution was simple. I used identical transformation each time so I moved it to setup(). For varying transformations you need to reset the stack of transformations in code.

1 Like

Hello @druuupppa ,

Please format and post your code as per instructions here:
https://discourse.processing.org/faq#format-your-code

See Guidelines - Asking Questions

I suggest you take a look here:

Did you include the necessary p. object prefix for all the elements you want in your PGraphics?

You are also passing your current PGraphics p to the function. Is that intended? Give that some consideration.

A working, minimal, formatted example in static mode:

PGraphics pg;

size(100, 100);

pg = createGraphics(100, 100);

for(int i = 0; i<=90; i+=9)
  {
  pg.beginDraw();
  pg.pushMatrix();
  pg.noFill();
  pg.rotate(radians(i));
  pg.ellipse(50, 0, random(5, 20), 5); // drawPetal()
  pg.popMatrix();
  pg.endDraw();
  }

image(pg, 0, 0);

image

Start with a simple working example and build on that one step at a time.

References:

:)

Hi. Thank you for your example. Working on it.

1 Like

Thanks for your helpful response. Working on it.

1 Like