13
$\begingroup$

I am trying to draw this pyramid like this enter image description here

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 enter image description here

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

$\endgroup$

2 Answers 2

12
$\begingroup$

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} :> {}

enter image description here

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}]

enter image description here

$\endgroup$
10
  • $\begingroup$ But if ViewPoint -> {8, 3, 7} ? $\endgroup$
    – cvgmt
    Oct 14, 2021 at 13:34
  • $\begingroup$ @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.) $\endgroup$
    – kglr
    Oct 14, 2021 at 13:43
  • $\begingroup$ 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? $\endgroup$ Oct 14, 2021 at 14:44
  • 1
    $\begingroup$ @JohnPaulPeter, this method works for a fixed view point. (I will post an answer with a method that works for dynamically changing viewpoint.) $\endgroup$
    – kglr
    Oct 14, 2021 at 14:49
  • $\begingroup$ @JohnPaulPeter, please see the update. $\endgroup$
    – kglr
    Oct 14, 2021 at 19:09
7
$\begingroup$

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}]}]

enter image description here

$\endgroup$

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.