2
$\begingroup$

I am trying to get a vertex list for a polygonal region made from the RegionDifference of two Polygons, and I am unsure of how to do so. For example:

region1 = Polygon[{{310, 577.315}, {310, 708}, {228.998, 708},
                   {160, 588.451}, {160, 404}, {210.065, 404}}];
region2 = Polygon[{{101.341, 364.}, {330., 187.811}, {558.659, 364.}, {330., 760.283}}];
regionDiff = RegionDifference[region1, region2];

So, I'd like to find the vertices of regionDiff.

Other options for getting a vertex list for the difference of two polygons are also welcome.

$\endgroup$
2
  • 2
    $\begingroup$ What version of Mathematica are you using? For version 11.2, regionDiff returns Polygon[{{160., 465.66}, {299.832, 708.}, {228.998, 708.}, {160., 588.451}}] directly. $\endgroup$
    – Jason B.
    Oct 30 '17 at 16:47
  • $\begingroup$ I am using version 10.4.1.0. Version 11.2 sounds better! $\endgroup$
    – Frobryo
    Oct 30 '17 at 16:49
1
$\begingroup$

You obtain the actual region with R = BoundaryDiscretizeRegion[regionDiff, MaxCellMeasure -> 100000] or R = BoundaryDiscretizeRegion[regionDiff, Method -> "Boolean"] (somehow depending on the version of Mathematica).

Afterwards, you can access properties of the MeshRegion R like with any other MeshRegions. For example, you obtain the coordinates with MeshCoordinates[R].

$\endgroup$
10
  • $\begingroup$ I just tried this with the example Polygons and it gave me a list of 1752 points. The difference region is a quadrilateral. $\endgroup$
    – Frobryo
    Oct 30 '17 at 16:53
  • $\begingroup$ @Frobryo - try MeshPrimitives[BoundaryDiscretizeRegion@regionDiff, 2] $\endgroup$
    – Jason B.
    Oct 30 '17 at 17:07
  • $\begingroup$ That works! To an extent. It's still giving more points than necessary (47 in the example), but they're all on the edge of the polygon and include the vertices, at least. $\endgroup$
    – Frobryo
    Oct 30 '17 at 17:11
  • $\begingroup$ You can prevent subdivision by cranking up the value of MaxCellMeasure. Like this: MeshPrimitives[ BoundaryDiscretizeRegion[regionDiff, MaxCellMeasure -> 100000], 2] $\endgroup$ Oct 30 '17 at 17:17
  • $\begingroup$ @Frobryo - this is odd. When I use your example definition of regionDiff and the BoundaryDiscretizeRegion, the result is a Polygon with four points, in version 10.3.1 $\endgroup$
    – Jason B.
    Oct 30 '17 at 17:17
1
$\begingroup$

In old versions (e.g. 10.4.1), you can use the undocumented function Graphics`PolygonUtils`PolygonComplement[]:

region1 = Polygon[{{310, 577.315}, {310, 708}, {228.998, 708},
                   {160, 588.451}, {160, 404}, {210.065, 404}}];
region2 = Polygon[{{101.341, 364.}, {330., 187.811}, {558.659, 364.}, {330., 760.283}}];
regionDiff = Graphics`PolygonUtils`PolygonComplement[region1, region2];

Graphics[{{Blue, region2}, {Red, region1}, {Green, regionDiff}}]

polygons and their difference

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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