tikz add tag
Anonymous 1123
I am trying to draw  a  rectangular parallelepiped  is inscribed in a right circular cone. My code
```
\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{fouriernc}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,backgrounds}
\begin{document}
%polar coordinates of visibility
\pgfmathsetmacro\th{65}
\pgfmathsetmacro\az{110}
\tdplotsetmaincoords{\th}{\az}
%parameters of the cone
\pgfmathsetmacro\R{5} %radius of base
\pgfmathsetmacro\v{2*\R} %hight of cone
\pgfmathsetmacro\a{3}
\pgfmathsetmacro\b{4}
\pgfmathsetmacro\r{{1/2*sqrt(\a*\a+\b*\b)}}
\pgfmathsetmacro\h{(\R-\r)*\v/\R}

\begin{tikzpicture} [scale=0.8, tdplot_main_coords]
\begin{scope}[canvas is xy plane at z=0]
\path
coordinate (O) at (0,0)
coordinate (A) at (\R,0)
coordinate (B) at (0,\R)
coordinate (C) at (-\R,0)
coordinate (D) at (0,-\R,0)
coordinate (M) at (\r,0)
coordinate (N) at (0,\r)
coordinate (P) at (-\r,0)
coordinate (Q) at (0,-\r);
\end{scope}
\begin{scope}[canvas is xy plane at z=\h] 
\path coordinate (M') at (\r,0)
coordinate (N') at (0,\r)
coordinate (P') at (-\r,0)
coordinate (Q') at (0,-\r)
;
\end{scope}
\coordinate (S) at (0,0,\v);

\foreach \v/\position in {O/below,A/below, B/right,  C/below, D/left, S/above} {\draw[draw =black, fill=black] (\v) circle (1.5pt) node [\position=0.2mm] {$\v$};
}
\foreach \X in {A,B} \draw[thick] (\X) -- (S);
\foreach \X in {M,N,P,Q} \draw[dashed] (\X) -- (\X');
\draw[dashed] (A) -- (B) -- (C) -- (D) -- cycle 
(M) -- (N) -- (P) -- (Q) -- cycle
(M') -- (N') -- (P') -- (Q') -- cycle
(A) -- (C) (B) -- (D)
(S)--(O)  (S)-- (C);

\foreach \Y in {M, N, P, Q, M', N', P', Q'} 
\fill (\Y) circle[radius=1.1pt];

\begin{scope}[canvas is xy plane at z=0]
\draw[dashed]  (O) circle[radius=\r];
\end{scope}


\pgfmathsetmacro\cott{{cot(\th)}}
\pgfmathsetmacro\fraction{\R*\cott/\v}
\pgfmathsetmacro\fraction{\fraction<1 ? \fraction : 1}
\pgfmathsetmacro\angle{{acos(\fraction)}}

% % angles for transformed lines
\pgfmathsetmacro\PhiOne{180+(\az-90)+\angle}
\pgfmathsetmacro\PhiTwo{180+(\az-90)-\angle}

% % coordinates for transformed surface lines
\pgfmathsetmacro\sinPhiOne{{sin(\PhiOne)}}
\pgfmathsetmacro\cosPhiOne{{cos(\PhiOne)}}
\pgfmathsetmacro\sinPhiTwo{{sin(\PhiTwo)}}
\pgfmathsetmacro\cosPhiTwo{{cos(\PhiTwo)}}

% % angles for original surface lines
\pgfmathsetmacro\sinazp{{sin(\az-90)}}
\pgfmathsetmacro\cosazp{{cos(\az-90)}}
\pgfmathsetmacro\sinazm{{sin(90-\az)}}
\pgfmathsetmacro\cosazm{{cos(90-\az)}}

% % draw basis circle
\tdplotdrawarc[tdplot_main_coords,thick]{(O)}{\R}{\PhiOne}{360+\PhiTwo}{anchor=north}{}
\tdplotdrawarc[tdplot_main_coords,dashed]{(O)}{\R}{\PhiTwo}{\PhiOne}{anchor=north}{}

% % displaying tranformed surface of the cone (rotated)
\draw[thick] (0,0,\v) -- (\R*\cosPhiOne,\R*\sinPhiOne,0);
\draw[thick] (0,0,\v) -- (\R*\cosPhiTwo,\R*\sinPhiTwo,0);
 \end{tikzpicture}
  \end{document}
```
![ScreenHunter 884.png](/image?hash=9dad13f0be8384e527c39c510afcfa3cbf9476eacae4a3597f2c78341c0c548a)

