Skip to content

Polygon Boolean

Justin edited this page Mar 14, 2022 · 3 revisions

CGAL provides boolean operations on polygons and polygons with holes

Note - For boolean operations it is recommended to use the EEK kernel. Precision maybe a issue if the EIK kernel is used.

This is provided through the PolygonBoolean2 class of which a static instance can be used as follows.


//Create two polygon to test with.
var poly1 = new Polygon2<EEK>(points1);
var poly2 = new Polygon2<EEK>(points2);

//Get the instance object.
var instance = PolygonBoolean2<EEK>.Instance;

//If you know the input is good then checking 
//can be disabled which can increase perform.
//instance.CheckInput = false;

//Create  list to hold the results.
//The result is always a list of PolygonWithHoles2.
var results = new List<PolygonWithHoles2<EEK>>();

//Perform what op you wish.
//Could be JOIN, INTERSECT, DIFFERENCE, SYMMETRIC_DIFFERENCE.

if(instance.Op(POLYGON_BOOLEAN.JOIN, poly1, poly2, results)
{
    //If the op was successful the results
    //list will  contain the polygons.
    foreach(var poly in results)
    {
         poly.Print();
    }
}

If desired the boolean op can be selected directly without the enum as follows.

if(instance.Join(poly1, poly2, results)
{
    //Polygons successfully joined.
}

A intersection function can also be found on the boolean class.

if(instance.DoIntersect(poly1, poly2)
{
   //Polygons intersect.
}