# How to draw the dashed line automatic behind of a pyramid?

I am trying to draw this pyramid like this

I tried

\[ScriptCapitalR] =
Pyramid[{{0, 0, 0}, {2, 0, 0}, {2, 2, 0}, {0, 2, 0}, {0, 0, 2}}];
Graphics3D[{EdgeForm[{Thick, Blue}],
FaceForm[{Pink, Opacity[0.2]}], \[ScriptCapitalR]}
, Boxed -> False]


I got

How to draw the dashed line automatic behind of a pyramid? like this Dashed mesh behind 3D object

For a fixed viewpoint, you can use BoundaryDiscretizeRegion with the option MeshCellStyle to style individual line primitives:

pyramid = Pyramid[{{0, 0, 0}, {2, 0, 0}, {2, 2, 0}, {0, 2, 0}, {0, 0, 2}}];

Show[BoundaryDiscretizeRegion[pyramid ,
MaxCellMeasure -> Infinity,
PlotTheme -> "Lines",
MeshCellStyle -> {{1, 4|5|8} -> Directive[Black, Dashing[Large]], {1, _} -> Black},
MeshCellLabel -> MapThread[{0, #} -> Placed[Style[#2, 16, Bold], #3] &,
{Range @ 5, {"C", "D", " B", "A ", "S"}, {Below, Below, After, Before, Above}}],
ViewPoint -> {.1, -3, 1}]] /. {_Line} :> {}


Update:

We can use a slight modification of the function DashedGraphics3D from this answer by Silvia to get a 2D image of Graphics3D object with lines behind the front surface rendered as dashed lines. The only changes I made in the function are to add Pyramid in the list of 3D primitives,

face3DPrimitives = {Pyramid, Cuboid, Cone, Cylinder, Sphere, Tube,
BSplineSurface}


and replace Dashed with Dashing[Large]:

Examples:

SeedRandom[7]

Grid[Transpose @ Table[
{Graphics3D[{FaceForm[Directive[[email protected], LightBlue]],
EdgeForm[Thick], pyramid},
Boxed -> False, ViewPoint -> v,
ImageSize -> 400, SphericalRegion -> True,
PlotLabel -> PromptForm[Style["view point", 16], Style[v, 16]]],
Show[DashedGraphics3D[Graphics3D[pyramid, Boxed -> False],
ViewPoint -> v, ImagePadding -> 120], ImageSize -> 300]},
{v, Round[ RandomReal[{-2, 3}, {4, 3}], .01]}],
Spacings -> {1, 1}]


• But if ViewPoint -> {8, 3, 7} ? Oct 14, 2021 at 13:34
• @cvgmt, you need to replace 4|5|8 manually with appropriate list of indices for each spec for ViewPoint. For ViewPoint -> {8, 3, 7} the pattern 1|3|5 works. (You can use the option MeshCellLabel -> Join[Thread[ Thread[{0, Range[5]}] -> (Style[#, Bold, 16] & /@ {"C", "D", "B", "A", "S"})], {{1, All} -> "Index"}] to see the indices of line primitives.)
– kglr
Oct 14, 2021 at 13:43
• When I rotate the output of Mathematica, I get incorrect about dashed lines. How to draw the dashed line automatic like when I rotate the sphere at the link in my question? Oct 14, 2021 at 14:44
• @JohnPaulPeter, this method works for a fixed view point. (I will post an answer with a method that works for dynamically changing viewpoint.)
– kglr
Oct 14, 2021 at 14:49
• @JohnPaulPeter, please see the update.
– kglr
Oct 14, 2021 at 19:09

To make the hidden line dashed is pretty complicated. However, if you can live with hidden lines drawn only faintly, you may use the option "Opacity" like:

base = {{0, 0, 0}, {2, 0, 0}, {2, 2, 0}, {0, 2, 0}};
top = {{0, 0, 2}};
Graphics3D[{EdgeForm[Thickness[0.01]], Opacity[0.8],
Triangle[Join[top, #]] & /@ Partition[base, 2, 1, {1, 1}]}]