共用方式為


如何旋轉物件

本主題描述如何圍繞指定的點旋轉物件。 若要旋轉物件,請呼叫 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);

下圖顯示將上述旋轉轉換套用至正方形的效果。 原始正方形是虛線外框,旋轉的正方形是實線外框。

圍繞原方形中心順時針旋轉45度的正方形圖例

下圖顯示在不同中心點上以相同角度旋轉的效果。 請注意,旋轉的物件與原始對象位於不同的位置。 左外框方塊是旋轉原始正方形中心的結果,而右外框方塊則是在原始正方形左上角旋轉的結果。

以不同中心點順時針旋轉45度的正方形圖例

Direct2D 參考

Direct2D 轉換概觀