原子模拟

原子模拟

我想在这个正方形中画出原子,正方形左边是蓝色原子,正方形右边是红色原子,中间是黑色。

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\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

我希望这是你想要的,

如果是这样,最直接的解决方案是使用ifthen包,用它来定义一个包含颜色的命令,并在几个命令中用这个命令来设置颜色\shade

\documentclass{standalone}

\usepackage{tikz}
\usepackage{ifthen}

\begin{document}

\begin{tikzpicture}
\def\nuPi{3.1459265}
\foreach \i in {11,10,...,0}{% This one doesn't matter
  \ifthenelse{\i = 0 \OR \i = 11}{\newcommand{\colore}{red}}{\newcommand{\colore}{gray}}
\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=\colore] ({\i+\dx+\rot},{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)}) circle(0.45);
\shade[ball color=\colore] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-0.9}) circle(0.45);
\shade[ball color=\colore] (\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=\colore] (\i+\dx+\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-2.8}) circle(0.45);
\shade[ball color=\colore] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-3.7}) circle(0.45);                                                               
\shade[ball color=\colore] (\i+\dx-\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-4.6}) circle(0.45);
}
}
\end{tikzpicture}
\end{document}

答案2

无需加载任何其他包,因为 TikZ 已经允许您在循环中有条件地设置颜色\foreach。我不确定您是否想要像这样突然、剧烈的颜色变化:

突然

或者像这样逐渐改变:

循序渐进

无论哪种方式,代码如下:

\documentclass[tikz,border=10pt,multi]{standalone}
\begin{document}
\begin{tikzpicture}
  \def\nuPi{3.1459265}
  \foreach \i [evaluate=\i as \k using {\i == 11 ? "red" : (\i == 0 ? "blue" : "gray") }] in {11,10,...,0}{%
    \colorlet{mycolour}{\k}
    \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
      % molecule orientation
      \shade[ball color=mycolour] ({\i+\dx+\rot},{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)}) circle(0.45);
      \shade[ball color=mycolour] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-0.9}) circle(0.45);
      \shade[ball color=mycolour] (\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=mycolour] (\i+\dx+\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-2.8}) circle(0.45);
      \shade[ball color=mycolour] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-3.7}) circle(0.45);
      \shade[ball color=mycolour] (\i+\dx-\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-4.6}) circle(0.45);
    }
  }
\end{tikzpicture}
\begin{tikzpicture}
  \def\nuPi{3.1459265}
  \foreach \i [evaluate=\i as \k using {\i > 0 ? "red" : (\i < 0 ? "blue" : "gray") }, evaluate=\i as \m using {abs(\i)*20}] in {-5,...,5}{%
    \colorlet{mycolour}{\k!\m!gray}
    \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=mycolour] ({\i+\dx+\rot},{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)}) circle(0.45);
      \shade[ball color=mycolour] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-0.9}) circle(0.45);
      \shade[ball color=mycolour] (\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=mycolour] (\i+\dx+\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-2.8}) circle(0.45);
      \shade[ball color=mycolour] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-3.7}) circle(0.45);
      \shade[ball color=mycolour] (\i+\dx-\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-4.6}) circle(0.45);
    }
  }
\end{tikzpicture}
\end{document}

相关内容