10

I am using RegionIntersection[] to intersect two rectangles, phrased as Polygon[]s:


BugRegInt
RegionIntersection[] returns an incorrect result (the green region above; detail below), and issues a series of error messages:
Errors
Here is the region returned as the intersection, which is not correct (it should be a quadrilateral):
Polygon[{{0.407273, 0.650444}, {0.509656, -0.0200315}, {0.998507, 
   0.0546171}, {0.640904, 0.767621}, {0.998507, 0.0546171}}]

Here it is as a line drawing:


PentQuad

Here is the originating call that produces the incorrect intersection:

RegionIntersection[
 Polygon[{{0.5096555454081809`, -0.02003146973392257`}, \
{0.9985073695269602`, 0.05461714932464575`}, {0.6966031018052412`, 
    2.0316992956026936`}, {0.2077512776864619`, 
    1.9570506765441251`}}] , 
 Polygon[{{0.9985073695269602`, 
    0.05461714932464575`}, {0.6409040075686694`, 
    0.767621034809768`}, {-1.1468443539373534`, \
-0.12901478643123643`}, {-0.7892409919790626`, -0.8420186719163586`}}]
 ]

Can anyone see what's going on?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Joseph O'Rourke
  • 4,731
  • 25
  • 42

2 Answers2

13

This is a bug that has been fixed in the development version. For a possible workaround, use exact coordinates, for example

sp = Function[p, SetPrecision[p, Infinity]];

ri = RegionIntersection[
 sp@Polygon[{{0.5096555454081809`, -0.02003146973392257`}, 
             {0.9985073695269602`, 0.05461714932464575`}, 
             {0.6966031018052412`, 2.0316992956026936`},   
             {0.2077512776864619`, 1.9570506765441251`}}], 
 sp@Polygon[{{0.9985073695269602`, 0.05461714932464575`}, 
             {0.6409040075686694`, 0.767621034809768`},    
             {-1.1468443539373534`, -0.12901478643123643`}, 
             {-0.7892409919790626`, -0.8420186719163586`}}]];

N[ri]

(* Polygon[{{0.40727258068338046`, 0.6504444171024929`}, 
            {0.5096555454081809`, -0.02003146973392257`}, 
            {0.9985073695269602`, 0.05461714932464575`}, 
            {0.6409040075686694`, 0.767621034809768`}}] *)
ilian
  • 25,474
  • 4
  • 117
  • 186
5

Another workaround is to turn the polygons into MeshRegions first,

RegionIntersection @@ (DiscretizeGraphics /@ {p1, p2}) // 
  MeshPrimitives[#, 2] & // First
(* Polygon[{{0.407273, 
   0.650444}, {0.509656, -0.0200315}, {0.998507, 
   0.0546171}, {0.640904, 0.767621}}] *)

Where p1 and p2 are your polygons

Edit - more odd behavior

What's even odder is that if you change the order of the points of the polygon (cyclicly so that you don't change the shape), then it will work. If points1 and points2 are the points of the polygon in the OP, then

RegionIntersection @@ (Polygon /@ {points1, points2})

fails as it did for OP, but

RegionIntersection @@ (Polygon /@ {RotateLeft@points1, 
RotateLeft@points2}) 

works perfectly.

Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • Thanks, Jason! Can you explain why this circumvents the problem? – Joseph O'Rourke Jun 22 '16 at 19:56
  • 1
    @JosephO'Rourke - I wish I could! I just have had some experience with regions lately, and one of the basic steps when trying anything is to try it on a MeshRegion versus a regular region. For it to work on a Polygon or other built in shape (like Circle or Disk), I think the R&D guys have to code the cases in individually. But when using a MeshRegion there is a general toolkit that is used. @ilian would know more about that than I do though.... – Jason B. Jun 22 '16 at 20:02
  • @JosephO'Rourke - so if you are working in 2D with a non-infinite region, then turning it into a MeshRegion with DiscretizeGraphics is often a great way to go. But if you are working in 3D, then it won't work. – Jason B. Jun 22 '16 at 20:03