如何將多個轉換套用至物件
若要在物件上執行多個轉換,表示將數個轉換合併成一個轉換。 也就是說,從每個轉換矩陣取得輸出,並將其作為下一個轉換的輸入,藉此取得所有矩陣轉換的累積效果。
假設兩個轉換矩陣,即旋轉矩陣和平移矩陣,彼此相乘。 結果是執行旋轉和轉譯函式的新矩陣。 因為矩陣乘法不具有交換性,旋轉變換乘以平移變換與平移變換乘以旋轉變換不同。
下列程式代碼範例示範如何套用旋轉,後面接著翻譯,然後轉譯接著旋轉。 請注意,轉譯結果不同。
D2D1_RECT_F rectangle = D2D1::RectF(300.0f, 40.0f, 360.0f, 100.0f);
// Draw the rectangle before transforming the render target.
m_pRenderTarget->DrawRectangle(
rectangle,
m_pOriginalShapeBrush,
1.0f,
m_pStrokeStyleDash
);
D2D1_MATRIX_3X2_F rotation = D2D1::Matrix3x2F::Rotation(
45.0f,
D2D1::Point2F(330.0f, 70.0f)
);
D2D1_MATRIX_3X2_F translation = D2D1::Matrix3x2F::Translation(20.0f, 10.0f);
// First rotate about the center of the square and then move
// 20 pixels to the right along the x-axis
// and 10 pixels downward along the y-axis.
m_pRenderTarget->SetTransform(rotation* translation);
// Draw the rectangle in the transformed space.
m_pRenderTarget->FillRectangle(rectangle, m_pFillBrush);
m_pRenderTarget->DrawRectangle(rectangle, m_pTransformedShapeBrush, 1.0f);
D2D1_RECT_F rectangle = D2D1::Rect(40.0f, 40.0f, 100.0f, 100.0f);
// Draw a rectangle without transforming it.
m_pRenderTarget->DrawRectangle(
rectangle,
m_pOriginalShapeBrush,
1.0f,
m_pStrokeStyleDash
);
D2D1_MATRIX_3X2_F translation = D2D1::Matrix3x2F::Translation(20.0f, 10.0f);
m_pRenderTarget->SetTransform(translation);
D2D1_MATRIX_3X2_F rotation = D2D1::Matrix3x2F::Rotation(
45.0f,
D2D1::Point2F(70.0f, 70.0f)
);
// First move 20 pixels to the right along the x-axis and
// 10 pixels downward along the y-axis,
// and then rotate about the center of the original square.
m_pRenderTarget->SetTransform(translation * rotation);
// Draw the rectangle in the transformed space.
m_pRenderTarget->FillRectangle(rectangle, m_pFillBrush);
m_pRenderTarget->DrawRectangle(rectangle, m_pTransformedShapeBrush);
此程式代碼會產生下圖所示的輸出。
相關主題