如何將多個轉換套用至物件
若要在物件上執行多個轉換,表示將數個轉換結合成一個轉換。 也就是說,取得每個轉換矩陣的輸出,並將其當做下一個轉換的輸入,藉此取得所有矩陣轉換的累計效果。
假設兩個轉換矩陣旋轉和轉譯會乘以一起。 結果是新的矩陣,可執行旋轉和轉譯的函式。 因為矩陣乘法不是通通的,所以旋轉轉換乘以轉譯轉換不同于轉譯轉換乘以旋轉轉換。
下列程式碼範例示範如何套用旋轉,後面接著翻譯,然後再套用旋轉。 請注意,轉譯結果不同。
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);
此程式碼會產生下圖所示的輸出。
相關主題