Group Abstract Group Abstract

Message Boards Message Boards

0
|
60 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Unexpected results during 2D regions basic Boolean manipulation

I tried to cut a Г-shaped hole inside a rectangle but got different results depending on whether a primitive was wrapped as a Region or passed directly. According to the documentation, both approaches—applying RegionDifference or RegionUnion to regions or graphics primitives—should behave the same.

Example

Path 1: Using Graphics Primitives Directly

outer = RegionDifference[
  Rectangle[{-1, -1}, {1, 1}],
  RegionUnion[
    Rectangle[{-0.9, -0.9}, {0.9, -0.4}],
    Rectangle[{0.4, -0.9}, {0.9, 0.4}]
  ]
];

outer

This returns:

Polygon[{{-1., 1.}, {1., 1.}, {1., -1.}, {-1., -1.}} -> {{{0.4, -0.9}, {0.9, -0.9}, {0.9, -0.4}, {0.9, 0.4}, {0.4, 0.4}, {0.4, -0.4}, {-0.9, -0.4}, {-0.9, -0.9}}}]

When plotted with Graphics:

Graphics[outer]

enter image description here

However, converting this to a Region and using RegionPlot:

RegionPlot[outer // Region]

Produces just a line (could it be, that Region ignores origin position?):

enter image description here

Path 2: Using Region on All Primitives

outer = RegionDifference[
  Rectangle[{-1, -1}, {1, 1}] // Region,
  RegionUnion[
    Rectangle[{-0.9, -0.9}, {0.9, -0.4}] // Region,
    Rectangle[{0.4, -0.9}, {0.9, 0.4}] // Region
  ]
];

RegionPlot[outer]

This gives the same thin line:

enter image description here

Thanks.

UPD: I checked my region with a test point, it returns incorrect results

RegionMember[outer, {0, 0}]

False
POSTED BY: Kirill Vasin
3 Replies

This is bug considering this answer on stackexchange.

outer = RegionDifference[
  Rectangle[{-1, -1}, {1, 1}],
  RegionUnion[
    Rectangle[{-0.9, -0.9}, {0.9, -0.4}],
    Rectangle[{0.4, -0.9}, {0.9, 0.4}]
  ]
];
RegionPlot[Rationalize[outer, 0]]

If it is rationalized it start to work properly.

POSTED BY: Kirill Vasin

If it is rationalized it start to work properly.

Interesting! And DiscretizeRegion[] seems to be annother way to make it work, e.g.:

outer = RegionDifference[Rectangle[{-1, -1}, {1, 1}], 
  DiscretizeRegion@
   RegionUnion[Rectangle[{-0.9, -0.9}, {0.9, -0.4}], 
    Rectangle[{0.4, -0.9}, {0.9, 0.4}]]]
POSTED BY: Henrik Schachner

Indeed. This also works:

outer = RegionDifference[DiscretizeRegion@Region@Rectangle[{-1, -1}, {1, 1}], 

   RegionUnion[DiscretizeRegion@Region@Rectangle[{-0.9, -0.9}, {0.9, -0.4}], 
    DiscretizeRegion@Region@Rectangle[{0.4, -0.9}, {0.9, 0.4}]]]
POSTED BY: Kirill Vasin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard