Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This topic describes how to combine two geometries. Direct2D supports four modes for combining geometries: Union, Intersect, XOR, and Exclude. These modes are specified in the D2D1_COMBINE_MODE enum type.
To combine two geometries by using any of the four modes
- Declare a path geometry: a variable of type ID2D1PathGeometry that will store the result of geometry combination.
- Declare a geometry sink: a variable of type ID2D1GeometrySink that will store the path geometry.
- Create the path geometry object by calling the ID2D1Factory::CreatePathGeometry method.
- Open the geometry sink object by calling the ID2D1PathGeometry::Open method.
- Use one of the four modes to combine the two geometries by calling the ID2D1EllipseGeometry::CombineWithGeometry method.
- Close the geometry sink object.
The following code declares the variables of type ID2D1PathGeometry and ID2D1GeometrySink.
ID2D1PathGeometry *m_pPathGeometryUnion;
ID2D1PathGeometry *m_pPathGeometryIntersect;
ID2D1PathGeometry *m_pPathGeometryXOR;
ID2D1PathGeometry *m_pPathGeometryExclude;
The following code uses each of the four modes to combine the two ID2D1EllipseGeometry objects and performs the following actions:
- Creates two ellipses, m_spEllipseGeometryOne and m_spEllipseGeometryTwo.
- Creates a path geometry object.
- Opens a geometry sink object.
- Combines the two ellipses.
- Closes the geometry sink object.
HRESULT DemoApp::CreateGeometryResources()
{
HRESULT hr = S_OK;
ID2D1GeometrySink *pGeometrySink = NULL;
// Create the first ellipse geometry to merge.
const D2D1_ELLIPSE circle1 = D2D1::Ellipse(
D2D1::Point2F(75.0f, 75.0f),
50.0f,
50.0f
);
hr = m_pD2DFactory->CreateEllipseGeometry(
circle1,
&m_pCircleGeometry1
);
if (SUCCEEDED(hr))
{
// Create the second ellipse geometry to merge.
const D2D1_ELLIPSE circle2 = D2D1::Ellipse(
D2D1::Point2F(125.0f, 75.0f),
50.0f,
50.0f
);
hr = m_pD2DFactory->CreateEllipseGeometry(circle2, &m_pCircleGeometry2);
}
if (SUCCEEDED(hr))
{
//
// Use D2D1_COMBINE_MODE_UNION to combine the geometries.
//
hr = m_pD2DFactory->CreatePathGeometry(&m_pPathGeometryUnion);
if (SUCCEEDED(hr))
{
hr = m_pPathGeometryUnion->Open(&pGeometrySink);
if (SUCCEEDED(hr))
{
hr = m_pCircleGeometry1->CombineWithGeometry(
m_pCircleGeometry2,
D2D1_COMBINE_MODE_UNION,
NULL,
NULL,
pGeometrySink
);
}
if (SUCCEEDED(hr))
{
hr = pGeometrySink->Close();
}
SafeRelease(&pGeometrySink);
}
}
if (SUCCEEDED(hr))
{
//
// Use D2D1_COMBINE_MODE_INTERSECT to combine the geometries.
//
hr = m_pD2DFactory->CreatePathGeometry(&m_pPathGeometryIntersect);
if (SUCCEEDED(hr))
{
hr = m_pPathGeometryIntersect->Open(&pGeometrySink);
if (SUCCEEDED(hr))
{
hr = m_pCircleGeometry1->CombineWithGeometry(
m_pCircleGeometry2,
D2D1_COMBINE_MODE_INTERSECT,
NULL,
NULL,
pGeometrySink
);
}
if (SUCCEEDED(hr))
{
hr = pGeometrySink->Close();
}
SafeRelease(&pGeometrySink);
}
}
if (SUCCEEDED(hr))
{
//
// Use D2D1_COMBINE_MODE_XOR to combine the geometries.
//
hr = m_pD2DFactory->CreatePathGeometry(&m_pPathGeometryXOR);
if (SUCCEEDED(hr))
{
hr = m_pPathGeometryXOR->Open(&pGeometrySink);
if (SUCCEEDED(hr))
{
hr = m_pCircleGeometry1->CombineWithGeometry(
m_pCircleGeometry2,
D2D1_COMBINE_MODE_XOR,
NULL,
NULL,
pGeometrySink
);
}
if (SUCCEEDED(hr))
{
hr = pGeometrySink->Close();
}
SafeRelease(&pGeometrySink);
}
}
if (SUCCEEDED(hr))
{
//
// Use D2D1_COMBINE_MODE_EXCLUDE to combine the geometries.
//
hr = m_pD2DFactory->CreatePathGeometry(&m_pPathGeometryExclude);
if (SUCCEEDED(hr))
{
hr = m_pPathGeometryExclude->Open(&pGeometrySink);
if (SUCCEEDED(hr))
{
hr = m_pCircleGeometry1->CombineWithGeometry(
m_pCircleGeometry2,
D2D1_COMBINE_MODE_EXCLUDE,
NULL,
NULL,
pGeometrySink
);
}
if (SUCCEEDED(hr))
{
hr = pGeometrySink->Close();
}
SafeRelease(&pGeometrySink);
}
}
return hr;
}
This code produces the output shown in the following illustration.
Related topics
-
ID2D1GeometrySink