【发布时间】:2018-01-04 03:02:10
【问题描述】:
我一直在尝试为一个项目旋转一个正方形,我进行了研究,并认为我有正确的公式来计算旋转点。我计算这些点,就好像它们是围绕正方形中心的个体一样。如何解决?
//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;
}
【问题讨论】:
-
你需要澄清一点。你认为你有正确的公式,那么当你实施它时发生了什么?你预期的行为是什么?你得到了什么错误?
-
当我运行它时,第一帧是屏幕中心的正方形,第二帧是向左平移的正方形,所有点都以随机角度使正方形更像平行四边形,第三帧是向右平移的正方形,但它又是一个常规正方形,然后在平行四边形和正方形之间交替,直到它离开屏幕
-
另一件事是,当它只是一个椭圆围绕另一个椭圆旋转时,该方法可以正常工作。所以我很确定这不是其中的方法或公式错误,而是我从两点到正方形的实现。
-
等一下,围绕椭圆旋转的椭圆不再起作用了,我想我确实把公式弄乱了或其他什么
-
请尝试正确格式化您的代码。我已经删除了
<code>块(从现在开始只需使用代码按钮),但您仍然应该修复缩进。
标签: animation math rotation drawing processing