# How can I draw the triangle tangent to sphere?

I am trying to draw figure of the Problem 21 https://artofproblemsolving.com/wiki/index.php/2019_AMC_10A_Problems/Problem_21

A sphere with center $O$ has radius 6. A triangle with sides of length $15$, $15$, and $24$ is situated in space so that each of its sides are tangent to the sphere. I tried

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{60}{140}
\begin{tikzpicture}[tdplot_main_coords,scale=1/2]
\path
(0,0,0)  coordinate  (O);

\begin{scope}
\end{scope}
\end{tikzpicture}
\end{document}


How can I draw triangle?

This is a version that computes the points from the inputs with pgf methods. The output is much less spectacular than minthien_2016's nice answer, I do not (in this answer) distinguish between visible and hidden parts of the triangle and circle. The steps are:

1. compute the radius r of the incircle;
2. compute the height h of the plane containing the triangle;
3. compute the distances of the points where the incircle touches the triangle to the corners of the triangle and the angles of the triangle;
4. add the corners. One edge can be taken to be parallel to the x axis. So two corners are at z=h, y=-r and x at the respective touching distances. The third corner can then be reconstructed from the angles.

This is the result:

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{fpu}
\def\pgfmathsetmacroFPU#1#2{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}%
\begin{document}
\begin{tikzpicture}[scale=1/2,declare
function={a=15;b=15;c=24;R=6;}]
\path (0,0,0)  coordinate  (O);
\pgfmathsetmacroFPU{\saux}{(a+b+c)/2}
% compute height of circle
% compute angles of triangle
\pgfmathsetmacro{\alphaaux}{acos((b*b+c*c-a*a)/(2*b*c))}
\pgfmathsetmacro{\betaaux}{acos((a*a+c*c-b*b)/(2*a*c))}
\pgfmathsetmacro{\gammaaux}{acos((a*a+b*b-c*c)/(2*a*b))}
% compute distances from the corners of the triangle to the points where
% the triangle touches the circle
\pgfmathsetmacro{\touchA}{(b+c-a)/2}
\pgfmathsetmacro{\touchB}{(a+c-b)/2}
\pgfmathsetmacro{\touchC}{(a+b-c)/2}
\tdplotsetmaincoords{60}{140}
\begin{scope}[tdplot_main_coords]
\path (-\touchA,-\inradius,\haux) coordinate[label=right:$A$](A)
(\touchB,-\inradius,\haux) coordinate[label=left:$B$](B)
coordinate[label=right:$C$](C)
coordinate (TAC)
coordinate (TBC)
(0,0,\haux) coordinate (M);
\draw (A) -- (B) -- (C) -- cycle;
\begin{scope}[canvas is xy plane at z=\haux]
\end{scope}
\foreach \X in {A,B,C,M,TAB,TAC,TBC}
\end{scope}
\end{tikzpicture}
\end{document}


With some calculations, I have this code

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot-circleofsphere}
\usepackage{fouriernc}
\begin{document}
\tdplotsetmaincoords{60}{140}
\begin{tikzpicture}[tdplot_main_coords,scale=1/2,line join = round, line cap = round,declare function={R=6;r=4;h=2*sqrt(5); Angle=acos(r/R);}]
\path
(-3, -4,h) coordinate (A)
(12, -4,h) coordinate (B)
(-36/5, 52/5,h)  coordinate (C)
(0,0,h)  coordinate  (I)
(0,0,0)  coordinate  (O)
(0,-4,h)  coordinate  (H)
(-96/25, -28/25, h)  coordinate  (K)
;
\begin{scope}
\fill[ball color=gray!10,tdplot_screen_coords] (O)  circle (R);
\end{scope}
\begin{scope}[shift={(O)}]
\tdplotCsDrawLatCircle[blue, thick]{R}{{Angle}}
\end{scope}
\draw[thick] (A) -- (B) -- (C) -- cycle;
\draw[dashed] (O) -- (I) -- (H) -- cycle (I) -- (K) -- (O);
\foreach \p in {A,B,C,I,O,H,K}
\draw[fill=black] (\p) circle (2.5pt);
\foreach \p/\g in {A/90,B/-90,C/-90,I/90,O/-90,H/90,K/90}
\path (\p)+(\g:6mm) node{$\p$};
\end{tikzpicture}
\end{document}


Asymptote version with removed top of the sphere, suitable for general triangle:

//
// file tri-tan-sphere.asy
// run "asy -f png -render=4 tri-tan-sphere.asy"
// to get tri-tan-sphere.png
//
import graph3; size(200,0);
currentprojection=orthographic(camera=(-20,-25,27),up=(0.2,0.9,1));
triple f(pair t){return (cos(t.y)*cos(t.x),cos(t.y)*sin(t.x),sin(t.y));}
real R=6, a=15, b=20, c=24,
rho=(a+b+c)/2,
r=1/4*sqrt(4*a^2*b^2-(a^2+b^2-c^2)^2)/rho,
alpha=2*aTan(r/(rho-a)),
h=sqrt(R^2-r^2);
triple O,A,B,C,I,At,Bt,Ct;
A=O; B=(c,0,0); C=(b*Cos(alpha),b*Sin(alpha),0);
I=(a*A+b*B+c*C)/2/rho;
transform3 t=shift(Z*h-I);
A=t*A; B=t*B; C=t*C; I=t*I;
guide3 gt=A--B--C--cycle;
At=B+(rho-b)/a*(C-B); Bt=C+(rho-c)/b*(A-C); Ct=A+(rho-a)/c*(B-A);
surface s=surface(gt);
draw(s,lightyellow+opacity(0.5));
draw(circle(I,r)^^gt,deepblue);
triple[] P={O,A,B,C,I,At,Bt,Ct,};
string[] L={"O","A","B","C","I","A_t","B_t","C_t",};
triple[] T={Z+Y,A-I,B-I,C-I,Z+Y,I-A,I-B,I-C,};
for(int i=0;i<P.length;++i){dot(P[i]); label("$"+L[i]+"$",P[i],2unit(T[i]));}
surface sc=scale3(R)*surface(f,(0,-pi/2),(2pi,acos(r/R)),Spline);
draw(sc,lightgray,meshpen=nullpen,render(merge=true));


Alternatively, command asy -f html tri-tan-sphere.asy generates tri-tan-sphere.html with interactive 3D vector WebGL graphics.