# How to plot the difference between two regions? [duplicate]

I'm trying to take the difference between two regions. In this case, one region is a cuboid, and the other is a cylinder. Here's what they look like drawn with Graphics3D:

hCub = 1.5;
W = 5;
L = 5;
cub = Cuboid[{0, 0, 0}, {W, L, hCub}];
cyl2botpt = {W, .5*L, 0};
cyl2toppt = {W, .5*L, hCub};
cyl2 = Cylinder[{cyl2botpt, cyl2toppt}, W/15];
Graphics3D[{Lighting -> "Neutral", Gray, Opacity[.5], cub, Opacity[1],
White, cyl2}, Boxed -> False, ImageSize -> 800]


Now what I'd really like to do is, subtract the cylinder region from the cuboid (so that what remains is the cuboid with half a cylinder sized volume missing from its side; I want the other side of the cylinder gone as well). More specifically, I'd like the part that was subtracted from the cuboid to have its surface be white, while the rest of the cuboid surface remains gray.

To attempt this, I tried using RegionDifference. However, that's not a graphics primitive anymore, so you can't use it with Graphics3D, you apparently have to use RegionPlot3D; in fact, it seems like you can't even stick it directly in there (I tried and it threw an error), because you have to use DiscretizeRegion on it first:

regdiff =
DiscretizeRegion[RegionDifference[cub, cyl2], AccuracyGoal -> 12];
RegionPlot3D[regdiff, PlotStyle -> Directive[Opacity[.4]],
BoundaryStyle -> Black, Boxed -> False]


However, this looks pretty bad. If you look at the edges, they're nowhere as clean as they were in the Graphics3D version:

I tried cranking the AccuracyGoal up in the code above, hoping that it would make a more accurate region, but for whatever reason it didn't appear to.

I also tried discretizing the cuboid and cylinder separately before taking their difference, but that didn't seem to help either.

Additionally, I'd have no clue how to do the coloring thing I'd like to achieve that I mentioned before.

So my question has two components: 1) Is this the best way to create this shape, or is there a clever way to do it with Graphics3D? Graphics3D seems "cleaner" but obviously you need primitives, and I don't know how to do it that way. And 2), how can I do the different boundary coloring that I mentioned?

• Increase the number of PlotPoints – Bob Hanlon Jul 13 '16 at 4:36
• I tried MaxCellMeasure -> {"Area" -> 0.01}. It takes more time to plot, but looks much better. – Lotus Jul 13 '16 at 4:47
• I marked this question as a duplicate of (48486) because that question, while not an exact duplicate, organizes other duplicates and seeks a solution beyond simply PlotPoints. If anyone disagrees with this and prefers a different original please comment. – Mr.Wizard Jul 13 '16 at 9:59
• @Mr.Wizard that's fair, I didn't find that question when I was searching. Do you think I should just make another question for the 2nd part of my question (regarding the coloring of different parts) ? – YungHummmma Jul 13 '16 at 15:38
• I am sorry, I must admit that I only skimmed your question and jumped to a conclusion as to its nature. I think the two questions are probably best separated however, so I encourage you to post a new question as described. I shall try to be more careful in the future. – Mr.Wizard Jul 13 '16 at 22:53

Introduction

Previously, I have done some similar work. Here is how I would approach it. Please bear in mind, there might be a better solution out there and I would give it some time for the community to respond

Example

RegionPlot3D[
RegionDifference[cub, cyl2],
PlotPoints -> 100, PlotStyle -> Directive[Red]
]


Note: cub and cyl12 are as in original post.

Output

Issues

I.1 - This code takes a bit of time to execute. Reducing the value passed to PlotPoints will speed up the execution.

Reference