【问题标题】:How to rotate a square in processing?如何在加工中旋转正方形?
【发布时间】: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


【解决方案1】:

有两件事:

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;

其他点也一样。

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多