计算旋转矩形的顶点 [英] Calculating vertices of a rotated rectangle

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

问题描述

伙计们,我正在尝试计算旋转矩形(2D)的顶点.

Hey guys, I am trying to calculate the vertices of a rotated rectangle (2D).

如果矩形没有旋转,这很容易,我想出了那部分.

It's easy enough if the rectangle has not been rotated, I figured that part out.

如果矩形已经旋转,我想到了两种可能的方法来计算顶点. 1)弄清楚如何将顶点从局部/对象/模型空间(我在下面指出的)转换为世界空间.老实说,我毫无头绪,如果这是最好的方法,那么我想如果能弄清楚的话,我会从中学到很多东西.

If the rectangle has been rotated, I thought of two possible ways to calculate the vertices. 1) Figure out how to transform the vertices from local/object/model space (the ones I figured out below) to world space. I honestly have no clue, and if it is the best way then I feel like I would learn a lot from it if I could figure it out...

2)使用trig找出矩形端点在世界空间中相对于矩形位置的位置.到目前为止,这一直是我一直尝试做的事情,但我还没有弄清楚该怎么做.

2) Use trig to somehow figure out where the endpoints of the rectangle are relative to the position of the rectangle in world space. This has been the way I have been trying to do up until now, I just haven't figured out how.

这里是到目前为止计算顶点的函数,谢谢您的帮助

Here's the function that calculates the vertices thus far, thanks for any help

void Rect::calculateVertices()
{
    if(m_orientation == 0) // if no rotation
    {
        setVertices(
        &Vertex( (m_position.x - (m_width / 2) * m_scaleX), (m_position.y + (m_height / 2) * m_scaleY), m_position.z), 
        &Vertex( (m_position.x + (m_width / 2) * m_scaleX), (m_position.y + (m_height / 2) * m_scaleY), m_position.z),
        &Vertex( (m_position.x + (m_width / 2) * m_scaleX), (m_position.y - (m_height / 2) * m_scaleY), m_position.z),
        &Vertex( (m_position.x - (m_width / 2) * m_scaleX), (m_position.y - (m_height / 2) * m_scaleY), m_position.z) );
    }
    else
    {
        // if the rectangle has been rotated..
    }

    //GLfloat theta = RAD_TO_DEG( atan( ((m_width/2) * m_scaleX) / ((m_height / 2) * m_scaleY) ) );
    //LOG->writeLn(&theta);

}

推荐答案

我只是变换每个点,并对每个点应用相同的旋转矩阵.如果是2D平面旋转,则如下所示:

I would just transform each point, applying the same rotation matrix to each one. If it's a 2D planar rotation, it would look like this:

x' = x*cos(t) - y*sin(t)
y' = x*sin(t) + y*cos(t)

其中(x,y)是原始点,(x',y')是旋转的坐标,t是相对于x轴以弧度测量的角度.旋转方向为逆时针方向.

where (x, y) are the original points, (x', y') are the rotated coordinates, and t is the angle measured in radians from the x-axis. The rotation is counter-clockwise as written.

我的建议是在纸上做一次.绘制一个矩形,计算新的坐标,然后重新绘制该矩形,以确保您在编码之前是正确的.然后将此示例用作单元测试,以确保您对其进行了正确编码.

My recommendation would be to do it out on paper once. Draw a rectangle, calculate the new coordinates, and redraw the rectangle to satisfy yourself that it's correct before you code. Then use this example as a unit test to ensure that you coded it properly.

这篇关于计算旋转矩形的顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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