Číst v angličtině

Sdílet prostřednictvím


Použití více transformací u objektu

Pokud chcete u objektu provést více transformací, znamená to, že zkombinujete několik transformací do jedné. To znamená, že vezme výstup z každé transformační matice a použije ho jako vstup pro další, čímž získá kumulativní účinky všech transformací matice.

Předpokládejme, že se dvě transformační matice, konkrétně matice otočení a matice překladu, společně vynásobí. Výsledkem je nová matice, která provádí funkce rotace i translace. Protože násobení matic není komutativní, rotační transformace vynásobená translační transformací se liší od translační transformace vynásobené rotační transformací.

Následující příklady kódu ukazují, jak použít rotaci následovanou překladem a následným převodem. Všimněte si, že výsledky vykreslování se liší.

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);

Kód vytvoří výstup zobrazený na následujícím obrázku.

ilustrace jednoho obdélníku, který byl nejprve přeložen a poté otočen, a jednoho obdélníku, který byl nejprve otočen a poté přeložen

Direct2D Reference

Přehled transformací Direct2D