marcusround
marcusround

Reputation: 75

How to rotate a PShape around its centre?

The Processing handbook (second edition) has this as example 17-10:

PShape zig;

void setup() {
  size(100, 100);
  zig = createShape();
  zig.beginShape();
  zig.fill(0);
  zig.noStroke();
  zig.vertex(10, 0);
  zig.vertex(100, 30);
  zig.vertex(90, 70);
  zig.vertex(100, 70);
  zig.vertex(10, 90);
  zig.vertex(50, 40);
  zig.endShape();
  zig.scale(0.7);
  zig.translate(-50, -50);
}

void draw() {
  background(204);
  shape(zig, 50, 50);
  zig.rotate(0.01);
}

And the images in the book that accompany this example show the shape rotating around the centre of the canvas.

However when I run that exact code, the shape rotates around the [0,0] point at the top left of canvas.

The -50, -50 translation and 50, 50 co-ordinates do seem to imply to me (based on my limited understanding) that the rotation should be centred around the [50, 50] point but this doesn't bear out when I run the code? Any ideas as to how I can get the shape to rotate around its centre? I mean specifically using the zig.rotate() method, I know you can translate & rotate the canvas as a whole but I want to rotate specifically this shape around its centre. Can you change a PShapes origin point after creating it? Thanks!

Upvotes: 3

Views: 1117

Answers (1)

ciaron
ciaron

Reputation: 1159

I was able to reproduce this in 3.0a10 using the default renderer. If I specify P2D as the renderer size(100,100,P2D), the PShape rotates about the centre-point, as expected. Specifying P2D in version 2.2.1 also works.

So I think this looks like a bug. Maybe it's worth opening an issue on the Processing Github?

Update: there already seems to be an open issue for this.

Upvotes: 2

Related Questions