TikZ-PGF – Drawing a Cube Within a Sphere Using TikZ

tikz-pgf

I would like to construct a diagram of a cube sitting inside a sphere. I can draw them both separately, but need some idea of how to construct the diagram such that the cube sits exactly within the sphere, the relationship between the side of the cube and the radius of the sphere would then be r=sqrt(3)*s/2, where r is the radius of the sphere holding the cube of side length s.

@Torbjørn T. @hugovdberg – Thanks for your suggestion!
I tried the following code, borrowing parts from segments available on the web. However, my sphere currently does not sit correctly at the same position of the cube. Also, I have the orientation of the sphere in error.

\documentclass[11pt]{scrartcl} 
\PassOptionsToPackage{dvipsnames,svgnames}{xcolor}        
\usepackage{xkeyval,tkz-base}
\usetikzlibrary{arrows,calc}

\usepackage{tikz}



\begin{document}

\begin{tikzpicture}
    [cube/.style={very thick,black},
        grid/.style={very thin,gray},
        axis/.style={->,blue,thick}]

%draw a grid in the x-y plane
\foreach \x in {-0.5,0,...,2.5}
    \foreach \y in {-0.5,0,...,2.5}
    {
        \draw[grid] (\x,-0.5) -- (\x,2.5);
        \draw[grid] (-0.5,\y) -- (2.5,\y);
    }

%draw the axes
\draw[axis] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
\draw[axis] (0,0,0) -- (0,3,0) node[anchor=west]{$y$};
\draw[axis] (0,0,0) -- (0,0,3) node[anchor=west]{$z$};

%draw the top and bottom of the cube
\draw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
\draw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;

%draw the edges of the cube
\draw[cube] (0,0,0) -- (0,0,2);
\draw[cube] (0,2,0) -- (0,2,2);
\draw[cube] (2,0,0) -- (2,0,2);
\draw[cube] (2,2,0) -- (2,2,2);



\foreach \t in {0,10,...,180}
    {\draw[gray] ({2*cos(\t)},{2*sin(\t)},0)
         \foreach \rho in {5,10,...,360}
             {--({2*cos(\t)*cos(\rho)},{2*sin(\t)*cos(\rho)},
         {2*sin(\rho)})}--cycle;
    }
\foreach \t in {-90,-85,...,90}% parallels
        {\draw[gray] ({2*cos(\t)},0,{2*sin(\t)})
     \foreach \rho in {5,10,...,360}
     {--({2*cos(\t)*cos(\rho)},{2*cos(\t)*sin(\rho)},
             {2*sin(\t)})}--cycle;
    } 

\end{tikzpicture}

\end{document}

Best Answer

You need to shift the cube to be centerd about the origin. The easiest way I can see to do that with your existing code is to apply a scope:

enter image description here

Code:

\documentclass[11pt]{scrartcl} 
\PassOptionsToPackage{dvipsnames,svgnames}{xcolor}        
\usepackage{xkeyval,tkz-base}
\usetikzlibrary{arrows,calc}

\usepackage{tikz}



\begin{document}

\begin{tikzpicture}
    [cube/.style={very thick,black},
        grid/.style={very thin,gray},
        axis/.style={->,blue,thick}]

%%draw a grid in the x-y plane
%\foreach \x in {-0.5,0,...,2.5}
%    \foreach \y in {-0.5,0,...,2.5}
%    {
%        \draw[grid] (\x,-0.5) -- (\x,2.5);
%        \draw[grid] (-0.5,\y) -- (2.5,\y);
%    }


%draw the axes
\draw[axis] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
\draw[axis] (0,0,0) -- (0,3,0) node[anchor=west]{$y$};
\draw[axis] (0,0,0) -- (0,0,5) node[anchor=west]{$z$};

\begin{scope}[shift={(-1,-1,-1)}]
    %draw the top and bottom of the cube
    \draw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
    \draw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
    
    %draw the edges of the cube
    \draw[cube] (0,0,0) -- (0,0,2);
    \draw[cube] (0,2,0) -- (0,2,2);
    \draw[cube] (2,0,0) -- (2,0,2);
    \draw[cube] (2,2,0) -- (2,2,2);
\end{scope}


\foreach \t in {0,10,...,180}
    {\draw[gray] ({2*cos(\t)},{2*sin(\t)},0)
         \foreach \rho in {5,10,...,360}
             {--({2*cos(\t)*cos(\rho)},{2*sin(\t)*cos(\rho)},
         {2*sin(\rho)})}--cycle;
    }
\foreach \t in {-90,-85,...,90}% parallels
        {\draw[gray] ({2*cos(\t)},0,{2*sin(\t)})
     \foreach \rho in {5,10,...,360}
     {--({2*cos(\t)*cos(\rho)},{2*cos(\t)*sin(\rho)},
             {2*sin(\t)})}--cycle;
    } 

\end{tikzpicture}

\end{document}