如何旋转对象

本主题介绍如何围绕指定点旋转对象。 若要旋转对象,请调用 Matrix3x2F::Rotation 方法。 此方法采用两个参数,即指定的角度和中心点。 角度是以度为单位的顺时针旋转角度,中心点是对象旋转的点。 中心点在转换对象的坐标系中表示。

例如,以下代码按正时针旋转 45 度左右的正方形中心。

    // Create a rectangle.
    D2D1_RECT_F rectangle = D2D1::Rect(438.0f, 301.5f, 498.0f, 361.5f);

    // Draw the rectangle.
    m_pRenderTarget->DrawRectangle(
        rectangle,
        m_pOriginalShapeBrush,
        1.0f,
        m_pStrokeStyleDash
        );

    // Apply the rotation transform to the render target.
    m_pRenderTarget->SetTransform(
        D2D1::Matrix3x2F::Rotation(
            45.0f,
            D2D1::Point2F(468.0f, 331.5f))
        );

    // Fill the rectangle.
    m_pRenderTarget->FillRectangle(rectangle, m_pFillBrush);

    // Draw the transformed rectangle.
    m_pRenderTarget->DrawRectangle(rectangle, m_pTransformedShapeBrush);

下图显示了将前面的旋转转换应用到正方形的效果。 原始正方形是一个虚线轮廓,旋转的正方形是实心轮廓。

illustration of a square rotated clockwise 45 degrees about the center of the original square

下图显示了围绕不同中心点的相同角度旋转的效果。 请注意,旋转的对象相对于原始对象处于不同的位置。 左轮廓正方形是围绕原始正方形中心旋转的结果,右边框是围绕原始平方的左上角旋转的结果。

illustration of a square rotated clockwise 45 degrees about a different center point

Direct2D 参考

Direct2D 转换概述