[英]Rotation of square by center using rotation matrix
我正在尝试使用带有此实现的旋转矩阵来旋转正方形。
void Square::rotateVertices(std::vector<float> &vertices)
{
float px = (vertices[0]+vertices[2])/2;
float py = (vertices[1]+vertices[5])/2;
for (int i = 0; i < 4; i++)
{
float x1 = vertices[i*2];
float y1 = vertices[i*2+1];
vertices[i * 2] = px + (x1 -px)*cos(angle) - (y1 -py)*sin(angle);
vertices[(i * 2) + 1] = py + (x1 -px)*sin(angle) + (y1 -py)*cos(angle);
}
}
使用以下代码计算顶点:
float ay = (size / float(height) / 2.f), ax = (size / float(width) / 2.f);
std::vector<float> vertices = {
2 * (((x) - float(width) / 2) / float(width)) - ax, 2 * ((y - float(height)) / float(height)) - ay + 1.0f,
2 * (((x) - float(width) / 2) / float(width)) + ax, 2 * ((y - float(height)) / float(height)) - ay + 1.0f,
2 * (((x) - float(width) / 2) / float(width)) + ax, 2 * ((y - float(height)) / float(height)) + ay + 1.0f,
2 * (((x) - float(width) / 2) / float(width)) - ax, 2 * ((y - float(height)) / float(height)) + ay + 1.0f};
rotateVertices(vertices);
在哪里:
计算后的正方形对于angle=0
看起来很正常,但是当我检查了 ie 时。 angle=M_PI/4
应该将正方形旋转 45 度,旋转适用于正方形到矩形的奇怪变换。(如图所示)
当然我很想保持比例,但我现在想不出解决办法。 如果有人能想到快速修复,或者能指出我正确的方向,我会很高兴。
那是因为您的正方形是矩形。 我的水晶球知道这一点,因为它的宽度和高度是分开计算的,因此,您的意思是让它们不同:
// if you wanted these to be the same you wouldn't calculate them separately
float ay = (size / float(height) / 2.f), ax = (size / float(width) / 2.f);
它看起来像正方形的唯一原因是屏幕投影也被扭曲以抵消它。
当您旋转矩形时,它们不再抵消。
您应该绘制一个具有相同宽度和高度的正方形,然后修复将正方形转换为屏幕坐标的矩阵,这样当您渲染具有相同宽度和高度的东西时,它在屏幕上会得到相同的宽度和高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.