2
$\begingroup$

Description:

I am trying to find a RegionIntersect between a Disk and a Polygon. I have noticed, the code crashes when Disk would originate near a corner. Please see example below.

Code:

r1 = {{0, 0}, {0, 29}, {12, 29}, {12, 14.5}, {9, 14.5}, {9, 0}};
iwork = {1, 4};
idont = {1, 1};

Example 1:

(*This produces expected result*)

Graphics[{
  {FaceForm @ White, EdgeForm @ Black, Polygon @ r1},
  RegionIntersection[Disk[iwork, 4], Polygon @ r1]
  }]

Note:

RegionIntersection produces:

DiskSegment[{1, 4}, 4, {\[Pi] + ArcTan[Sqrt[15]], 3 \[Pi] - ArcTan[Sqrt[15]]}]

Output 1:

working example

Example 2:

(*This code produces unexpected result*)

Graphics[{
  {FaceForm @ White, EdgeForm @ Black, Polygon @ r1},
  RegionIntersection[Disk[idont, 4], Polygon @ r1]
  }]

Note:

RegionIntersection produces:

RegionIntersection[Disk[{1, 1}, 4], 
 Polygon[{{0, 0}, {0, 29}, {12, 29}, {12, 14.5}, {9, 14.5}, {9, 0}}]]

Output 2:

not working example

Why RegionIntersect does not evaluate when origin is near corner? How could this code be changed to retrieve similar result as with Circle.

Circle Alternative:

circle alternative

Reference:

Graphics
Polygon
Disk
Circle

$\endgroup$
5
$\begingroup$

As you say in your post, for some cases, RegionIntersection is able to return a Graphics primitive

RegionIntersection[Disk[iwork, 4], Polygon@r1]
(* DiskSegment[{1, 4}, 4, {π + ArcTan[Sqrt[15]], 
  3 π - ArcTan[Sqrt[15]]}] *)

to which you can simply apply Graphics and get a visual result. This is not the general case. If you took those regions and created MeshRegion objects first, it would not work

With[{rr = 
   RegionIntersection[DiscretizeGraphics@Disk[iwork, 4], 
    DiscretizeGraphics@Polygon@r1]},
 {rr, Graphics@rr, RegionPlot@rr}]

Mathematica graphics

But you can see that RegionPlot can take a Region and create a Graphics object from it (not a Graphics primitive, mind you). Then you combine it with the other shape with Show

Show[
 Graphics[{FaceForm@White, EdgeForm@Black, Polygon@r1}], 
 RegionPlot[RegionIntersection[Disk[idont, 4], Polygon@r1], 
  PlotStyle -> Black, BoundaryStyle -> Black]
 ]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thank you @JasonB. This makes things a little bit clearer. How does it decide if output is Graphics or Region? I couldn't find anything in the documentation about it $\endgroup$ – e.doroskevic May 23 '16 at 15:50
  • $\begingroup$ Not sure, it must have some internal methods to check. So the safest route would be to always use RegionPlot $\endgroup$ – Jason B. May 23 '16 at 15:55
  • $\begingroup$ see here: reference.wolfram.com/language/guide/…. Certain graphics primitives are regions, so you can safely assume the output of RegionIntersection (if valid) is a region. DiskSegment is new in 10.2 BTW. $\endgroup$ – george2079 May 23 '16 at 16:15

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.