I do have a somewhat complex question (that might be easy to solve, please bear with me I picked up Mathematica in the last two hours). I want to draw 3D shapes using Mathematica. Specifically I want to draw a rectangle, a cylinder and remove a complex shaped that can be drawn by using Plot or PolarPlot in 2D.

A good starting point would be simply removing another cylinder from my bigger cylinder. A good example on how I could draw all the shapes is shown below.

r = 1.2;
rPerf = 0.5;
b = 2.4;
w = 5;
l = w;
oringP = {-l/2, -l/2, 0};

cube1 = Cuboid[oringP, {w/2, l/2, 1}];
cyl1 = Cylinder[{{0, 0, 1}, {0, 0, b + 1}}, r];
cyl2 = Cylinder[{{0, 0, 1.5}, {0, 0, b + 1}}, rPerf];

myExampleDrawing = 
 Graphics3D[{EdgeForm[{Thick, Black}], cube1, cyl1, cyl2,
                AspectRatio -> Automatic,
                ImageSize -> 200}, Boxed -> False]

I found out I can use RegionDifference but that doesn't allow me to keep using Graphics3D which seems much more computational friendly and keeps smooth lines. I can generate something closer to the previous one, with the subtraction, by using the following line

RegionPlot3D[RegionUnion[RegionDifference[cyl1, cyl2], cube1], PlotPoints -> 70]

However, it has some artefacts on the union between the cuboid and the cylinder. Finally, I would like to have a complex shape instead of cyl2, extrude it much like in this thread where a function to extrude the points is used. Then finally I'd like to subtract this extruded complex shape from the cylinder. However, I don't seem to be able to reproduce the example code (extracted from the linked answer). The code is the following:

R[\[Theta]_] := (1 + 0.5 Sin[2 \[Theta]]);
shape1 = PolarPlot[R[\[Theta]], {\[Theta], 0, 2 \[Pi]}, Axes -> False,
    PlotStyle -> {Orange, Thickness[0.02]}];
points = (Flatten@shape1[[1]])[[2, 1]];
Options[Extrude] = 
  Join[Options[Graphics3D], {Closed -> True, Capped -> True}];

Extrude[curve_, {zmin_, zmax_}, opts : OptionsPattern[]] := 
  Module[{info, points, color, tube, caps}, 
   info = Flatten@{curve[[1]]};
   points = Select[info, Head[#] === Line &][[1, 1]];
   If[OptionValue[Closed], points = points~Join~{points[[1]]}];
   color = Select[info, Head[#] === Directive &];
   If[Length[color] == 0, color = Orange, 
    color = First@Select[color[[1]], ColorQ]];
   tube = 
        points /. {x_, y_} -> {x, y, #} & /@ {zmin, zmax}], 1], 3, 1]];
   If[OptionValue[Closed] && OptionValue[Capped], 
    caps = Polygon[points /. {x_, y_} -> {x, y, #}] & /@ {zmin, zmax};
    tube = Flatten@{tube, caps}, tube = {tube}];
   Graphics3D[Flatten@{EdgeForm[None], color, #} & /@ tube, 
    FilterRules[{opts}, Options[Graphics3D]]]];
Extrude[shape1, {-2, 5}, Boxed -> False]

It returns several errors.

So basically I want to understand if it is possible to:

  1. Create 3D geometries using subtraction of primitives and plot it using Graphics3D
  2. Extrude 2D geometries easily
  3. Create 3D geometries using subs traction of primitives and the extruded geometry while still plotting it with Graphics3D


Your Answer

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

Browse other questions tagged or ask your own question.