5
$\begingroup$

Let for example:

regn = RegionDifference[Rectangle[{-2, -2}, {2, 2}], Disk[{0, 0}, 1]];

The only documented way I found to draw a gereric region regn filled is a bit "complex":

RegionPlot[RegionMember[regn, {x, y}], {x, -5, 5}, {y, -5, 5}]

Mathematica graphics

There is a direct way to make a Graphics object with some filled generic region regn and other graphics primitives like Line, Disk, Rectangle without resorting to use RegionPlot and then Show to combine different Graphic-s?

I'm interested to a generic way for a generic region, not only for the region in the example.

$\endgroup$
2
  • $\begingroup$ What do you mean with "not exactly readable"? $\endgroup$ – rhermans Nov 21 '14 at 14:30
  • 2
    $\begingroup$ Graphics[{Lighter[Blue], Rectangle[{-2, -2}, {2, 2}], White, Disk[{0, 0}, 1]}, PlotRange -> {{-5, 5}, {-5, 5}}, Frame -> True] $\endgroup$ – Bob Hanlon Nov 21 '14 at 14:31
6
$\begingroup$

Building on your self-answer here is an example of using the primitives generated by RegionPlot inside another Graphics expression:

regn = RegionDifference[Rectangle[{-2, -2}, {2, 2}], Disk[{0, 0}, 1]];

elem = RegionPlot[regn][[1]];

Graphics[{Polygon[{{-2, -2}, {3, 3}, {-2, 3}, {3, -2}}], 
  elem, {Orange, Disk[{0, 0}, 1.5, {Pi/4, 3 Pi/4}]}}]

enter image description here

If a different style is needed for the region its hard-coded Directives can be stripped:

elem2 = DeleteCases[elem, _Directive, -1];

Graphics[{Polygon[{{-2, -2}, {3, 3}, {-2, 3}, {3, -2}}],
  {Brown, elem2}, {Orange, Disk[{0, 0}, 1.5, {Pi/4, 3 Pi/4}]}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Simple and effective, as you are used to be, thanks! $\endgroup$ – unlikely Dec 22 '14 at 17:24
2
$\begingroup$

It appear that plotting a Region can be simply done by passing the region to RegionPlot without any range specification. At the time the question was asked this use of RegionPlot wasn't documented.

regn = RegionDifference[Rectangle[{-2, -2}, {2, 2}], Disk[{0, 0}, 1]];
RegionPlot[regn]

My question was originally also about a way to combine a filled region with Others graphics primitives without using Show. I think Show it's an effective way to do this, if we want to keep the region's generality, but have a look at the Mr.Wizard answer.

$\endgroup$
1
  • $\begingroup$ Thanks for the self-answer; I wasn't aware of this. $\endgroup$ – Mr.Wizard Dec 22 '14 at 16:43
2
$\begingroup$

You can sort of achieve it with DiscretizeRegion and BoundaryMesh

regn = BoundaryMesh@DiscretizeRegion@
   RegionDifference[Rectangle[{-2, -2}, {2, 2}], Disk[{0, 0}, 1]]

Show[regn, Graphics@Line[{{0, 0}, {3, 1}}]]

Region with line

MeshRegions (what DiscretizeRegion generates) have many styling options.

$\endgroup$
1
$\begingroup$

Perhaps the MeshPrimitives function allows us to move toward a more canonical way of adding regions to Graphics(3D) without resorting to RegionPlot + extraction + style deletion. Here is Mr. Wizard's second example converted to this form and another example in 3D, which is what I was tinkering with that led me to this question.

Graphics[{Polygon[{{-2, -2}, {3, 3}, {-2, 3}, {3, -2}}], {Brown, 
   EdgeForm@Brown, 
   MeshPrimitives[DiscretizeRegion@regn, 1 | 2]}, {Orange, 
   Disk[{0, 0}, 1.5, {Pi/4, 3 Pi/4}]}}]

enter image description here

Graphics3D[{EdgeForm[], Gray, Opacity@.2, Sphere[], Brown, Opacity@1, 
  Cuboid[{-1, -1, -2}, {1, 1, -1}], Blue, 
  MeshPrimitives[
   BoundaryDiscretizeRegion@
    RegionDifference[Ball[], Cuboid[{-1, -1, -1/3}, {1, 1, 1}]], 2]}, 
 Boxed -> False]

enter image description here

$\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.