简体   繁体   English

计算旋转矩形的顶点

[英]Calculating vertices of a rotated rectangle

I am trying to calculate the vertices of a rotated rectangle (2D).我正在尝试计算旋转矩形(2D)的顶点。

It's easy enough if the rectangle has not been rotated, I figured that part out.如果矩形没有旋转,这很容易,我想出了那部分。

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. Use trig to somehow figure out where the endpoints of the rectangle are relative to the position of the rectangle in world space.使用 trig 以某种方式找出矩形的端点相对于矩形在世界空间中的位置的位置。 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);

}

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:如果是 2D 平面旋转,则如下所示:

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

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.其中 (x, y) 是原始点,(x', y') 是旋转坐标,t 是从 x 轴以弧度测量的角度。 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.然后将此示例用作单元测试以确保您正确编码。

I think you were on the right track using atan() to return an angle.我认为您使用atan()返回一个角度是在正确的轨道上。 However you want to pass height divided by width instead of the other way around.但是,您希望通过height除以width而不是相反。 That will give you the default (unrotated) angle to the upper-right vertex of the rectangle.这将为您提供与矩形右上角的默认(未旋转)角度。 You should be able to do the rest like this:您应该能够像这样完成其余的工作:

// Get the original/default vertex angles
GLfloat vertex1_theta = RAD_TO_DEG( atan(
            (m_height/2 * m_scaleY)
            / (m_width/2 * m_scaleX) ) );
GLfloat vertex2_theta = -vertex1_theta; // lower right vertex
GLfloat vertex3_theta = vertex1_theta - 180; // lower left vertex
GLfloat vertex4_theta = 180 - vertex1_theta; // upper left vertex

// Now get the rotated vertex angles
vertex1_theta += rotation_angle;
vertex2_theta += rotation_angle;
vertex3_theta += rotation_angle;
vertex4_theta += rotation_angle;

//Calculate the distance from the center (same for each vertex)
GLfloat r = sqrt(pow(m_width/2*m_scaleX, 2) + pow(m_height/2*m_scaleY, 2));

/* Calculate each vertex (I'm not familiar with OpenGL, DEG_TO_RAD
 * might be a constant instead of a macro)
 */
vertexN_x = m_position.x + cos(DEG_TO_RAD(vertexN_theta)) * r;
vertexN_y = m_position.y + sin(DEG_TO_RAD(vertexN_theta)) * r;

// Now you would draw the rectangle, proceeding from vertex1 to vertex4.

Obviously more longwinded than necessary, for the sake of clarity.为了清楚起见,显然比必要的冗长。 Of course, duffymo's solution using a transformation matrix is probably more elegant and efficient :)当然,duffymo 使用变换矩阵的解决方案可能更优雅和高效:)

EDIT : Now my code should actually work.编辑:现在我的代码应该可以正常工作了。 I changed (width / height) to (height / width) and used a constant radius from the center of the rectangle to calculate the vertices.我将(width / height)更改为(height / width)并使用从矩形中心开始的恒定半径来计算顶点。 Working Python (turtle) code at http://pastebin.com/f1c76308chttp://pastebin.com/f1c76308c 上工作的 Python(海龟)代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM