# TikZ: Sphere that contains some points

There should be a sphere that contains the points A,B,C,D and S.

Does somebody know how to draw the picture with sphere correctly?

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{angles, quotes, babel}
\usepackage{amsmath, amssymb}
\begin{document}

\begin{tikzpicture}[x=1cm, y=1cm, z={(4mm,3mm)}, scale=1.5, font=\small]
\pgfmathsetmacro\a{2}
\pgfmathsetmacro\b{2.5}
\pgfmathsetmacro\c{3}

\pgfmathsetmacro{\r}{0.5*sqrt(\a^2+\b^2+\c^2)} %

\pgfmathsetmacro{\AC}{sqrt(\a^2+\b^2)} %
\pgfmathsetmacro{\PA}{0.5*\AC} %

\pgfmathsetmacro{\h}{sqrt(\r^2-\PA^2)} %

% Pyramid
\draw (0,0,0) coordinate[label=below:$A$] (A)
--(\a,0,0) node[midway, below]{$a$} coordinate[label=below:$B$] (B)
--(\a,0,\b) node[midway,right=2mm]{$b$} coordinate[label=$C$] (C);

\draw[densely dashed] (C)--(0,0,\b) coordinate[label=left:$D$] (D)--(A);
\draw (A)--(0,\c,0) node[midway, left]{$c$} coordinate[label=$S$] (S);

% Diagonals
\draw (A)--(C);
\draw ($(A)!0.5!(C)$) coordinate coordinate[label=below:$P$]  (P) -- ($(S)!0.5!(C)$) node[midway, left]{$h$} coordinate[label=$M$] (M);
\draw (D)--(B);

% Point M
\draw (S)--(M) node[midway, above]{$r$} -- (C) node[midway, above]{$r$};

\draw[densely dashed] (D) -- (M);
\foreach \P in {A,B,C} \draw (M) -- (\P);

\node[anchor=north west, align=left, yshift=-7mm] at (A){$\begin{array}{l} a = \a ,~ b = \b,~ c = \c \\ |AC| = \sqrt{a^2+b^2} = \AC \\ |PA| = 0.5 |AC| = \PA \\ h = \sqrt{r^2- |PA|^2} = \h \\ r = 0.5 \sqrt{a^2+b^2+c^2} = \r \end{array}$
};

\end{tikzpicture}

\end{document}

• I recommend Asymotote for real 3D. Commented Jul 21, 2019 at 14:02
• Commented Jul 21, 2019 at 14:58

In your coordinate system it would be a mess because you do not use orthographic projections and the sphere will look like an egg. If you use orthographic projections, a sphere of radius r is just as circle of the same radius in screen coordinates.

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{60}{30}

\begin{tikzpicture}[tdplot_main_coords, font=\small]
\pgfmathsetmacro\a{2}
\pgfmathsetmacro\b{2.5}
\pgfmathsetmacro\c{3}
% Pyramid
\draw (0,0,0) coordinate[label=below:$A$] (A)
--(\a,0,0) node[midway, below]{$a$} coordinate[label=below:$B$] (B)
--(\a,\b,0) node[midway,right=2mm]{$b$} coordinate[label=$C$] (C);

\draw[densely dashed] (C)--(0,\b,0) coordinate[label=left:$D$] (D)--(A);
\draw (A)--(0,0,\c) node[midway, left]{$c$} coordinate[label=$S$] (S);

\path (\a/2,\b/2,\c/2) coordinate[label=$M$](M);
% Diagonals
\draw (A)--(C);
\draw ($(A)!0.5!(C)$) coordinate coordinate[label=below:$P$]  (P) --  node[midway, left]{$h$} (M);
\draw (D)--(B);

% Point M
\draw (S)--(M) node[midway, above]{$r$} -- (C) node[midway, above]{$r$};

\draw[densely dashed] (D) -- (M);
\foreach \P in {A,B,C} \draw (M) -- (\P);

\shade[tdplot_screen_coords,ball color = gray!40, opacity = 0.4]

• Add the line \draw (P) circle[radius={sqrt(\a*\a+\b*\b)/2}]; look better. Commented Jul 23, 2019 at 6:49