3

I would like to draw atoms in this square where in the left of square blue atoms in the right of square red atoms and in the middle black.

\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}
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – user36296 Jul 10 '16 at 13:00
  • 2
    I'm guessing that this is not your code. If so, please attribute it correctly as a matter of courtesy to the original author (if nothing else). – cfr Jul 10 '16 at 22:26
  • 1
    I guess the original source is texample.net/tikz/examples/membrane-surface Quote from there: "Proper attribution is to mention somewhere in your code or document who made the illustration and to provide a link to texample.net." – user36296 Jul 11 '16 at 9:03
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – user36296 Aug 13 '16 at 16:16
2

I hope this is what you want to get,

If so, the most straightforward solution is to use the ifthen package, use it to define a command containing the color, and use this command to set the color in the several \shade commands.

\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

There is no need to load any additional packages as TikZ already allows you to set the colours conditionally as part of the \foreach loop. I'm not sure whether you want sudden, dramatic changes of colour like this:

sudden

or a gradual change like this:

gradual

Either way, here's the code:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.