如何使用ID2D1PathGeometry绘制圆形

4

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


我认为你可以用两个弧线构建一个圆形。 - zdd
1个回答

8
通过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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接