2
$\begingroup$

I have two very simple polygons.

a = Polygon[{{2, 0}, {2, 100}, {502, 100}, {502, 0}, {2, 0}}]
b = Polygon[{{202, -200}, {202, 296}, {300, 296}, {300, -200}, {202, -200}}]

enter image description here

I wish to produce a new polygon which (for example) describes their union as pictured above, but is expressed as a single Polygon[] instance. In this simple case, I can create it by-hand as:

c = Polygon[{
    {2, 0}, {2, 100}, {202, 100}, {202, 296}, {300, 296}, 
    {300, 100}, {502, 100}, {502, 0}, {300, 0}, {300, -200}, 
    {202, -200}, {202, 0}, {2, 0}}}]

Of course, I wish to obtain this polygon automatically. Unfortunately RegionUnion above returns a BooleanRegion:

enter image description here

This is despite some other region functions like RegionIntersection returning a Polygon.

enter image description here

In fact, I have difficulty predicting what families of polygons will return Polygon (or other acceptable symbols like Triangle) vs BooleanRegion when given to functions RegionIntersection, RegionUnion, RegionDifference.

How can I convert a BooleanRegion of two simple Polygons into a Polygon (or a list thereof)?

$\endgroup$

1 Answer 1

7
$\begingroup$

You could discretize the polygons, which lets the union operation return the object as a BoundaryMeshRegion. From there you can extract the polygons as long as there are no holes.

MeshPrimitives[RegionUnion @@ BoundaryDiscretizeGraphics /@ {a, b}, 2]
{Polygon[{{202., 100.}, {2., 100.}, {2., 0.}, {202., 0.}, 
 {202., -200.}, {300., -200.}, {300., 0.}, {502., 0.}, {502., 100.},
 {300., 100.}, {300., 296.}, {202., 296.}}]}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.