How to rotate a PShape around its centre?

南楼画角 提交于 2019-12-23 17:19:56

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/30990708/how-to-rotate-a-pshape-around-its-centre

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!