如何在处理中旋转方块?

时间:2018-01-04 03:02:10

标签: animation math rotation drawing processing

我一直试图为一个项目旋转一个方格,我已经完成了研究,并认为我有正确的公式来计算旋转点。我计算得分就好像他们在广场中心周围一样。如何解决?

//Declaring variables
float x0, y0, xo, yo,x1,y1,x2,y2,x3,y3, theta, newx, newy, s, c;
void setup() {
size (800,800);
//To debug 
//frameRate(1);
fill(0);
//Initializing variables
xo = 400;
yo = 400;
x0 = 350;
y0 = 450;
x1 = 350;
y1 = 350;
x2 = 450;
y2 = 350;
x3 = 450;
y3 = 450;
theta = radians(5);
s = sin(theta);
c = cos(theta);
}
void draw() {
//Reseting the background
background(255);
//Drawing the square
quad(x0,y0,x1,y1,x2,y2,x3,y3);
//Doing the rotations
x0 = rotateX(x0,y0);
y0 = rotateY(x0,y0);
x1 = rotateX(x1,y1);
y1 = rotateY(x1,y1);
x2 = rotateX(x2,y2);
y2 = rotateY(x2,y2);
x3 = rotateX(x3,y3);
y3 = rotateY(x3,y3);
}
//Rotate x coordinate method
float rotateX(float x, float y) {
x -= xo;
newx = x * c - y * s;
x = newx + xo;
return x;
}
//Rotate y coordinate method
float rotateY(float x, float y) {
y -= yo;
newy = x * s - y * c;
y = newy + yo;
return y;
}

1 个答案:

答案 0 :(得分:0)

有两件事:

1)rotateY()中有一个符号错误。 y字词应该有正号:

newy = x * s + y * c;

2)当你这样做时:

x0 = rotateX(x0,y0);
y0 = rotateY(x0,y0);

...然后第一个调用修改x0,然后第二个调用使用。但第二次调用需要原始坐标正确旋转:

float x0Rotated = rotateX(x0, y0);
y0 = rotateY(x0, y0);
x0 = x0Rotated;

其他要点同样如此。