4

ID2D1PathGeometry::Open returns an ID2D1GeometrySink that supports adding lines, arcs, and curves. But is there a way to add a circle (ellipse), or at least simulate one somehow?

1 Answers1

8

With function AddArc, you can simply draw two arcs with the same x and y radius to build up a circle. here is the picture and the code, hopes this can help you.

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);
}
zdd
  • 8,258
  • 8
  • 46
  • 75