# Rotate problem with PGraphics

**URL:** <https://discourse.processing.org/t/rotate-problem-with-pgraphics/37753>\
**Category:** Coding Questions\
**Created:** [June 28, 2022, 11:16pm UTC](https://discourse.processing.org/t/rotate-problem-with-pgraphics/37753 "2022-06-28T23:16:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![druuupppa](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@druuupppa](https://discourse.processing.org/u/druuupppa)\
**Post date:** [June 28, 2022, 11:16pm UTC](https://discourse.processing.org/t/rotate-problem-with-pgraphics/37753/1 "2022-06-28T23:16:08Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [June 29, 2022, 3:11am UTC](https://discourse.processing.org/t/rotate-problem-with-pgraphics/37753/2 "2022-06-29T03:11:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 29, 2022, 10:19pm UTC](https://discourse.processing.org/t/rotate-problem-with-pgraphics/37753/3 "2022-06-29T22:19:19Z")

</div>

Hello @druuupppa ,

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

See [Guidelines - Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147)

I suggest you take a look here:

> [@druuupppa](#):
>
> drawPetal(p, x, y, r, rt1, rt2 );

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:

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/7/e71ec788a53efa3674eed637ff61aa00696d74eb.png)

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

References:

> **[PGraphics / Reference](https://processing.org/reference/PGraphics.html)**
>
> Main graphics and rendering context, as well as the base API implementation for processing "core". Use this class if you need to draw into an off-screen graphics buffer. A PGraphics object can be cons…

> **[Render Techniques](https://processing.org/tutorials/rendering/)**
>
> Tools for rendering geometries in Processing.

`:)`

---

<div class="post-metadata">

**Author:** ![druuupppa](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@druuupppa](https://discourse.processing.org/u/druuupppa)\
**Post date:** [June 29, 2022, 11:55pm UTC](https://discourse.processing.org/t/rotate-problem-with-pgraphics/37753/4 "2022-06-29T23:55:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![druuupppa](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@druuupppa](https://discourse.processing.org/u/druuupppa)\
**Post date:** [June 29, 2022, 11:56pm UTC](https://discourse.processing.org/t/rotate-problem-with-pgraphics/37753/5 "2022-06-29T23:56:30Z")

</div>

Thanks for your helpful response. Working on it.
