Parametric Sphere

parametric-sphere

Edit and compile if you like:

\documentclass{article}
%
% File name: sphere.tex
% Description: 
% A sphere of radius a is drawn 
% by means of its parametric equations 
% (in spherical coordinates.)
% 
% Date of creation: April, 23rd, 2022.
% Date of last modification: April, 23rd, 2022.
% Author: Efraín Soto Apolinar.
% https://www.aprendematematicas.org.mx/author/efrain-soto-apolinar/instructing-courses/
% Terms of use:
% According to TikZ.net
% https://creativecommons.org/licenses/by-nc-sa/4.0/
% 
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{math}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{1pt}
%
\begin{document}
	%
	\tdplotsetmaincoords{60}{130}
	\begin{tikzpicture}[tdplot_main_coords]
		% Parametric equations of the sphere
		\tikzmath{function equis(\r,\p,\t) {return \r * sin(\p r) * cos(\t r);};}
		\tikzmath{function ye(\r,\p,\t) {return \r * sin(\p r) * sin(\t r);};}
		\tikzmath{function zeta(\r,\p,\t) {return \r * cos(\p r);};}
		\pgfmathsetmacro{\tcero}{0.0}
		\pgfmathsetmacro{\phiInit}{0.0}
		\pgfmathsetmacro{\phiMid}{0.5*pi}
		\pgfmathsetmacro{\phiEnd}{pi}
		\pgfmathsetmacro{\thetaInit}{0.5*pi}
		\pgfmathsetmacro{\thetaMid}{1.85*pi}
		\pgfmathsetmacro{\thetaEnd}{2.5*pi}
		%
		\pgfmathsetmacro{\step}{0.02}
		\pgfmathsetmacro{\next}{\tcero+0.5*\step}
		\pgfmathsetmacro{\sig}{2.0*\step}
		\pgfmathsetmacro{\radio}{2.0}
		\pgfmathsetmacro{\sigP}{\phiMid+\step}
		\pgfmathsetmacro{\sigPp}{\sigP+\step}
		% Part of the z axis below the sphere
		\draw[dashed] (0,0,-1.25*\radio) -- (0,0,-\radio);
		% I start to draw the sphere from below
		% Part of the sphere under the plane z = 0
		\foreach \p in {\sigP,\sigPp,...,\phiEnd}{
			\draw[cyan,thick,opacity=0.25] plot[domain=\thetaInit:\thetaEnd,smooth,variable=\t] 
				({equis(\radio,\p,\t)},{ye(\radio,\p,\t)},{zeta(\radio,\p,\t)}); 
		}
		% Then I draw the part of the coordinate axis that is inside the sphere. 
		%%% Coordinate axis
		\draw[thick,->] (0,0,0) -- (1.5*\radio,0,0) node [below left] {\footnotesize$x$};
		\draw[dashed] (0,0,0) -- (-1.25*\radio,0,0);
		\draw[thick,->] (0,0,0) -- (0,1.5*\radio,0) node [right] {\footnotesize$y$};
		\draw[dashed] (0,0,0) -- (0,-1.25*\radio,0);
		\draw[dashed] (0,0,0) -- (0,0,-\radio);
		% As a reference, I draw a circumference at z = 0 (phi = pi / 2).
		\draw[red,opacity=0.25] plot[domain=0:2*pi,smooth,variable=\t] 
				({equis(\radio,\phiMid,\t)},{ye(\radio,\phiMid,\t)},{zeta(\radio,\phiMid,\t)});
		% As a reference, I draw a circumference at x = 0 (theta = 0).
		\draw[red,opacity=0.25] plot[domain=0:2*pi,smooth,variable=\t] 
				({equis(\radio,\t,0)},{ye(\radio,\t,0)},{zeta(\radio,\t,0)});
		% As a reference, I draw a circumference at y = 0 (theta = pi/2).
		\draw[red,opacity=0.25] plot[domain=0:2*pi,smooth,variable=\t] 
				({equis(\radio,\t,\thetaInit)},{ye(\radio,\t,\thetaInit)},{zeta(\radio,\t,\thetaInit)});
		%
		% Now I draw the part of the sphere that is behind the axis
		%
		\foreach \p in {\step,\sig,...,\phiMid}{
			\draw[cyan,thick,opacity=0.25] plot[domain=\thetaInit:\thetaMid,smooth,variable=\t] 
				({equis(\radio,\p,\t)},{ye(\radio,\p,\t)},{zeta(\radio,\p,\t)}); 
		}
		% Z axis that is inside the sphere
		% This part has to be in front of the rear part of the sphere
		\draw[thick] (0,0,0) -- (0,0,\radio);
		%
		% Sphere (the part that is in front of the z axis)
		%
		\foreach \p in {\step,\sig,...,\phiMid}{
			\draw[cyan,thick,opacity=0.25] plot[domain=\thetaMid:\thetaEnd,smooth,variable=\t] 
				({equis(\radio,\p,\t)},{ye(\radio,\p,\t)},{zeta(\radio,\p,\t)}); 
		}
		% Part of the z axis that is above the sphere
		\draw[thick,->] (0,0,\radio) -- (0,0,1.5*\radio) node [above] {\footnotesize$z$};
	\end{tikzpicture}
\end{document}


Click to download: parametric-sphere.texparametric-sphere.pdf
Open in Overleaf:
parametric-sphere.tex
See more on the author page of Efraín Soto Apolinar.

Leave a Reply

Your email address will not be published.