4
$\begingroup$

Why is the output so irregular in the following simple example?

Region[RegionDifference[
       Rectangle[{-2, -1}, {5, 1}], 
       RegionUnion[Disk[{0.5, 0}, 0.25], Disk[{3.5, 0}, 0.25]]
]]

enter image description here

I'm using MMA 12.1 on Windows.

$\endgroup$
1
  • 4
    $\begingroup$ What's displayed is a quick representation of the region. To get a high quality plot, use RegionPlot or DiscretizeRegion. $\endgroup$
    – Michael E2
    Nov 14 '21 at 14:02
6
$\begingroup$

This irregularity only appears in the visualization from the Region wrapper. Under the hood the region is still exactly intact:

reg = RegionDifference[Rectangle[{-2, -1}, {5, 1}], 
  RegionUnion[Disk[{0.5, 0}, 0.25], Disk[{3.5, 0}, 0.25]]];

Area[Rationalize[reg]]
14 - π/8

Most likely Region is using a general method like marching squares to visualize this region:

BoundaryDiscretizeRegion[reg, Method -> "MarchingSquares"]

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ Yep, more or less: Trace[ MakeBoxes[#, StandardForm] &[reg], _BoundaryDiscretizeRegion, TraceInternal -> True ] $\endgroup$
    – Michael E2
    Nov 14 '21 at 14:03
6
$\begingroup$
DiscretizeRegion[#
   , AccuracyGoal -> 3
   , PerformanceGoal -> "Speed"
   (*,Method\[Rule]"Continuation"*)
   ] &@RegionDifference[Rectangle[{-2, -1}, {5, 1}], 
  RegionUnion[Disk[{0.5, 0}, 0.25], Disk[{3.5, 0}, 0.25]]]

  • AccuracyGoal can be set to 2 to get good enough circles. A value such as 10 locks my computer up but that could be due to many reasons.
  • Method->"Continuation" (without the accuracy goal) also delivers good results for this case while maintaining good response time.

enter image description here

$\endgroup$
3
$\begingroup$

A one more way is as follows.

RegionPlot[RegionDifference[Rectangle[{-2, -1}, {5, 1}], 
RegionUnion[Disk[{0.5, 0}, 0.25], Disk[{3.5, 0}, 0.25]]], AspectRatio -> 2/7]

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.