使用旋转矩阵通过中心旋转正方形

发布于 2025-01-29 09:00:34 字数 1654 浏览 3 评论 0原文

我正在尝试使用此实现的旋转矩阵旋转正方形。

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);

wery:

  • 宽度 - 窗口宽度
  • 高度 - 窗口高度
  • 大小 -

计算平方后的正方形大小看起来正常,Angle = 0,但是当我检查了IE时。 Angle = M_PI/4应该旋转正方形45度,旋转适用于正方形向矩形的奇怪转换。(如图所示)

当然,我很想保留比例,但是目前我无法想到解决方案。如果有人能想到快速修复,或者可以将我指向正确的方向,我会很高兴。

I'm trying to rotate a square using rotation matrix with this implementation.

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);
    }
}

Vertices are calculated using code below:

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);

where:

  • width - window width
  • height - window height
  • size - size of square

After calculation square looks normal for angle=0, but when I've checked ie. angle=M_PI/4 which should rotate square 45 degrees, rotation applies with strange transformation of square to rectangle.(as shown in pictures)
Squares rotation example

Of course I would love to have the proportions kept, but I cannot think of the solution at the moment. If anybody could think of fast fix, or could point me in the correct direction I would be pleased.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒一口袋星星 2025-02-05 09:00:34

那是因为您的正方形一个矩形。我的水晶球之所以知道这一点,是因为其宽度和高度是单独计算的,因此,您的意思是让它们与众不同:

// 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);

看起来像是正方形的唯一原因是因为屏幕投影也被扭曲以取消它。

当您旋转矩形时,它们将不再取消。

您应该绘制一个具有相同宽度和高度的正方形,然后修复将正方形转换为屏幕坐标的矩阵,以便当您渲染具有相同宽度和高度的东西时,它确实会在屏幕上获得相同的宽度和高度。

That's because your square is a rectangle. My crystal ball knows this because its width and height are calculated separately, and therefore, you meant for them to be different:

// 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);

The only reason it looks like a square is because the screen projection is also distorted to cancel it out.

When you rotate the rectangle they no longer cancel out.

You should draw a square with the same width and height, and then fix the matrices that convert the square into screen coordinates, so that when you render something with the same width and height it does get the same width and height on the screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文