处理中,使用矩阵旋转矩形? [英] Processing, Rotate rectangle using matrix?

查看:131
本文介绍了处理中,使用矩阵旋转矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试不使用rotate函数而是使用矩阵来旋转矩形..我知道如何使用矩阵旋转线,但是我所有旋转矩形的尝试都失败了.

I am trying to rotate a rectangle without using the rotate function, but instead using a matrix..I know how to rotate a line using a matrix but all my attempts to rotate a rectangle have failed.

我不认为这是用完了,但是这是我的代码来旋转行.

I dont think this is use full but heres is my code that rotates the line.

float[][] rotation;
float[] position;
float theta = 180;
float pointX;
float pointY;
void setup() {

  frameRate(60);
  size(600, 600);

  pointX = 0;
  pointY = 0;

 rotation = new float[2][2];
 position = new float[8];
}

 void draw() {
 background(200);
 theta = mouseX;

 position[0] = mouseY;
 position[1] = mouseY;

 position[2] = -mouseY;
 position[3] = mouseY;

 rotation[0][0] = cos(radians(theta));
 rotation[0][1] = -sin(radians(theta));
 rotation[1][0] = sin(radians(theta));
 rotation[1][1] = cos(radians(theta));

 float newpos[] = new float[8]; 

 newpos[0] += position[0] * rotation[0][0];
 newpos[1] += position[1] * rotation[0][1];  

 translate(width/2, height/2);

 line(0, 0, pointX+newpos[0], pointY+newpos[1]);
 line(0, 0, pointX+newpos[0] * -1, pointY+newpos[1] * -1);

}

推荐答案

尽管线的行为正确,但这是偶然的……您在计算该点的新x和y时具有至关重要的部分,而不是应有的已经.正如您在维基百科中所发现的那样,您需要在矩阵中计算正弦和余弦正确地做到了,但是在创建新点时,您并没有完全做到这一点:

Although the lines behaves properly it is by chance... You have a crucial part of the calculation of the new x and y of the point not as it should have been. As you can find in wikipedia, you need to calculate the sin and cos in the matrix as you properly did, but when creating the new point you don't exactly do this:

这篇关于处理中,使用矩阵旋转矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