tikz add tag
Anonymous 1123
I have two pictures are drawn with GeospacW. How to draw this pictures in Tikz?
![ScreenHunter 872.png](/image?hash=11e4a75b5d1ca461396ffd6a77b1927c4c0a300a1316a49a447b352e0c006f6f)

and 

![ScreenHunter 873.png](/image?hash=ebd3a7c5828e5c2ea1be45e94ff727d29b0d04bd62fb34761475a52a31f766f9)
Top Answer
marmot
Ususally it is rather hard to check whether a line gets blocked by another object. In this case one can make things work with some reversed clips. This should give you the correct picture as long as you do not change the view angle `theta` too much.

```
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{3dtools}% https://github.com/marmotghost/tikz-3dtools
% https://tex.stackexchange.com/a/59168
\tikzset{reverseclip/.style={overlay,insert path={(-12383.99999pt,-12383.99999pt) rectangle (12383.99999pt,12383.99999pt)}}}
\begin{document}
\begin{tikzpicture}[declare function={a=2;h=5;}]
 \begin{scope}[3d/install view={phi=70,psi=0,theta=70}]
  \path (-a,-a,0) coordinate (A) (-a,a,0) coordinate (B)
   (a,a,0) coordinate (C) (a,-a,0) coordinate (D)
   (0,0,h) coordinate (T) (0,0,h/2) coordinate (I);
  \tikzset{3d/polyhedron/.cd,O={(I)},fore/.style={fill=none},
  	draw face with corners={{(A)},{(B)},{(T)}},
	draw face with corners={{(B)},{(C)},{(T)}},
	draw face with corners={{(C)},{(D)},{(T)}},
	draw face with corners={{(D)},{(A)},{(T)}}} 
  \edef\Xmax{6}	
  \begin{scope}[3d/polyhedron/.cd,O={(I)},
   	fore/.style={fill=none,draw=red},back/.style={fill=none,draw=red,densely dashed}] 
   \foreach \X [count=\Y] in
   {\the\numexpr\Xmax-1,\the\numexpr\Xmax-2,...,1}	
   {\pgfmathsetmacro{\myx}{(\Xmax-(\X))*a/\Xmax}
	\pgfmathsetmacro{\myy}{h*(\X)/\Xmax}
	\pgfmathsetmacro{\myz}{h*(\X-1)/\Xmax}
	\path (-\myx,-\myx,\myz) coordinate (A0-\Y) (-\myx,\myx,\myz) coordinate (B0-\Y)
     (\myx,\myx,\myz) coordinate (C0-\Y) (\myx,-\myx,\myz) coordinate (D0-\Y)
	 (-\myx,-\myx,\myy) coordinate (A1-\Y) (-\myx,\myx,\myy) coordinate (B1-\Y)
     (\myx,\myx,\myy) coordinate (C1-\Y) (\myx,-\myx,\myy) coordinate (D1-\Y);
	\tikzset{3d/polyhedron/.cd,
  	 draw face with corners={{(A0-\Y)},{(B0-\Y)},{(B1-\Y)},{(A1-\Y)}},
	 draw face with corners={{(B0-\Y)},{(C0-\Y)},{(C1-\Y)},{(B1-\Y)}},
	 draw face with corners={{(C0-\Y)},{(D0-\Y)},{(D1-\Y)},{(C1-\Y)}},
	 draw face with corners={{(D0-\Y)},{(A0-\Y)},{(A1-\Y)},{(D1-\Y)}}}  
	\ifnum\Y=1
	 \draw[red] (A1-\Y) -- (B1-\Y) -- (C1-\Y) -- (D1-\Y) -- cycle;
	\else
     \pgfmathtruncatemacro{\Z}{\Y-1}
	 \begin{scope}
	  \clip (A0-\Z) -- (B0-\Z) -- (B1-\Z) -- (A1-\Z) -- cycle [reverseclip];
	  \clip (B0-\Z) -- (C0-\Z) -- (C1-\Z) -- (C1-\Z) -- cycle [reverseclip];
	  \clip (C0-\Z) -- (D0-\Z) -- (D1-\Z) -- (C1-\Z) -- cycle [reverseclip];
	  \clip (D0-\Z) -- (A0-\Z) -- (A1-\Z) -- (D1-\Z) -- cycle [reverseclip];
	  \draw[red] (A1-\Y) -- (B1-\Y) -- (C1-\Y) -- (D1-\Y) -- cycle;
	 \end{scope}
	\fi	  
   }
  \end{scope}
 \end{scope}
\end{tikzpicture}
\end{document}
```
![Screen Shot 2020-10-17 at 9.40.01 AM.png](/image?hash=8d899a84076891faf5b2eb4dd022ebc9fdef28a08c1c8dbab59cb87862c44286)

