如何使用ID2D1PathGeometry绘制圆

时间:2012-12-13 06:24:34

标签: c++ direct2d

ID2D1PathGeometry :: Open返回一个支持添加直线,圆弧和曲线的ID2D1GeometrySink。但有没有办法添加一个圆(椭圆),或至少以某种方式模拟一个?

1 个答案:

答案 0 :(得分:7)

使用AddArc函数,您可以简单地绘制两个具有相同x和y半径的圆弧来构建圆。这是图片和代码,希望这可以帮到你。

enter image description here

// create circle with two arcs
hr = g_pD2DFactory->CreatePathGeometry(&g_pCircleGeometry) ;
if (SUCCEEDED(hr))
{
    ID2D1GeometrySink *pSink = NULL;

    hr = g_pCircleGeometry->Open(&pSink);
    if (SUCCEEDED(hr))
    {
        pSink->SetFillMode(D2D1_FILL_MODE_WINDING);

        pSink->BeginFigure(
            D2D1::Point2F(100, 300), // Start point of the top half circle
            D2D1_FIGURE_BEGIN_FILLED
            );

        // Add the top half circle
        pSink->AddArc(
            D2D1::ArcSegment(
            D2D1::Point2F(400, 300), // end point of the top half circle, also the start point of the bottom half circle
            D2D1::SizeF(150, 150), // radius
            0.0f, // rotation angle
            D2D1_SWEEP_DIRECTION_CLOCKWISE,
            D2D1_ARC_SIZE_SMALL
            ));            

        // Add the bottom half circle
        pSink->AddArc(
            D2D1::ArcSegment(
            D2D1::Point2F(100, 300), // end point of the bottom half circle
            D2D1::SizeF(150, 150),   // radius of the bottom half circle, same as previous one.
            0.0f, // rotation angle
            D2D1_SWEEP_DIRECTION_CLOCKWISE,
            D2D1_ARC_SIZE_SMALL
            ));       

        pSink->EndFigure(D2D1_FIGURE_END_CLOSED);
    }
    hr = pSink->Close();
    SAFE_RELEASE(pSink);
}