如何在处理中旋转方块?

问题描述 投票:0回答:1

我一直试图为一个项目旋转一个正方形,我做了研究,并认为我有正确的公式来计算旋转点。我计算得分就好像他们是在广场中心周围的个体一样。怎么解决?

//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;
}
animation math rotation drawing processing
1个回答
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;

对于其他要点也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.