6
$\begingroup$
g=Graphics3D[{
  {Red, Sphere[{1, 1, 0}, Scaled[0.05`]]},
  {Green, Sphere[{1, 2, 3}, Scaled[0.05`]]},
  {Blue, Sphere[{-2, -2, 2}, Scaled[0.05`]]}}]

gives

enter image description here

Notice that spheres are all cut at the edge.

PlotRange->All does not work here. A more weird thing is that AbsoluteOptions@g

{AlignmentPoint -> Center, AspectRatio -> Automatic, 
 AutomaticImageSize -> False, Axes -> True, AxesEdge -> Automatic, 
 AxesLabel -> None, AxesOrigin -> Automatic, AxesStyle -> {}, 
 Background -> None, BaselinePosition -> Automatic, BaseStyle -> {}, 
 Boxed -> True, BoxRatios -> {1., 1., 1.}, BoxStyle -> {}, 
 ClipPlanes -> None, ClipPlanesStyle -> Automatic, 
 ColorOutput -> Automatic, ContentSelectable -> Automatic, 
 ControllerLinking -> False, ControllerMethod -> Automatic, 
 ControllerPath -> Automatic, CoordinatesToolOptions -> Automatic, 
 DisplayFunction -> Identity, Epilog -> {}, FaceGrids -> None, 
 FaceGridsStyle -> {}, FormatType -> TraditionalForm, 
 ImageMargins -> 0., ImagePadding -> All, ImageSize -> Automatic, 
 ImageSizeRaw -> Automatic, LabelStyle -> {}, Lighting -> Automatic, 
 Method -> Automatic, PlotLabel -> None, 
 PlotRange -> {{0., 1.}, {0., 1.}, {0., 1.}}, 
 PlotRangePadding -> Automatic, PlotRegion -> Automatic, 
 PreserveImageOptions -> Automatic, Prolog -> {}, 
 RotationAction -> "Fit", SphericalRegion -> Automatic, 
 Ticks -> Automatic, TicksStyle -> {}, TouchscreenAutoZoom -> False, 
 ViewAngle -> Automatic, ViewCenter -> {0.5, 0.5, 0.5}, 
 ViewMatrix -> Automatic, ViewPoint -> {1.3, -2.4, 2.}, 
 ViewProjection -> Automatic, ViewRange -> All, 
 ViewVector -> Automatic, ViewVertical -> {0., 0., 1.}}

Notice the PlotRange -> {{0., 1.}, {0., 1.}, {0., 1.}}, How could that be? This is not the correct PlotRange, then what PlotRange does Mathematica actually use internally? Why can not AbsoluteOption correctly show it?

I know I can use for example PlotRangePadding -> 1 to make all sphere complete. But How to make the plot range just enough for any cases without manual adjustment? I think the key is to get the correct absolute PlotRange.

$\endgroup$

1 Answer 1

3
$\begingroup$
ClearAll[boundingBoxDiagonal, fromScaledRadii]

boundingBoxDiagonal = EuclideanDistance @@ Transpose@
     CoordinateBounds @ Transpose[Cases[#, Sphere[c_, _] :> c, All]] &;

fromScaledRadii = ReplaceAll[#,
   Sphere[c_, Scaled @ r_] :> Sphere[c, r boundingBoxDiagonal @ #]] &;

Examples:

g1 = Graphics3D[{{Red, Sphere[{1, 1, 0}, Scaled[0.05]]},
       {Green, Sphere[{1, 2, 3}, Scaled[0.05]]}, 
       {Blue,  Sphere[{-2, -2, 2}, Scaled[0.05]]}}, 
      ImageSize -> Medium];

Row[{g1, Show[fromScaledRadii @ g1, PlotRangePadding -> None]}]

enter image description here

g2 = Graphics3D[{{Red, Sphere[{1, 1, 0}, Scaled[0.1]]}, 
       {Green, Sphere[{1, 2, 3}, Scaled[0.025]]}, 
       {Blue,  Sphere[{-2, -2, 2}, Scaled[0.2]]}}, 
      ImageSize -> Medium];

Row[{g2, Show[fromScaledRadii @ g2, PlotRangePadding -> None]}]

enter image description here

$\endgroup$
3
  • $\begingroup$ kglr, Thank you so much for answering. If I understand correctly, you convert scaled radius to absolute radius. But I think that may not be a general fix. For example, I actually need to put scaled fontsize label on each sphere, and if the sphere radius becomes absolute, the total graphics3D would be a mass when scaling. $\endgroup$
    – matheorem
    Commented Feb 7, 2022 at 15:47
  • $\begingroup$ and your boundingBoxDiagonal seems not right. If we use Axes->True. We can see the diagonal is approximately sqrt(43) $\endgroup$
    – matheorem
    Commented Feb 7, 2022 at 15:49
  • $\begingroup$ A sphere with scaled text would be something like Graphics3D[{Red, Sphere[{1, 1, 0}, Scaled[0.05]], FontSize -> Scaled[0.05], Text[Style["A", White, Bold], {1, 1, 0}]}] $\endgroup$
    – matheorem
    Commented Feb 7, 2022 at 15:56

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.