add tag
Anonymous 1123
I see this code from [here](https://www.overleaf.com/project/60b3af145e71b668aa3580e5) 

```
\documentclass[tikz,border=5mm,convert={outfile=\jobname.png}]{standalone}
\usepackage{tikz-3dplot-circleofsphere}
\usetikzlibrary{calc,angles}
%==============
\everymath{\displaystyle}
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%

 \tdplotsetmaincoords{70}{110}
 \begin{tikzpicture}[tdplot_main_coords,scale=3,font=\footnotesize,>=stealth]
 \def\r{2}
 \pgfmathsetmacro{\a}{sqrt(3)}
 \pgfmathsetmacro{\b}{sqrt(2)}
 \begin{scope}[thin,black!50]
 \draw[dashed] (-1.3*\r,0,0) -- (\r,0,0)
 (0,-1.05*\r,0) -- (0,\r,0)
 (0,0,0) -- (0,0,\r);
 \draw[->] (\r,0,0) -- (1.3*\r,0,0) node[anchor=north east] {$x$};
 \draw[->] (0,\r,0) -- (0,1.3*\r,0) node[anchor=north] {$y$};
 \draw[->] (0,0,\r) -- (0,0,1.3*\r) node[anchor=south east] {$z$};
 \draw[tdplot_screen_coords] (\r,0,0) arc (0:180:\r);
 \tdplotCsDrawLatCircle{\r}{0}
 \end{scope}
 \tdplotCsDrawCircle{\r}{0}{60}{60}
 \tdplotCsDrawCircle{\r}{70.5}{60}{60}
 \tdplotCsDrawLatCircle{\r}{60}
 \tdplotCsDrawCircle{\r}{35.4}{35.3}{84.9}
 \path (0,0,0) coordinate (O)
 (0,0,\a) coordinate (O_1)
 (1.5,0,\a/2) coordinate (O_2)
 (0.5,\b,\a/2) coordinate (O_3)
 (0.94,{2/3},1.63) coordinate (O_4)
 (1,0,\a) coordinate (M)
 (1.09,0.58,1.57) coordinate (K)
 (barycentric cs:O_1=1,O_2=1,O_3=1) coordinate (L)
 ($(L)-(O_4)+(K)$) coordinate (N);
 \draw[dashed] (M)--(O)--(O_1)--(M)--(O_2)--(O_1) (O)--(O_2)--(K)--(O_4)--(O) (O_2)--(L) (K)--(N);
 \draw[fill=black] (O) circle (0.5pt) node[below right]{$O$}
 (O_1) circle (0.5pt) node[above left]{$O_1$}
 (M) circle (0.5pt) node[above]{ $M$}
 (O_2) circle (0.5pt) node[left]{$O_2$}
 (O_4) circle (0.5pt) node[right=0.05cm]{\tiny $O_4$}
 (K) circle (0.5pt) node[left]{$K$}
 (N) circle (0.5pt) node[below]{ $N$}
 (L) circle (0.5pt) node[below right]{ $L$};
 \end{tikzpicture}
%=============
\end{document}

```

![ScreenHunter 205.png](/image?hash=0898a792a4b6ebc28e3486ec557ff57212796e4504225e8fa8bf1b02263c638d)

I tried to draw it with `3dtools`. 
```
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
	
	\begin{tikzpicture}[3d/install view={phi=110,theta=70},line cap=butt,line join=round,c/.style={circle,fill,inner sep=1pt},declare function={rs=2;rc=1;d=sqrt(rs*rs-rc*rc);a=sqrt(2);}] 
		\path
		(0,0,0) coordinate (O)
		(0,0,d) coordinate (O_1)
		(1.5,0,d/2) coordinate (O_2)
		(0.5,a,d/2) coordinate (O_3)
		(0.94,{2/3},1.63) coordinate (O_4);
		\draw[3d/screen coords] (O) circle[radius=rs];
		\path pic{3d/circle on sphere={R=rs, P={(0,0,d)}}}; 
		\path pic{3d/circle on sphere={R=rs,P={(O_2)}}};
		\path pic{3d/circle on sphere={R=rs, P={(O_3)}}};
		\path pic{3d/circle on sphere={R=rs, P={(O_4)}}};
		\path pic{3d/circle on sphere={R=rs, P={(O)}}};
		
		%\path foreach \p/\g in {O/-90,O_1/90,O_2/0,O_3/0}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};
	\end{tikzpicture}
\end{document}      
```
![ScreenHunter 204.png](/image?hash=e602aa4d84ca776dc4794f9c6c5285ac1f6cc22f37157bd4228fb991356d9743)

My questions:
1. The circle with center `O_4` is incorrect. How to correct it?
2. How can I find coordinates of the points `O_2, O_3, O_4` by `3dtools`?
3. Can I solve the general problem?

Top Answer
user 3.14159
The reason why the circle is incorrect is that the code uses rounded values. On the other hand, the radius `r` of the circle is computed via
```
r=sqrt(R^2-d^2) ,
```
where `R` is the radius of the sphere and `d` is the distance of the center of the circle from the center of the sphere. If we compute the sensitivity of `r` on `d`, we need to compute the derivative of `r` w.r.t. `d` (which, among other things, also illustrates why it makes a lot of sense to distinguish differential `d`s typographically from variables `d`), we see that
```
r'=-d/sqrt(R^2-d^2) .
```
That is, if `d` is very close to `R`, then a tiny error in `d` can give you a very wrong radius. 

This is confirmed with `3dtools`, with which it is rather easy to construct the circle. All we need to do is to compute two angles, `alpha` and `beta`, and to install local coordinate systems on the circles. The details are in the comments of the following code. 
```
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}

\begin{tikzpicture}[3d/install view={phi=110,theta=70},
	line cap=butt,line join=round,c/.style={circle,fill,inner sep=1pt},
	declare function={rs=2;rc=1;d=sqrt(rs*rs-rc*rc);a=sqrt(2);
	    % alpha is just the latitude angle of O_2 and O_3
		alpha=asin(rc/rs);
		% the beta angle emerges from the requirement that O--O_2 and 
		% O--O_3 enclose an angle of 2*alpha
		beta=acos((cos(2*alpha)-pow(sin(alpha),2))/(pow(cos(alpha),2)));}] 
 \path
  (0,0,0) coordinate (O)
  (0,0,d) coordinate (O_1)
  ({d*cos(alpha)},0,{d*sin(alpha)}) coordinate (O_2)
  ({d*cos(alpha)*cos(beta)},{d*cos(alpha)*sin(beta)},{d*sin(alpha)}) coordinate (O_3);
 % basis vectors for circle 2 
 \pgfmathsetmacro{\myexb}{TDunit("(0,0,1)x(O_2)")}
 \pgfmathsetmacro{\myeyb}{TDunit("(O_2)x(\myexb)")}
 % basis vectors for circle 3 
 \pgfmathsetmacro{\myexc}{TDunit("(0,0,1)x(O_3)")}
 \pgfmathsetmacro{\myeyc}{TDunit("(O_3)x(\myexc)")}
 \path[3d coordinate/.list={%
 	{(T_1)=(O_1)+[rc*cos(beta/2)]*(1,0,0)+[rc*sin(beta/2)]*(0,1,0)},
 	{(T_2)=(O_2)+[rc*cos(90-beta/2)]*(\myexb)+[rc*sin(90-beta/2)]*(\myeyb)},
	{(T_3)=(O_3)+[rc*cos(90+beta/2)]*(\myexc)+[rc*sin(90+beta/2)]*(\myeyc)}}]
  (O)pic[draw=none]{3d circle through 3 points={A={(T_1)},B={(T_2)},C={(T_3)},
  center name=O_4}};
 \draw[3d/screen coords] (O) circle[radius=rs];
 \pgfmathsetmacro{\myM}{TD("(O_4)")}
 \pgfmathsetmacro{\myr}{tddistance("(O_4)","(T_1)")}
 \typeout{O_4=(\myM), r=\myr}
 \path foreach \X in {1,2,3,4} 
 { (O)pic{3d/circle on sphere={R=rs,C={(O)},P={(O_\X)}}}}; 
% 
 \path foreach \p/\g in {O/-90,O_1/90,O_2/0,O_3/0,T_1/90,T_2/-135,T_3/-45}{
 	(\p)node[c,label={[font=\tiny,label distance=0pt,inner sep=0pt]\g:{$\p$}}]{}};
\end{tikzpicture}
\end{document}   
```
![Screen Shot 2021-05-31 at 6.50.35 PM.png](/image?hash=8848e89a35c1a671cbd2478a62f7b26135be55df15ab95b7da2939a8bdfad64d)
This code gets `O_4=(0.93881,0.66382,1.62599)`. The corresponding distances are 
```
1.99623 for your input and 1.99145 for the 3dtools result,
```
and the resulting radii are
```
0.122701 for your input and 0.184757 for the 3dtools result,
```
respectively. As you can see, an error of about 0.25% at the level of the distance translates into a 50% error at the level of the radius. This is why the circle produced in the code you posted is incorrect.

`3dtools` uses `fpu` to counter such effects. There is no guarantee that this is always sufficient, but at least in this case it is. (It wouldn't be too difficult to use `xfp` instead because all the computations in the package yield dimensionless results. Replacing `fpu` by `xfp` blindly, however, will lead to major problems that manifest only in certain scenarios, which are, however, widely used.)

The above code is already general, i.e. can be used for various `rc`. (Of course, if the radius `rc` becomes too large, three circles will no longer fit on the sphere while touching each other only once, and then the code does not work any more.)
```
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
\foreach \rc in {1,1.1,...,3.4,3.3,3.2,...,1.1}
{\begin{tikzpicture}[3d/install view={phi=110,theta=70},
	line cap=butt,line join=round,c/.style={circle,fill,inner sep=1pt},
	declare function={rs=4;rc=\rc;d=sqrt(rs*rs-rc*rc);
	    % alpha is just the latitude angle of O_2 and O_3
		alpha=90-2*atan2(rc,d);
		% the beta angle emerges from the requirement that O--O_2 and 
		% O--O_3 enclose an angle of 2*atan2(rc,d)
		beta=acos((cos(2*atan2(rc,d))-pow(sin(alpha),2))/(pow(cos(alpha),2)));
		}] 
 \path
  (0,0,0) coordinate (O)
  (0,0,d) coordinate (O_1)
  ({d*cos(alpha)},0,{d*sin(alpha)}) coordinate (O_2)
  ({d*cos(alpha)*cos(beta)},{d*cos(alpha)*sin(beta)},{d*sin(alpha)}) coordinate (O_3)
  ;
 % basis vectors for circle 2 
 \pgfmathsetmacro{\myexb}{TDunit("(0,0,1)x(O_2)")}
 \pgfmathsetmacro{\myeyb}{TDunit("(O_2)x(\myexb)")}
 % basis vectors for circle 3 
 \pgfmathsetmacro{\myexc}{TDunit("(0,0,1)x(O_3)")}
 \pgfmathsetmacro{\myeyc}{TDunit("(O_3)x(\myexc)")}
 \path[3d coordinate/.list={%
 	{(T_1)=(O_1)+[rc*cos(beta/2)]*(1,0,0)+[rc*sin(beta/2)]*(0,1,0)},
 	{(T_2)=(O_2)+[rc*cos(90-beta/2)]*(\myexb)+[rc*sin(90-beta/2)]*(\myeyb)},
	{(T_3)=(O_3)+[rc*cos(90+beta/2)]*(\myexc)+[rc*sin(90+beta/2)]*(\myeyc)}}]
  (O)pic[draw=none]{3d circle through 3 points={A={(T_1)},B={(T_2)},C={(T_3)},
  	center name=O_4}};
 \draw[3d/screen coords] (O) circle[radius=rs];
 \path foreach \X in {1,2,3,4} 
 { (O)pic{3d/circle on sphere={R=rs,C={(O)},P={(O_\X)}}}}; 
\end{tikzpicture}}
\end{document}  
```
![ani.gif](/image?hash=42edbe15953e251665854942d3632eeca5e3aacbe519b7b14a17332de9188fbc)

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.