The reverse clip method has glitches when one combines too many reverse clips and/or some contours have the "wrong" orientation. For this reason I added a style for the convex hull of points. After I was done, I realized that [solutions exist already](https://tex.stackexchange.com/questions/249860/compute-and-draw-a-convex-hull). I did not understand all strategies there but believe that I basically rediscovered [this strategy](https://tex.stackexchange.com/a/250354). At a given point I may add the style to the `3dtools` library because it may allow us to tackle many more cases than previously thought: just collect all faces that are drawn already and are in the foreground and use some clips and reverse clips to draw the solid or dashed lines further back. Of course one will still need reliable and simple methods to decide wheter a face is further back.

```
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{3dtools}% https://github.com/marmotghost/tikz-3dtools
% https://tex.stackexchange.com/a/59168
\tikzset{reverseclip/.style={overlay,insert
path={(-12383.99999pt,-12383.99999pt) -| (12383.99999pt,12383.99999pt) -| cycle}}}
\makeatletter
\pgfmathdeclarefunction{x2d}{1}{%
\begingroup%
\pgf@process{\pgfpointanchor{#1}{center}}%
\pgfmathparse{\pgf@x}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\pgfmathdeclarefunction{y2d}{1}{%
\begingroup%
\pgf@process{\pgfpointanchor{#1}{center}}%
\pgfmathparse{\pgf@y}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%

\tikzset{convex hull of/.code={% expects a list of points
\edef\pgfutil@tmpl{#1}% store the list for manipulations
\edef\pgfutil@tmpp{}% path to be constructed
\c@pgf@counta1%
\pgfutil@for\pgfutil@tmpa:={\pgfutil@tmpl}\do{%
\ifnum\c@pgf@counta=1\relax
\pgfmathsetmacro{\pgfutil@tmpb}{x2d("\pgfutil@tmpa")}% min
\edef\pgfutil@tmpd{\pgfutil@tmpa}% leftmost point
\else
\pgfmathsetmacro{\pgfutil@tmpc}{x2d("\pgfutil@tmpa")}% x coordinate
\ifdim\pgfutil@tmpc pt<\pgfutil@tmpb pt\relax
\edef\pgfutil@tmpb{\pgfutil@tmpc}% new min
\edef\pgfutil@tmpd{\pgfutil@tmpa}% new leftmost point
\fi
\fi
\advance\c@pgf@counta by1%
}
\edef\pgfutil@tmpp{(\pgfutil@tmpd)}%
\edef\pgfutil@tmpj{\pgfutil@tmpd}%
\edef\pgfutil@tmpe{90}% slope correction
\edef\pgfutil@tmpk{1}% first round flag
\loop
%\typeout{Removing  \pgfutil@tmpd}%
\edef\pgfutil@tmph{\noexpand\@removeelement{\pgfutil@tmpd}{\pgfutil@tmpl}{\noexpand\pgfutil@tmpl}}%
\pgfutil@tmph
%\typeout{List is now \pgfutil@tmpl}%
\c@pgf@countb1%
\pgfutil@for\pgfutil@tmpa:={\pgfutil@tmpl}\do{%
\pgfmathsetmacro{\pgfutil@tmpf}{Mod(atan2(y2d("\pgfutil@tmpa")-y2d("\pgfutil@tmpd"),
x2d("\pgfutil@tmpa")-x2d("\pgfutil@tmpd"))+\pgfutil@tmpe,360)}%
%\typeout{\the\c@pgf@countb: \pgfutil@tmpa\space has angle \pgfutil@tmpf.}%
\ifnum\c@pgf@countb=1\relax
\edef\pgfutil@tmpg{\pgfutil@tmpf}%
\edef\pgfutil@tmpi{\pgfutil@tmpa}%
\else
%\unless\ifdim\pgfutil@tmpf pt>180pt\relax
\ifdim\pgfutil@tmpf pt>\pgfutil@tmpg pt\relax
\edef\pgfutil@tmpg{\pgfutil@tmpf}%
\edef\pgfutil@tmpi{\pgfutil@tmpa}%
\fi
%\fi
\fi
\advance\c@pgf@countb by1%
}
%
\ifx\pgfutil@tmpi\pgfutil@tmpj\relax
\c@pgf@counta=2\relax
\else
\edef\pgfutil@tmpd{\pgfutil@tmpi}%
\edef\pgfutil@tmpp{\pgfutil@tmpp--(\pgfutil@tmpd)}%
\pgfmathsetmacro{\pgfutil@tmpe}{180+\pgfutil@tmpe-\pgfutil@tmpf}%
%\typeout{offset angle=\pgfutil@tmpe}%
\advance\c@pgf@counta by-1%
\fi
\ifnum\pgfutil@tmpk=1\relax
\edef\pgfutil@tmpl{\pgfutil@tmpl,\pgfutil@tmpj}% add the first point again
\edef\pgfutil@tmpk{0}%
\fi
\ifnum\c@pgf@counta>2\relax
\repeat
\edef\pgfutil@tmpp{\pgfutil@tmpp--cycle}%
\tikzset{insert path=\pgfutil@tmpp}%
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[declare function={a=2;h=5;},
	3d/polyhedron/.cd,fore/.style={fill=none,draw,solid},
	back/.style={fill=none,draw,densely dashed}]
 \begin{scope}[3d/install view={phi=70,psi=0,theta=70},
 	3d/polyhedron/.cd,O={(I)},]
  \path (-a,-a,0) coordinate (A) (-a,a,0) coordinate (B)
   (a,a,0) coordinate (C) (a,-a,0) coordinate (D)
   (0,0,h) coordinate (T) (0,0,h/2) coordinate (I);
  \tikzset{3d/polyhedron/.cd,O={(I)},
  	draw face with corners={{(A)},{(B)},{(T)}},
	draw face with corners={{(B)},{(C)},{(T)}},
	draw face with corners={{(C)},{(D)},{(T)}},
	draw face with corners={{(D)},{(A)},{(T)}}} 
  \edef\Xmax{6}	
  \begin{scope}[3d/polyhedron/.cd,O={(I)},
   	fore/.append style={draw=red},
	back/.append style={draw=none}] 
   \foreach \X [count=\Y] in
   {\the\numexpr\Xmax-1,\the\numexpr\Xmax-2,...,1}	
   {\pgfmathsetmacro{\myx}{(\Xmax-(\X))*a/\Xmax}
	\pgfmathsetmacro{\myy}{h*(\X)/\Xmax}
	\pgfmathsetmacro{\myz}{h*(\X-1)/\Xmax}
	\path (-\myx,-\myx,\myz) coordinate (A0-\Y) (-\myx,\myx,\myz) coordinate (B0-\Y)
     (\myx,\myx,\myz) coordinate (C0-\Y) (\myx,-\myx,\myz) coordinate (D0-\Y)
	 (-\myx,-\myx,\myy) coordinate (A1-\Y) (-\myx,\myx,\myy) coordinate (B1-\Y)
     (\myx,\myx,\myy) coordinate (C1-\Y) (\myx,-\myx,\myy) coordinate (D1-\Y);
	\tikzset{3d/polyhedron/.cd,
  	 draw face with corners={{(A0-\Y)},{(B0-\Y)},{(B1-\Y)},{(A1-\Y)}},
	 draw face with corners={{(B0-\Y)},{(C0-\Y)},{(C1-\Y)},{(B1-\Y)}},
	 draw face with corners={{(C0-\Y)},{(D0-\Y)},{(D1-\Y)},{(C1-\Y)}},
	 draw face with corners={{(D0-\Y)},{(A0-\Y)},{(A1-\Y)},{(D1-\Y)}}}  
	\ifnum\Y=1
	 \draw[red] (A1-\Y) -- (B1-\Y) -- (C1-\Y) -- (D1-\Y) -- cycle;
	\else
     \pgfmathtruncatemacro{\Z}{\Y-1}
	 \begin{scope}
	  \clip[convex hull of={A0-\Z,B0-\Z,C0-\Z,D0-\Z,A1-\Z,B1-\Z,C1-\Z,D1-\Z}] 
	  	[reverseclip];
	  \draw[red] (A1-\Y) -- (B1-\Y) -- (C1-\Y) -- (D1-\Y) -- cycle;
	 \end{scope}
	\fi	  
   }
  \end{scope}
 \end{scope}
\end{tikzpicture}
\end{document}
```
![Screen Shot 2020-10-17 at 10.16.44 AM.png](/image?hash=0247bbf4e505bb53231c27357865bc1631bf53cbd70f13e40cd351744c0636f9)

I added the `convex hull of` style to the [`3dtools` library](https://github.com/marmotghost/tikz-3dtools). The code can be shortened accordingly. 
```
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{3dtools}% https://github.com/marmotghost/tikz-3dtools
% https://tex.stackexchange.com/a/59168
\tikzset{reverseclip/.style={overlay,insert path={(-12383.99999pt,-12383.99999pt) rectangle (12383.99999pt,12383.99999pt)}}}
\pgfdeclarelayer{background} 
\pgfdeclarelayer{foreground}
\pgfdeclarelayer{behind}  
\pgfsetlayers{behind,background,main,foreground}
\begin{document}
\begin{tikzpicture}[declare function={a=2;h=5;},line cap=round,
	line join=round,
	3d/polyhedron/.cd,fore/.style={fill=none,draw,solid},
	back/.style={fill=none,draw,dash pattern=on 0.6pt off 1pt,very thin}]
 \begin{scope}[3d/install view={phi=70,psi=0,theta=70},
 	3d/polyhedron/.cd,O={(I)},]
  \path (-a,-a,0) coordinate (A) (-a,a,0) coordinate (B)
   (a,a,0) coordinate (C) (a,-a,0) coordinate (D)
   (0,0,h) coordinate (T) (0,0,h/2) coordinate (I);
  \tikzset{3d/polyhedron/.cd,O={(I)},fore layer=foreground,
  	back layer=behind,
  	draw face with corners={{(A)},{(B)},{(T)}},
	draw face with corners={{(B)},{(C)},{(T)}},
	draw face with corners={{(C)},{(D)},{(T)}},
	draw face with corners={{(D)},{(A)},{(T)}}} 
  \edef\Xmax{6}	
  \begin{scope}[3d/polyhedron/.cd,O={(I)},
   	fore/.append style={draw=red},
	back/.append style={draw=red}] 
   \foreach \X [count=\Y] in
   {\the\numexpr\Xmax-1,\the\numexpr\Xmax-2,...,1}	
   {\pgfmathsetmacro{\myx}{(\Xmax-(\X))*a/\Xmax}
	\pgfmathsetmacro{\myy}{h*(\X)/\Xmax}
	\pgfmathsetmacro{\myz}{h*(\X-1)/\Xmax}
	\path (-\myx,-\myx,\myz) coordinate (A0-\Y) (-\myx,\myx,\myz) coordinate (B0-\Y)
     (\myx,\myx,\myz) coordinate (C0-\Y) (\myx,-\myx,\myz) coordinate (D0-\Y)
	 (-\myx,-\myx,\myy) coordinate (A1-\Y) (-\myx,\myx,\myy) coordinate (B1-\Y)
     (\myx,\myx,\myy) coordinate (C1-\Y) (\myx,-\myx,\myy) coordinate (D1-\Y);
	\tikzset{3d/polyhedron/.cd,
  	 draw face with corners={{(A0-\Y)},{(B0-\Y)},{(B1-\Y)},{(A1-\Y)}},
	 draw face with corners={{(B0-\Y)},{(C0-\Y)},{(C1-\Y)},{(B1-\Y)}},
	 draw face with corners={{(C0-\Y)},{(D0-\Y)},{(D1-\Y)},{(C1-\Y)}},
	 draw face with corners={{(D0-\Y)},{(A0-\Y)},{(A1-\Y)},{(D1-\Y)}}}  
	\ifnum\Y=1
	 \draw[red] (A1-\Y) -- (B1-\Y) -- (C1-\Y) -- (D1-\Y) -- cycle;
	\else
     \pgfmathtruncatemacro{\Z}{\Y-1}
	 \begin{scope}
	  \clip (A0-\Z) -- (D0-\Z) -- (C0-\Z) -- (C1-\Z) -- (B1-\Z)
	   -- (A1-\Z) -- cycle [reverseclip];
	  \draw[red] (A1-\Y) -- (B1-\Y) -- (C1-\Y) -- (D1-\Y) -- cycle;
	 \end{scope}
	\fi	  
   }
  \end{scope}
 \end{scope}
\end{tikzpicture}
\end{document}
```
![Screen Shot 2020-10-17 at 10.49.59 PM.png](/image?hash=47cba1f6c4250a0ebd7e2348da5561b717cd6a7e9884bb263f925a609f21dd96)

Here is an animated version based on [this comment](https://topanswers.xyz/transcript?room=1472&id=77818#c77818).
```
\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3dtools}% https://github.com/marmotghost/tikz-3dtools
% https://tex.stackexchange.com/a/59168
% Notice the change of orientation here, i.e. no rectangle
\tikzset{reverseclip/.style={overlay,insert
path={(-12383.99999pt,-12383.99999pt) -| (12383.99999pt,12383.99999pt) -| cycle}}}
\pgfdeclarelayer{background} 
\pgfdeclarelayer{foreground}
\pgfdeclarelayer{behind}  
\pgfsetlayers{behind,background,main,foreground}
\begin{document}
\foreach \Angle in {5,15,...,355}
{\tdplotsetmaincoords{60}{\Angle}
\begin{tikzpicture}[declare function={a=2;h=5;},line cap=round,
	line join=round,tdplot_main_coords,
	3d/polyhedron/.cd,fore/.style={fill=none,draw,solid},
	back/.style={fill=none,draw,dash pattern=on 0.6pt off 1pt,very thin}]
 \path[tdplot_screen_coords,use as bounding box] (-3.5,-2) rectangle (3.5,5.5);
 \begin{scope}[3d/polyhedron/.cd,O={(I)},]
  \path (-a,-a,0) coordinate (A) (-a,a,0) coordinate (B)
   (a,a,0) coordinate (C) (a,-a,0) coordinate (D)
   (0,0,h) coordinate (T) (0,0,h/2) coordinate (I);
  \tikzset{3d/polyhedron/.cd,O={(I)},fore layer=foreground,
  	back layer=behind,
  	draw face with corners={{(A)},{(B)},{(T)}},
	draw face with corners={{(B)},{(C)},{(T)}},
	draw face with corners={{(C)},{(D)},{(T)}},
	draw face with corners={{(D)},{(A)},{(T)}}} 
  \edef\Xmax{6}	
  \begin{scope}[3d/polyhedron/.cd,O={(I)},
   	fore/.append style={draw=red},
	back/.append style={draw=red}] 
   \foreach \X [count=\Y] in
   {\the\numexpr\Xmax-1,\the\numexpr\Xmax-2,...,1}	
   {\pgfmathsetmacro{\myx}{(\Xmax-(\X))*a/\Xmax}
	\pgfmathsetmacro{\myy}{h*(\X)/\Xmax}
	\pgfmathsetmacro{\myz}{h*(\X-1)/\Xmax}
	\path (-\myx,-\myx,\myz) coordinate (A0-\Y) (-\myx,\myx,\myz) coordinate (B0-\Y)
     (\myx,\myx,\myz) coordinate (C0-\Y) (\myx,-\myx,\myz) coordinate (D0-\Y)
	 (-\myx,-\myx,\myy) coordinate (A1-\Y) (-\myx,\myx,\myy) coordinate (B1-\Y)
     (\myx,\myx,\myy) coordinate (C1-\Y) (\myx,-\myx,\myy) coordinate (D1-\Y)
	 (0,0,0.5*\myy+0.5*\myz) coordinate (I-\Y);
	\ifnum\Y=1
	  \tikzset{3d/polyhedron/.cd,
	   draw face with corners={{(A1-\Y)},{(B1-\Y)},{(C1-\Y)},{(D1-\Y)}},
  	   draw face with corners={{(A0-\Y)},{(B0-\Y)},{(B1-\Y)},{(A1-\Y)}},
	   draw face with corners={{(B0-\Y)},{(C0-\Y)},{(C1-\Y)},{(B1-\Y)}},
	   draw face with corners={{(C0-\Y)},{(D0-\Y)},{(D1-\Y)},{(C1-\Y)}},
	   draw face with corners={{(D0-\Y)},{(A0-\Y)},{(A1-\Y)},{(D1-\Y)}}}  
	\else
     \pgfmathtruncatemacro{\Z}{\Y-1}
	 %\begin{scope}
	  \draw[red,dash pattern=on 0.6pt off 1pt,very thin] (A1-\Y) -- (B1-\Y) -- (C1-\Y) -- (D1-\Y) -- cycle;
	  \tikzset{3d/polyhedron/.cd,O={(I-\Y)},
	   before hidden/.code={
	   \clip[convex hull of={A0-\Z,B0-\Z,C0-\Z,D0-\Z,A1-\Z,B1-\Z,C1-\Z,D1-\Z}] 
	  	[reverseclip];},
	   before visible/.code={\clip[convex hull of={A0-\Z,B0-\Z,C0-\Z,D0-\Z,A1-\Z,B1-\Z,C1-\Z,D1-\Z}] 
	  	[reverseclip];},
  	   draw face with corners={{(A0-\Y)},{(B0-\Y)},{(B1-\Y)},{(A1-\Y)}},
	   draw face with corners={{(B0-\Y)},{(C0-\Y)},{(C1-\Y)},{(B1-\Y)}},
	   draw face with corners={{(C0-\Y)},{(D0-\Y)},{(D1-\Y)},{(C1-\Y)}},
	   draw face with corners={{(D0-\Y)},{(A0-\Y)},{(A1-\Y)},{(D1-\Y)}},
	   draw face with corners={{(A1-\Y)},{(B1-\Y)},{(C1-\Y)},{(D1-\Y)}}}  
	 %\end{scope}
	\fi	  
   }
  \end{scope}
 \end{scope}
\end{tikzpicture}}
\end{document}
```
![ani.gif](/image?hash=134aa19f5a8ae89c158e1ea593363cc6ee7e425eb182c4cef378fa456ebd5b64)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.