如何在处理中旋转正方形



我一直在尝试为一个项目旋转一个正方形,我已经做了研究,并认为我有正确的公式来计算旋转的点。我计算点,就好像它们是围绕正方形中心的单个点一样。如何解决?

//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( 您在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;

其他点也是如此。

最新更新