7
$\begingroup$

I wish to use RegionDifference to take a cube shape out of a cylinder. First I make the cylinder and cube and combine them in RegionUnion.

reg1 = Cylinder[{{0, 0, 0}, {10, 0, 0}}, 0.5];
reg2 = Cuboid[{5, 0, 0}, {10, 1, 1}];
Region[RegionUnion[reg1, reg2], Axes -> True]

Mathematica graphics

So this looks good so far. Now I wish to take the cuboid out of the cylinder leaving a notch in the cylinder. I try

reg = RegionDifference[reg1, reg2];
Region[reg, Axes -> True, PlotRange -> All]

Mathematica graphics

My cylinder is chopped off short and given a bad end (away from the subtraction). Is there a workaround?

Version 11.3 for windows.

$\endgroup$
2
  • 1
    $\begingroup$ Wow, that's really weird. Please contact support. Honestly, I am quite disappointed with the almost nonexistent usability of the BooleanRegion facilities. $\endgroup$ Mar 14, 2019 at 18:37
  • 2
    $\begingroup$ I have sent it off to support.I agree about being fed up. Second time in two days you have had to help me out -for which I am very grateful. $\endgroup$
    – Hugh
    Mar 14, 2019 at 19:24

2 Answers 2

8
$\begingroup$

Please note the RegionBounds:

reg1 = Cylinder[{{0, 0, 0}, {10, 0, 0}}, 0.5];
reg2 = Cuboid[{5, 0, 0}, {10, 1, 1}];
reg = RegionDifference[reg1, reg2];

bounds = RegionBounds@reg;
Region[reg, Axes -> True, PlotRange -> bounds]

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ Whoa. Why did PlotRange -> All not work? Anyways, good job! $\endgroup$ Mar 14, 2019 at 21:43
  • $\begingroup$ I put in PlotRange All because I wondered if it was a plotting problem. Are there known issues with PlotRange? $\endgroup$
    – Hugh
    Mar 14, 2019 at 22:31
  • $\begingroup$ @Hugh How Mathematica works is probably only known to the developers. For the user remains only trial and error. But I have already encountered this problem earlier. You have sent it off to support, that's ok. $\endgroup$
    – rmw
    Mar 15, 2019 at 9:05
6
$\begingroup$

This seems to be a viable workaround although it is a shame that we have to discretize the cylinder that early.

reg1 = BoundaryDiscretizeRegion[Cylinder[{{0, 0, 0}, {10, 0, 0}}, 0.5], MaxCellMeasure -> 0.001];
reg2 = BoundaryDiscretizeRegion[Cuboid[{5, 0, 0}, {10, 1, 1}]];
reg = RegionDifference[reg1, reg2]

enter image description here

As a rule of thumb, I would strongly discourage applying boolean operations to graphics primitives and everything else which is neither a MeshRegion nor a BoundaryMeshRegion.

$\endgroup$
1
  • $\begingroup$ @JasonB. Thank you for the edit. That was an error that I make too often... $\endgroup$ Mar 14, 2019 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.