在 tikz 中绘制具有分子层的 3d 晶格

在 tikz 中绘制具有分子层的 3d 晶格

我想用 Tikz 实现下图(可能带有颜色)。在此处输入图片描述
以下是我到目前为止所做的:
i) 使用 Tikz 绘制 3D 立方体
ii) 从中获取分子层这里
这是我的代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\cubex}{4}
\pgfmathsetmacro{\cubey}{4}
\pgfmathsetmacro{\cubez}{4}
\draw (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\\[1cm]
\begin{tikzpicture}
  \def\nuPi{3.1459265}
  \foreach \i in {11,10,...,0}{% This one doesn't matter
    \foreach \j in {5,4,...,0}{% This will crate a membrane
                               % with the front lipids visible
      % top layer
      \pgfmathsetmacro{\dx}{rand*0.1}% A random variance in the x coordinate
      \pgfmathsetmacro{\dy}{rand*0.1}% A random variance in the y coordinate,
                                     % gives a hight fill to the lipid
      \pgfmathsetmacro{\rot}{rand*0.1}% A random variance in the
                                      % molecule orientation
      \shade[ball color=red] ({\i+\dx+\rot},{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)}) circle(0.45);
      \shade[ball color=gray] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-0.9}) circle(0.45);
      \shade[ball color=gray] (\i+\dx-\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-1.8}) circle(0.45);
      % bottom layer
      \pgfmathsetmacro{\dx}{rand*0.1}
      \pgfmathsetmacro{\dy}{rand*0.1}
      \pgfmathsetmacro{\rot}{rand*0.1}
      \shade[ball color=gray] (\i+\dx+\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-2.8}) circle(0.45);
      \shade[ball color=gray] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-3.7}) circle(0.45);
      \shade[ball color=red] (\i+\dx-\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-4.6}) circle(0.45);
    }
  }
\end{tikzpicture}
\end{document} 

在此处输入图片描述

我最终的问题是如何将图层固定到立方体上,并按照图表所示绘制内部立方体(最好带有轴)

答案1

我尝试排除复杂的代码和数学运算。这是暴力攻击。

\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary{calc,fadings,decorations.pathreplacing}

\tikzset{My Line Style/.style={ultra thick, blue, fill=yellow!10, fill opacity=0.5,join=round}}

\begin{document}
\begin{tikzpicture}[rotate around x=5] 
    \foreach \x in {0,1,2,3,4,5,6}{% 
      \foreach \z in {0,1,2,3,4,5,6}{%      
      \shade[ball color=gray] (\x,0,\z) circle(0.5);
      }
    }
    \foreach \x in {0.5,1.5,2.5,3.5,4.5,5.5}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0,1,2,3,4,5,6}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{2*sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0.5,1.5,2.5,3.5,4.5,5.5}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{3*sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0,1,2,3,4,5,6}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{4*sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0.5,1.5,2.5,3.5,4.5,5.5}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{5*sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0,1,2,3,4,5,6}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{6*sqrt(0.74)},\z) circle(0.5);
      }
    }

    \draw [My Line Style] (6,{4*sqrt(0.74)},6) -- (4,{4*sqrt(0.74)},6) -- (4,{6*sqrt(0.74)},6) -- 
                          (6,{6*sqrt(0.74)},6) -- cycle;
    \draw [My Line Style] (4,{6*sqrt(0.74)},6) -- (4,{6*sqrt(0.74)},4) -- (6,{6*sqrt(0.74)},4) --
                          (6,{6*sqrt(0.74)},6) -- cycle;
    \draw [My Line Style] (6,{4*sqrt(0.74)},6) -- (6,{4*sqrt(0.74)},4) -- (6,{6*sqrt(0.74)},4) --
                          (6,{6*sqrt(0.74)},6) -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

你可以简单地将立方体的绘图移动到tikzpicture与分子相同的位置。另外,我会不是建议使用随机数生成器来放置分子,因为你想在它们上面绘图。相反,将它们放在网格上以获取晶体结构。

下面的内容应该可以让您通过调整参数来实现您想要的效果:

在此处输入图片描述

