6
$\begingroup$

I am trying to draw a figure and color it like this from here:

enter image description here

enter image description here

I tried

CSGRegion["Difference", {Ball[{0, 0, 0}, 5], Cylinder[{{0, 0, -5}, {0, 0, 5}}, 2]}]

enter image description here

and

With[{r = 5, rHole = 2.5}, 
 ParametricPlot3D[{r Sin[\[Theta]] Cos[\[Phi]], 
   r Sin[\[Theta]] Sin[\[Phi]], r Cos[\[Theta]]}, {\[Theta], 
   0, \[Pi]}, {\[Phi], 0, 2 \[Pi]}, PlotPoints -> 100, Mesh -> None, 
  RegionFunction -> 
   Function[{x, y, z, \[Theta], \[Phi]}, x^2 + y^2 >= rHole^2], 
  ColorFunction -> 
   Function[{x, y, z, \[Theta], \[Phi]}, 
    ColorData["TemperatureMap"][Rescale[z, {-r, r}]]], 
  ColorFunctionScaling -> False, Lighting -> "Neutral", Axes -> False,
   Boxed -> False]]

enter image description here

How can I color my graphic like in the first screenshot?

$\endgroup$
2
  • $\begingroup$ CSGRegion["Difference", {Style[Ball[{0, 0, 0}, 5], Blue], Style[Cylinder[{{0, 0, -5}, {0, 0, 5}}, 2], Red]}]? $\endgroup$ Commented Aug 2, 2025 at 9:18
  • $\begingroup$ @cvgmt Can you make it like the first first screenshot? $\endgroup$ Commented Aug 2, 2025 at 9:26

1 Answer 1

15
$\begingroup$
sphere = 
  SphericalPlot3D[5, {θ, 0, π}, {φ, 0, 2 π}, 
   RegionFunction -> Function[{x, y, z}, x^2 + y^2 >= 2^2], 
   Boxed -> False, Axes -> False, MeshStyle -> White, 
   ColorFunction -> Function[{x, y, z}, ColorData["Rainbow"][z]], 
   PlotStyle -> Opacity[.5]];
cylinder = 
  ContourPlot3D[
   x^2 + y^2 == 2^2, {x, y, z} ∈ Ball[{0, 0, 0}, 5], 
   RegionBoundaryStyle -> None, ContourStyle -> FaceForm[Yellow, Red],
    Mesh -> None];
Show[sphere, cylinder]

enter image description here

  • If we do not use the expression of sphere and cylinder, we could use OpenCascadeLink and FEM to separate the exterior sphere and interior cylinder faces. see the answer by @user21
Clear["Global`*"];
Needs["OpenCascadeLink`"];
Needs["NDSolve`FEM`"];
reg1 = Ball[{0, 0, 0}, 5];
reg2 = Cylinder[{{0, 0, -5}, {0, 0, 5}}, 2];
shape = OpenCascadeShapeDifference[
   OpenCascadeShape /@ {reg1,reg2}];
bmesh = OpenCascadeShapeSurfaceMeshToBoundaryMesh[shape, 
   "ShapeSurfaceMeshOptions" -> {"AngularDeflection" -> .05}];
mesbm = Flatten[
   MeshElementSplitByMarker[
    MeshOrderAlteration[bmesh, 1]["BoundaryElements"]]];
{sphere, cylinder} = 
  MeshRegion[bmesh["Coordinates"], Polygon[ElementIncidents[#]]] & /@ 
   mesbm;
{RegionPlot3D[sphere, 
   ColorFunction -> Function[{x, y, z}, ColorData["Rainbow"][z]], 
   PlotStyle -> Opacity[.5], MeshFunctions -> {#3 &, ArcTan[#1, #2] &},
    Mesh -> 20, MeshStyle -> White, Boxed -> False], 
  Graphics3D[{EdgeForm[], FaceForm[Yellow, Red], cylinder}]} // Show

enter image description here

  • Reply to comment for sphere and cone ets.
Clear["Global`*"];
Needs["OpenCascadeLink`"];
Needs["NDSolve`FEM`"];
clip[reg1_, reg2_] := Module[{shape, bmesh, mesbm, regs},
  shape = OpenCascadeShapeDifference[OpenCascadeShape /@ {reg1, reg2}];
  bmesh = 
   OpenCascadeShapeSurfaceMeshToBoundaryMesh[shape, 
    "ShapeSurfaceMeshOptions" -> {"AngularDeflection" -> .05}];
  mesbm = Flatten[MeshElementSplitByMarker[bmesh["BoundaryElements"]]];
  regs = 
   MeshRegion[bmesh["Coordinates"], Polygon[ElementIncidents[#]]] & /@
     mesbm;
  {RegionPlot3D[regs[[1]], 
     ColorFunction -> Function[{x, y, z}, ColorData["Rainbow"][z]], 
     PlotStyle -> Opacity[.5], 
     MeshFunctions -> {#3 &, ArcTan[#1, #2] &}, Mesh -> 20, 
     MeshStyle -> White, Boxed -> False], 
    Graphics3D[{EdgeForm[], FaceForm[Yellow, Red], 
      regs[[Complement[
         bmesh["BoundaryElementMarkerUnion"], {1}]]]}]} // Show]
reg1 = Ball[{0, 0, 0}, 5];
reg2 = Cone[{{1, 3, -5}, {2, -7, 5}}, 4];
clip[reg1, reg2]

enter image description here

reg1 = Ball[{0, 0, 0}, 5];
reg2 = TransformedRegion[Ellipsoid[{0, 0, 0}, {7, 2, 3}], 
   RotationTransform[π/3, {1, 1, 1}]];
clip[reg1, reg2]

enter image description here

reg1 = Ball[{0, 0, 0}, 5];
reg2 = FilledTorus[{3, 2, 1}, {3, 5}];
clip[reg1, reg2]

enter image description here

$\endgroup$
3
  • $\begingroup$ Does your method still work for this? CSGRegion["Difference", {Ball[{0, 0, 0}, 5], Style[Cone[{{1, 3, -5}, {2, -7, 5}}, 4], Orange]}] $\endgroup$ Commented Aug 4, 2025 at 1:55
  • $\begingroup$ @minthao_2011 see the updated. $\endgroup$ Commented Aug 5, 2025 at 0:30
  • $\begingroup$ Thank you very much. $\endgroup$ Commented Aug 5, 2025 at 0:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.