How to  draw this picture by another way?
Top Answer
user 3.14159
This code recycles the `cone` pic from another answer. In order to decide whether the edges from a point `A`, `B`, `C`, or `D` to `S` are visible or hidden, we compute the normal on the cone at these points. The normal is given by the vector product of the tangent at the base circle and the respective edge. The tangent is simply given by the vector product of the point and `(0,0,1)`.
```
\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3dtools}
\tikzset{pics/3d/cone/.style={code={
	\tikzset{3d/cone/.cd,#1}
	\def\pv##1{\pgfkeysvalueof{/tikz/3d/cone/##1}}%
	\pgfmathsetmacro{\sdtip}{screendepth(0,0,\pv{h})}
	\pgfmathsetmacro{\aspectangle}{atan2(\sdtip,sqrt(\pv{h}*\pv{h}-\sdtip*\sdtip))}
	\path (0,0,\pv{h}) coordinate (-tip);
    \begin{scope}[x={(0,0,tan(\aspectangle))},y={($(0,0,0)!1cm!90:(0,0,\pv{h})$)}]
	 \pgfmathtruncatemacro{\itest}{abs(tan(\aspectangle)*\pv{r}/\pv{h})<1}
	 \ifnum\itest=1
	  \pgfmathsetmacro{\alphacrit}{acos(tan(\aspectangle)*\pv{r}/\pv{h})}
	  \ifdim\sdtip pt>0pt
	   \path[/tikz/3d/cone/hidden] (\alphacrit:\pv{r}) arc[start angle=\alphacrit,end
	   angle=-\alphacrit,radius=\pv{r}];
	   \path[/tikz/3d/cone/visible] (\alphacrit:\pv{r}) 
	   arc[start angle=\alphacrit,end  angle=360-\alphacrit,radius=\pv{r}]
	   -- (-tip) -- cycle;
	  \else
	   \path[/tikz/3d/cone/visible] circle[radius=\pv{r}];
	   \path[/tikz/3d/cone/visible] (\alphacrit:\pv{r}) 
	   -- (-tip) -- (360-\alphacrit:\pv{r});
	  \fi
	 \else
	  \path[/tikz/3d/cone/visible] circle[radius=\pv{r}];	 
	 \fi
    \end{scope}
	}},
	3d/cone/.cd,r/.initial=1,h/.initial=1,
	hidden/.style={draw,very thin,densely dashed},
	visible/.style=draw}
\begin{document}
\foreach \Angle in {5,15,...,355}
{\begin{tikzpicture}[visible/.style={draw,solid},
	hidden/.style={draw,very thin,densely dashed},
	declare function={
	R=5;% radius of the base of the cone
	v=2*R;% height
	a=3;
	b=4;
	r=0.5*sqrt(a*a+b*b);
	h=(R-r)*v/R;
	}]
\tdplotsetmaincoords{65}{\Angle}
\begin{scope}[tdplot_main_coords]
 \pic{3d/cone={r=R,h=v}};
 \path
 (0,0,0) coordinate (O)
 (R,0,0) coordinate (A)
 (0,R,0) coordinate (B)
 (-R,0,0) coordinate (C)
 (0,-R,0) coordinate (D)
 (r,0,0) coordinate (M)
 (0,r,0) coordinate (N)
 (-r,0,0) coordinate (P)
 (0,-r,0)  coordinate (Q)
 (r,0,h) coordinate (M')
 (0,r,h) coordinate (N')
 (-r,0,h) coordinate (P')
 (0,-r,h)  coordinate (Q')
 (0,0,v) coordinate (S);
 \draw[hidden] (A) -- (B) -- (C) -- (D) -- cycle (A) -- (C) (B) -- (D)
	 (M) -- (N) -- (P) -- (Q) -- cycle
 	(M') -- (N') -- (P') -- (Q') -- cycle
	foreach \X in {P,Q,M,N}	{(\X) -- (\X')}
	(O) circle[radius=r];
 \foreach \X in {A,B,C,D}
 {\pgfmathsetmacro{\myT}{TD("(0,0,1)x(\X)")}% tangent
  \pgfmathsetmacro{\myN}{TD("(\myT)x(S)-(\X)")}% normal
  \pgfmathtruncatemacro{\itest}{sign(screendepth(\myN))}
  \ifnum\itest=-1
   \draw[hidden] (\X) -- (S);
  \else
   \draw[visible] (\X) -- (S);
  \fi
 }
\end{scope}
\end{tikzpicture}}
\end{document}
```
![ani.gif](/image?hash=8ad0d67abf15e94c7fe29ffbdfd53d5e7728d14f15080ebf9b8f165a7f56c9fa)

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.