14
$\begingroup$

I need to clip several regions reg: like Rectangle, Polygon, Ellipse... My goal isn't to plot the clipping but to have a representation of each new region.

I searched if there was any package or function to perform polygon clipping at least, but I couldn't find any lead.

My main question is:

  • Is there an already implemented algorithm in mathematica to perform polygon or region clipping ?

else:

  • Should I better link an external polygon clipping library (such as Clipper, Boost.Geometry or CGAL) with mathematica to do so ?

Edit:

To illustrate what I aim for:

Overlapping Rectangle

I have 5 rectangles that are overlapping, I would like to have:

Non-overlapping Regions

I have now 8 regions that aren't intersecting/overlapping with others.

$\endgroup$
4
  • 2
    $\begingroup$ Are you looking for RegionIntersection? $\endgroup$
    – Szabolcs
    Sep 16, 2016 at 9:34
  • 1
    $\begingroup$ I have edit my question to illustrate my problem. @Szabolcs It must in fact use RegionIntersection, but also RegionDifference... I tried a custom solution to do such result, but it's not working that well on any kind of polygons or regions disposition. $\endgroup$
    – A.M.
    Sep 16, 2016 at 11:16
  • $\begingroup$ have you looked at BooleanRegion ? $\endgroup$
    – george2079
    Sep 16, 2016 at 11:38
  • $\begingroup$ Related: "How do I flatten transparency on a graphics, for conversion to eps or similar?" $\endgroup$ Sep 17, 2016 at 8:35

1 Answer 1

7
$\begingroup$

Using boolean operations to test every possible combination of area intersections:

r1 = Disk[{0, 0}, 1];
r2 = Disk[{1/8, 0}, {1/2, 3/2}];
r3 = Rectangle[{1/4, 0}, {2, 1}];
regions={r1,r2,r3};
Graphics[{Opacity[.5], {{Red, r1}, {Green, r2}, {Blue, r3}}}]

enter image description here

range = {{-2,2},{-2,2}};
(*warning RegionPlot throws errors
    if nothing is in the plot range*)
sub[set_, r_] := 
 Module[{
   in = Flatten@Position[set, 1], 
   out = Flatten@Position[set, 0]}, 
  If[Union@set === {1}, RegionIntersection @@ r, 
   RegionDifference[
      BooleanRegion[And, r[[in]]], 
      BooleanRegion[Or, r[[out]]]]]]
c = Tuples[{0, 1}, Length@regions][[2 ;;]]; (*dont include first tuple {0,0,..}*)
subregions = Select[sub[#, regions] & /@ c, Area@# > 0 &];
s = MapThread[RegionPlot[#1, PlotStyle -> #2, PlotRange -> range] &,
   {subregions, 
    Table[Hue[RandomReal[{0, 2/3}]], {Length@subregions}]}];
Grid[{s, c}]
Show[s, PlotRange -> range]

enter image description here

enter image description here

notice there are disconnected sub regions that happen when a single region is split by the others. Kind of stumped on how to split those automatically into separate regions.

edit per comment from @JasonB we can get all the discrete regions, but it requires generating mesh regions:

  ConnectedMeshComponents@DiscretizeRegion[#] & /@ subregions

enter image description here

edit: the example

regions = {Rectangle[{5, -15}, {35, 15}], 
   Rectangle[{25, -20}, {45, 20}], Rectangle[{20, -5}, {35, 5}], 
   Rectangle[{30, 10}, {45, 20}], Rectangle[{35, -20}, {45, -5}]};
range = {{0, 50}, {-30, 30}}

enter image description here

$\endgroup$
7
  • $\begingroup$ Thanks for your reply @george2079 However, c variable definition is missing, what is the set_ arguments for ? $\endgroup$
    – A.M.
    Sep 16, 2016 at 14:30
  • $\begingroup$ sort for the excessive editing... now its working right..! excepting for the pesky issue of how to split the disconnected regions. $\endgroup$
    – george2079
    Sep 16, 2016 at 15:33
  • $\begingroup$ @george2079 - you can use ConnectedMeshComponents $\endgroup$
    – Jason B.
    Sep 16, 2016 at 15:53
  • $\begingroup$ @JasonB, thanks - added that. $\endgroup$
    – george2079
    Sep 16, 2016 at 16:03
  • $\begingroup$ @george2079 I've tried your code on my example regions = {Rectangle[{5, -15}, {35, 15}], Rectangle[{25, -20}, {45, 20}], Rectangle[{20, -5}, {35, 5}], Rectangle[{30, 10}, {45, 20}], Rectangle[{35, -20}, {45, -5}]} and I have issues with RegionPlot::nnregion and RegionPlot::nnregion $\endgroup$
    – A.M.
    Sep 16, 2016 at 17:53

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.