如何旋轉物件

本主題描述如何旋轉物件的相關指定點。 若要旋轉物件,請呼叫 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 轉換概觀