使用 Tikz,可以在球体内绘制立方体吗?

使用 Tikz,可以在球体内绘制立方体吗?

我想构造一个立方体位于球体内部的示意图。我可以分别绘制它们,但需要一些关于如何构造示意图的想法,以便立方体恰好位于球体内部,立方体的边长和球体半径之间的关系将是 r=sqrt(3)*s/2,其中 r 是容纳边长为 s 的立方体的球体的半径。

@Torbjørn T. @hugovdberg - 感谢您的建议!我尝试了以下代码,借用了网上提供的片段。但是,我的球体目前无法正确地位于立方体的同一位置。此外,我的球体方向也存在错误。

\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}

答案1

您需要将立方体移动到以原点为中心的位置。我认为使用现有代码执行此操作的最简单方法是应用scope

在此处输入图片描述

代码:

\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

在此处输入图片描述我最终成功画出了我想要的图形。立方体现在位于球体内部,并且它们都有一个共同的中心 (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

另一个答案是使用pgfplots,这样可以轻松更改视点,此外,可以轻松设置三维中的半径和偏移量。我必须说,立方体角的计算可能可以优化,但对每个坐标使用相同的 3 个宏会导致所有坐标在最后一个位置上聚集在一起。

\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}

球体中的立方体

答案4

这里是代码:

\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}

下图是:

球体中的立方体

相关内容