2

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)?

Anti Earth
  • 1,211
  • 6
  • 13

1 Answers1

7

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.}}]}
Greg Hurst
  • 35,921
  • 1
  • 90
  • 136