Condividi tramite


Come trasformare una geometria

Per trasformare una geometria, è possibile applicare la trasformazione alla destinazione di rendering chiamando SetTransform o applicando la trasformazione alla geometria chiamando CreateTransformedGeometry. Anche se entrambi gli approcci trasformano una geometria, hanno alcune differenze fondamentali. CreateTransformedGeometry influisce sul riempimento, ma non influisce sulla larghezza del tratto. Inoltre, CreateTransformedGeometry trasforma la geometria da sola senza influire sulle altre forme sulla destinazione di rendering, mentre SetTransform applica la trasformazione a tutte le forme nella destinazione di rendering.

Questo argomento descrive come trasformare una geometria chiamando CreateTransformedGeometry.

Per trasformare una geometria

  1. Dichiarare una variabile ID2D1TransformedGeometry .
  2. Chiamare il metodo CreateTransformedGeometry per creare una geometria trasformata.

Il codice seguente illustra come creare un bicchiere di ora, trasformare il vetro dell'ora e disegnare gli occhiali dell'ora originali e risultanti.

// Create a path geometry.
if (SUCCEEDED(hr))
{
    hr = m_pD2DFactory->CreatePathGeometry(&m_pPathGeometry);

    if (SUCCEEDED(hr))
    {
        // Write to the path geometry using the geometry sink.
        hr = m_pPathGeometry->Open(&pSink);

        if (SUCCEEDED(hr))
        {
            pSink->BeginFigure(
                D2D1::Point2F(0, 0),
                D2D1_FIGURE_BEGIN_FILLED
                );

            pSink->AddLine(D2D1::Point2F(200, 0));

            pSink->AddBezier(
                D2D1::BezierSegment(
                    D2D1::Point2F(150, 50),
                    D2D1::Point2F(150, 150),
                    D2D1::Point2F(200, 200))
                );

            pSink->AddLine(D2D1::Point2F(0, 200));

            pSink->AddBezier(
                D2D1::BezierSegment(
                    D2D1::Point2F(50, 150),
                    D2D1::Point2F(50, 50),
                    D2D1::Point2F(0, 0))
                );

            pSink->EndFigure(D2D1_FIGURE_END_CLOSED);

            hr = pSink->Close();
        }
        SafeRelease(&pSink);
    }
}

if (SUCCEEDED(hr))
{
    // Create a transformed geometry which is tilted at an angle to the previous geometry
    hr = m_pD2DFactory->CreateTransformedGeometry(
        m_pPathGeometry,
        D2D1::Matrix3x2F::Rotation(
            45.f,
            D2D1::Point2F(100, 100)),
        &m_pTransformedGeometry
        );
}