Anonymous 1123
I am trying to draw [this quesion](https://tex.stackexchange.com/questions/539956/how-can-i-draw-sphere-cut-by-plane-by-using-tikz-3dplot) by using 3dtools with syntax draw ordered paths
I tried

\documentclass[border=2mm,12pt,tikz]{standalone}
\usetikzlibrary{3dtools}%
\usepackage{fouriernc}
\begin{document}

\begin{tikzpicture}[3d/install view={phi=30,theta=70},scale=1,line join = round, line cap = round,declare function={R=5;r=4;h=sqrt(R^2 - r^2);myx= 2; myy=sqrt(R*R-h*h- myx*myx); k=-1; Angle=k*acos(r/R);}]
\path
(0,0,0) coordinate (O)
(0,0,k*h) coordinate (H)
(myx,myy,k*h) coordinate (M)
;
\path [3d/screen coords,save named path=sph] (O) circle[radius=R];

\begin{scope} [canvas is xy plane at z=k*h]
\path[save named path=rec] (-R,-R) rectangle (R,R);
\draw (-R,-R) rectangle (R,R);
\end{scope}

\path[save named path=cir] pic{3d/circle on sphere={R=R,C={(O)}, P={(H)}}};

\tikzset{3d/draw ordered paths={rec,sph}};
\foreach \p in {H,M,O}
{\draw[fill=black] (\p) circle (1.5pt);}
\foreach \p/\g in {M/90,O/-135,H/30}
{\path (\p)+(\g:3mm) node{$\p$}; }
\draw[3d/hidden] (O) -- (H) -- (M) --cycle;
\end{tikzpicture}
\end{document}


![ScreenHunter 134.png](/image?hash=707f33276b949e6f263302927b4cd924152bf2610b8828d357b1fa9cbd408b44)

How can I get correct result?
user 3.14159
The draw ordered paths does not do magic. In particular, if the paths do not intersect in 2D you need to add more ingredients. However, you can combine existing paths to auxiliary paths that can be used in draw ordered paths. We do this here with the visible part of the intersection circle and the boundaries of the plane in the foreground. (There is one ambiguity whether one has to use the original or reversed path, and possibly more, letting those decide by LaTeX is painful so I just made an ad hoc choice.)

\documentclass[border=2mm,12pt,tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing,3dtools}%
\usepackage{fouriernc}
\begin{document}
\begin{tikzpicture}[3d/install view={phi=30,theta=70},
line join = round, line cap = round,
declare function={R=5;r=4;h=sqrt(R^2 - r^2);myx= 2;
myy=sqrt(R*R-h*h- myx*myx); k=-1; Angle=k*acos(r/R);},
manage line/.code={
\path[save named path/.expanded=l\LastX#1] (P\LastX) -- (P#1);
\pgfmathsetmacro{\myP}{TD("0.5*(P\LastX)+0.5*(P#1)")}
\pgfmathtruncatemacro{\itest}{screendepth(\myP)>0?1:0}
\ifnum\itest=0
\edef\lstp{l\LastX#1,\lstp}
\else
\edef\lstp{\lstp,l\LastX#1}
\edef\iv{\the\numexpr\iv+1}
\path[store path=l\iv] (P\LastX) -- (P#1);
\fi
\edef\LastX{#1}
}]
\path
(0,0,0) coordinate (O)
(0,0,k*h) coordinate (H)
(myx,myy,k*h) coordinate (M)
(-R,-R,k*h) coordinate (P1)
(-R,R,k*h) coordinate (P2)
(R,R,k*h) coordinate (P3)
(R,-R,k*h) coordinate (P4) ;
\draw[3d/hidden] (O) -- (H) -- (M) --cycle;
\path[3d/screen coords,save named path=sph] (O) circle[radius=R];
% deal with the intersection circle
\path[3d/visible/.append style={postaction={store path=csph}}]
pic{3d/circle on sphere={R=R,C={(O)}, P={(H)}}};
\edef\lstp{sph,aux}
% deal with the lines: we add them before or after the circles
% depending on the screendepth
\edef\iv{0}
\edef\LastX{1}
\tikzset{manage line/.list={4,3,2,1}}
% auxiliary path in the plane composed of the visible part of the
% circle and the boundaries of the plane in the foreground
\path[save named path=aux,stored path/restore reversed path=csph,
stored path/append path=l1,stored path/append path=l2] -- cycle;
\tikzset{3d/ordered paths/.cd,aux/.style={draw=none}}
\tikzset{3d/draw ordered paths/.expanded=\lstp};
\foreach \p/\g in {M/90,O/-135,H/30}
{\draw[fill=black] (\p) circle[radius=1.5pt]  +(\g:3mm) node{$\p$}; }
\end{tikzpicture}
\end{document}

![Screen Shot 2021-05-07 at 9.43.23 AM.png](/image?hash=14e250dbd10934fd66403332b1986f38cb93a26abee3156dc9c0402c6409963c)

When you want to have a fully rotatable version one needs to painfully replace the ad hoc choices by some prescription that LaTeX can understand.

\documentclass[border=2mm,12pt,tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing,3dtools}%
\usepackage{fouriernc}
\begin{document}
\tikzset{manage line/.code={
\path[save named path/.expanded=l\LastX#1] (P\LastX) -- (P#1);
\pgfmathsetmacro{\myP}{TD("0.5*(P\LastX)+0.5*(P#1)-(H)")}
\pgfmathtruncatemacro{\itest}{screendepth(\myP)>0?1:0}
\ifnum\itest=0
\edef\lstp{l\LastX#1,\lstp}
\else
\ifnum\iv=1\relax
\ifnum\LastX=\lastvis
\edef\lstafter{\lstafter,l\LastX#1}
\edef\iflip{0}
\else
\edef\lstafter{l\LastX#1,\lstafter}
\edef\iflip{1}
\fi
\else
\edef\lstafter{l\LastX#1}
\fi
\edef\lastvis{#1}
\edef\iv{\the\numexpr\iv+1}
\path[store path=l\iv] (P\LastX) -- (P#1);
\fi
\edef\LastX{#1}
}}
\foreach \Angle in {5,15,...,355}
{\begin{tikzpicture}[3d/install view={phi=\Angle,theta=70},
line join = round, line cap = round,same bounding box=A,
declare function={R=5;r=4;h=sqrt(R^2 - r^2);myx= 2;
myy=sqrt(R*R-h*h- myx*myx); k=-1; Angle=k*acos(r/R);}]
\path
(0,0,0) coordinate (O)
(0,0,k*h) coordinate (H)
(myx,myy,k*h) coordinate (M)
(-R,-R,k*h) coordinate (P1)
(-R,R,k*h) coordinate (P2)
(R,R,k*h) coordinate (P3)
(R,-R,k*h) coordinate (P4) ;
\draw[3d/hidden] (O) -- (H) -- (M) --cycle;
\path[3d/screen coords,save named path=sph] (O) circle[radius=R];
% deal with the intersection circle
\path[3d/visible/.append style={postaction={store path=csph}}]
pic{3d/circle on sphere={R=R,C={(O)}, P={(H)}}};
\edef\lstp{sph,aux}
% deal with the lines: we add them before or after the circles
% depending on the screendepth
\edef\iv{0}
\edef\LastX{1}
\tikzset{manage line/.list={4,3,2,1}}
\edef\lstp{\lstp,\lstafter}
%  % auxiliary path in the plane composed of the visible part of the
%  % circle and the boundaries of the plane in the foreground
\ifnum\iflip=1
\path[save named path=aux,stored path/restore reversed path=csph,
stored path/append path=l2,stored path/append path=l1] -- cycle;
\else
\path[save named path=aux,stored path/restore reversed path=csph,
stored path/append path=l1,stored path/append path=l2] -- cycle;
\fi
\tikzset{3d/ordered paths/.cd,aux/.style={draw=none}}
\tikzset{3d/draw ordered paths/.expanded=\lstp};
{\draw[fill=black] (\p) circle[radius=1.5pt]  +(\g:3mm) node{$\p$}; }

![ani.gif](/image?hash=867103c72fe4d2de6b3319d9f495afe412bd98d61369b00d4bbf6a29f184980c)