8

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}
4
  • Information to other users: This question has also been asked over at LaTeX Community
    – Johannes_B
    Mar 13, 2014 at 7:08
  • Yes @Johannes_B, I am looking for someone who can provide me with an answer. The LaTeX community is very sparse, more so than here. Mar 13, 2014 at 7:10
  • 4
    If you have some code drawing a sphere and a cube, perhaps you could add that to your question, in the form a minimal working example. Easier to help if we have a starting point I think. Mar 13, 2014 at 12:37
  • 3
    Some hints to get you started (since you also only gives us some hints ;) ), using pgfplots you can easily draw a 3d axis system, take a look at the manual how to do that. Inside the axis you can just use TikZ as you can outside, and you can use the axis cs coordinate system to position your cube and sphere inside the axis. If you need any help on the exact details, please post a minimal working example as suggested by Torbjørn.
    – hugovdberg
    Mar 13, 2014 at 16:31

4 Answers 4

6

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}
2
  • Thanks Peter, I had not heard of scope. How would you change the orientation of the sphere such that it stands parallel to the y-axis? Mar 14, 2014 at 1:24
  • @stars83clouds: In the \foreach the coordinates are the (x,y,z) so just swapping the y axis with the appropriate one (I think the z) should do it. You should try commenting out portions of the code and see what each piece does. Mar 14, 2014 at 1:28
3

enter image description hereI managed to eventually produce the figure that I was seeking. The cube now sits inside the sphere and each have a common centre at (1,1,1).

\documentclass[11pt]{standalone}

\PassOptionsToPackage{dvipsnames,svgnames}{xcolor}     
\usepackage{tikz,xkeyval,tkz-base}
\usetikzlibrary{arrows,calc}


\tikzset{
    MyPersp/.style={scale=1.8,x={(-0.8cm,-0.4cm)},y={(0.8cm,-0.4cm)},
    z={(0cm,1cm)}},
%  MyPersp/.style={scale=1.5,x={(0cm,0cm)},y={(1cm,0cm)},
%    z={(0cm,1cm)}}, % uncomment the two lines to get a lateral view
   MyPoints/.style={fill=white,draw=black,thick}
    }



\begin{document}

\begin{tikzpicture}[MyPersp,font=\large]


\draw[thick,->] (0,0,0) -- (3.0,0,0) node[anchor=north east]{$x, LOS$};
\draw[thick,->] (0,0,0) -- (0,3.0,0) node[anchor=north west]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,3.0) node[anchor=south]{$z$};




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




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

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






\foreach \t in {0,15,...,165}% meridians
    {\draw[gray] ({1.73*cos(\t)+1.0},{1.73*sin(\t)+1.0},1.0)
        \foreach \rho in {5,10,...,360}
            {--({1.73*cos(\t)*cos(\rho)+1.0},
  {1.73*sin(\t)*cos(\rho)+1.0},{1.73*sin(\rho)+1.0})}--cycle;
    }
\foreach \t in {-75,-60,...,75}% parallels
   {\draw[gray] ({1.73*cos(\t)+1.0},1.0,{1.73*sin(\t)+1.0})
        \foreach \rho in {5,10,...,360}
           {--({1.73*cos(\t)*cos(\rho)+1.0},   
{1.73*cos(\t)*sin(\rho)+1.0},{1.73*sin(\t)+1.0})}--cycle;
   }        


\end{tikzpicture}

\end{document}
3

Another answer using pgfplots, this allows for easy changes in viewpoint, further more, radius and offset in 3 dimensions can be easily set. I must say, the calculation of the corners of the cube can probably be optimized, but using the same 3 macros for every coordinate results in all coordinates humped together on the last position.

\documentclass{scrartcl} 
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\newcommand{\radius}{10}
\newcommand{\offsetx}{5}
\newcommand{\offsety}{5}
\newcommand{\offsetz}{10}