笔记:

代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.pathreplacing}

\tikzset{My Line Style/.style={ultra thick, blue, fill=yellow!10, fill opacity=0.5, join=round}}

\begin{document}
\begin{tikzpicture}
    \newcommand*{\Radius}{0.45}%
    \newcommand*{\ZFactor}{1.5*\Radius}%
    \newcommand*{\YFactor}{2.0*\Radius}%
    \newcommand*{\XFactor}{2.0*\Radius}%

    \foreach \z in {0,...,4} {
      \pgfmathsetmacro{\ZCoord}{\ZFactor*\z}
      \foreach \x in {0,...,10} {
          \pgfmathsetmacro{\XCoord}{\YFactor*\x}
          \foreach \y in {0,...,6} {
              \pgfmathsetmacro{\YCoord}{\XFactor*\y}
              \shade[ball color=gray!20] (\XCoord,\YCoord,\ZCoord) circle(\Radius);
          }
      }
  }
  
  %% Select the ones to highlight
    \foreach \z in {2,...,4} {
      \pgfmathsetmacro{\ZCoord}{\ZFactor*\z}
      \foreach \x in {4,...,6} {
          \pgfmathsetmacro{\XCoord}{\YFactor*\x}
          \foreach \y in {3,...,5} {
              \pgfmathsetmacro{\YCoord}{\XFactor*\y}
              \shade[ball color=red!40] (\XCoord,\YCoord,\ZCoord) circle(\Radius);
          }
      }
  }
  
  
  %% Draw the cube
  \begin{scope}[shift={(3.5*\XFactor,2.5*\YFactor,-4*\ZFactor)}]%xshift=4*\Radius cm, yshift=4*\Radius]
        \pgfmathsetmacro{\cubex}{2*\XFactor}
        \pgfmathsetmacro{\cubey}{2*\YFactor}
        \pgfmathsetmacro{\cubez}{3*\ZFactor}
        \draw [My Line Style] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
        \draw [My Line Style] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
        \draw [My Line Style] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
    \end{scope}
\end{tikzpicture}
\end{document} 

答案3

另一个答案,在我看来更准确地反映了晶体结构。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.pathreplacing}
\begin{document}

% You can tweak these
\colorlet{odd plane}{black!20}
\colorlet{even plane}{black!20}
\def\ballradius{0.45}


\def\DrawRow#1#2{
    \foreach \x in {0,...,#2}
       \shade[ball color=ball] ($(#1) +(\x, 0,0)$) circle(\ballradius);
}
\def\DrawOddPlane#1{ 
  \pgfmathsetmacro{\aux}{#1-1}
  \colorlet{ball}{odd plane}
  \foreach \z in {0,...,#1} {
      \DrawRow{0,0,\z}{#1}
      \if\z#1\relax\else
      \DrawRow{0.5,0,\z+0.5}{\aux}
      \fi
  }
}
\def\DrawEvenPlane#1{ 
  \pgfmathsetmacro{\aux}{#1-1}
  \colorlet{ball}{even plane}
  \foreach \z in {0,...,#1} {
      \DrawRow{0.5,0,\z}{\aux}
      \if\z#1\relax\else
      \DrawRow{0,0,\z+0.5}{#1}
      \fi
  }
}

\begin{tikzpicture}
   \foreach \y in {0,...,3} {
      \begin{scope}[yshift=\y cm]
          \DrawOddPlane{3}
      \end{scope}
      \if\y3\relax\else
      \begin{scope}[yshift=\y cm + 0.5cm]
          \DrawEvenPlane{3}
      \end{scope}
      \fi
  }
    \pgfmathsetmacro{\cubex}{1}
    \pgfmathsetmacro{\cubey}{1}
    \pgfmathsetmacro{\cubez}{1}
    \draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw (3,3,3) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\end{document} 

生成:

结果

您可以调整偶数和奇数平面的球的颜色以及球的半径,例如:

% You can tweak these
\colorlet{odd plane}{black!20}
\colorlet{even plane}{green!20}
\def\ballradius{0.3}

结果:

结果2

相关内容