13

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

John Paul Peter
  • 1,315
  • 2
  • 10

2 Answers2

12

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[Opacity@.5, 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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • But if ViewPoint -> {8, 3, 7} ? – cvgmt Oct 14 '21 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 '21 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? – John Paul Peter Oct 14 '21 at 14:44
  • 1
    @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 '21 at 14:49
  • @JohnPaulPeter, please see the update. – kglr Oct 14 '21 at 19:09
  • @kglr Thank you very much. I do not see [this answer by Silvia] – John Paul Peter Oct 14 '21 at 23:21
  • @JohnPaulPeter, sorry forgot to paste the link. Added now. – kglr Oct 14 '21 at 23:29
  • @kglr I can not get the resullt. I copied all your code SeedRandom[7] Grid[Transpose @ Table[ {Graphics3D[{FaceForm[Directive[Opacity@.5, 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}] – John Paul Peter Oct 14 '21 at 23:57
  • @JohnPaulPeter, did you make the change in silvia's DashedGraphics3D code ( that is,is replace face3DPrimitives = { Cuboid, Cone, Cylinder, Sphere, Tube, BSplineSurface} with face3DPrimitives = {Pyramid, Cuboid, Cone, Cylinder, Sphere, Tube, BSplineSurface})? – kglr Oct 15 '21 at 00:04
7

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

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57