\begin{tikzpicture}
\begin{axis}[
    axis equal,
    view={18}{8},
    grid=major,
  ]
    \pgfmathsetmacro\sidelength{\radius/sqrt(3)}
    \pgfmathsetmacro\posAx{\offsetx-\sidelength}
    \pgfmathsetmacro\posAy{\offsety-\sidelength}
    \pgfmathsetmacro\posAz{\offsetz-\sidelength}
    \coordinate (A) at (axis cs:\posAx,\posAy,\posAz) {};
    \pgfmathsetmacro\posBx{\offsetx+\sidelength}
    \pgfmathsetmacro\posBy{\offsety-\sidelength}
    \pgfmathsetmacro\posBz{\offsetz-\sidelength}
    \coordinate (B) at (axis cs:\posBx,\posBy,\posBz) {};
    \pgfmathsetmacro\posCx{\offsetx+\sidelength}
    \pgfmathsetmacro\posCy{\offsety+\sidelength}
    \pgfmathsetmacro\posCz{\offsetz-\sidelength}
    \coordinate (C) at (axis cs:\posCx,\posCy,\posCz) {};
    \pgfmathsetmacro\posDx{\offsetx-\sidelength}
    \pgfmathsetmacro\posDy{\offsety+\sidelength}
    \pgfmathsetmacro\posDz{\offsetz-\sidelength}
    \coordinate (D) at (axis cs:\posDx,\posDy,\posDz) {};
    \pgfmathsetmacro\posEx{\offsetx-\sidelength}
    \pgfmathsetmacro\posEy{\offsety-\sidelength}
    \pgfmathsetmacro\posEz{\offsetz+\sidelength}
    \coordinate (E) at (axis cs:\posEx,\posEy,\posEz) {};
    \pgfmathsetmacro\posFx{\offsetx+\sidelength}
    \pgfmathsetmacro\posFy{\offsety-\sidelength}
    \pgfmathsetmacro\posFz{\offsetz+\sidelength}
    \coordinate (F) at (axis cs:\posFx,\posFy,\posFz) {};
    \pgfmathsetmacro\posGx{\offsetx+\sidelength}
    \pgfmathsetmacro\posGy{\offsety+\sidelength}
    \pgfmathsetmacro\posGz{\offsetz+\sidelength}
    \coordinate (G) at (axis cs:\posGx,\posGy,\posGz) {};
    \pgfmathsetmacro\posHx{\offsetx-\sidelength}
    \pgfmathsetmacro\posHy{\offsety+\sidelength}
    \pgfmathsetmacro\posHz{\offsetz+\sidelength}
    \coordinate (H) at (axis cs:\posHx,\posHy,\posHz) {};

    \draw[blue] (A) -- (B) -- (C) -- (D) -- cycle;% Bottom Face
    \draw[blue] (C) -- (D) -- (H) -- (G) -- cycle;% Back Face
    \draw[blue] (A) -- (D) -- (H) -- (E) -- cycle;% Left Face
    \draw[blue] (B) -- (C) -- (G) -- (F) -- cycle;% Right Face
    \draw[blue] (A) -- (B) -- (F) -- (E) -- cycle;% Front Face
    \draw[blue] (E) -- (F) -- (G) -- (H) -- cycle;% Top Face

    \addplot3[%
        opacity = 0.25,
        surf,
        shader=flat,
        fill=white,
        draw=black!80,
        z buffer = sort,
        samples = 41,
        variable = \u,
        variable y = \v,
        domain = 0:180,
        y domain = 0:360,
    ]
    ({\radius*cos(u)*sin(v)+\offsetx}, {\radius*sin(u)*sin(v)+\offsety}, {\radius*cos(v)+\offsetz});
\end{axis}
\end{tikzpicture}

\end{document}

cube in sphere

1
  • 1
    I hope that you will have no objection to my adding an image to demonstrate the output. (You can roll back if you do, of course.)
    – cfr
    Dec 3, 2015 at 1:48
2

Here the code:

\documentclass[12pt]{article}


\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,3d}

  \begin{document}



  \tdplotsetmaincoords{60}{110}
  \begin{tikzpicture}[scale=1.5]


    \def\R{sqrt(3)}
    \coordinate (O) at (0,0,0);
    \fill[ball color=white!10, opacity=1.0] (O) circle (\R); % 3D lighting effect

    \begin{scope}[tdplot_main_coords, shift={(0,0)}, rotate=0]
      \draw[-latex, opacity=0.2] (O)--(3.5,0,0) node[anchor=east] {$X$};
      \draw[-latex, opacity=0.2] (O)-- (0,2.8,0) node[anchor=south] {$Y$};
      \draw[-latex, opacity=0.2] (O)--(0,0,2.5) node[anchor=south] {$Z$};



      \draw[fill=blue]  coordinate (O) circle (1pt) node[anchor=south  east] {$O$};

      \draw[opacity=0.8]
      (1,-1,1)   coordinate (A) --
      (-1,-1,1)  coordinate (B) --
      (-1,1,1)   coordinate (C) --
      (1,1,1)    coordinate (D) -- cycle
      (1,1,-1)   coordinate (E) --
      (-1,1,-1)  coordinate (F) --
      (-1,-1,-1) coordinate (G) --
      (1,-1,-1)  coordinate (H) -- cycle
      (A)--(H) (B)--(G) (D)--(E) (C)--(F);

      \foreach \l in {A,B,C,D,E,F,G,H}
      \draw[fill=black] (\l) circle (1pt);
    \end{scope}
  \end{tikzpicture}


  \end{document}

and here the figure:

cube in sphere

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .