# 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 last modification: April, 23rd, 2022.
% Author: Efraín Soto Apolinar.
% https://www.aprendematematicas.org.mx/author/efrain-soto-apolinar/instructing-courses/
% According to TikZ.net
%
\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{\sigP}{\phiMid+\step}
\pgfmathsetmacro{\sigPp}{\sigP+\step}
% Part of the z axis below the sphere
% 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]
}
% 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[thick,->] (0,0,0) -- (0,1.5*\radio,0) node [right] {\footnotesize$y$};
% 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]
% 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]
% 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]
%
% 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]
}
% Z axis that is inside the sphere
% This part has to be in front of the rear part of the sphere
%
% 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]
\draw[thick,->] (0,0,\radio) -- (0,0,1.5*\radio) node [above] {\footnotesize$z$};