5

I am learning how to make figures using TikZ, and I am trying to do a stack of 5 cylinders with gaps between them. I am thinking about something like the following graph:

Cylinders

I have managed to plot 5 stacked cylinders as:

\begin{tikzpicture}
\node (Electrode0) [cylinder, shape border rotate=90, draw,
minimum height=1.322cm,minimum width=2.976cm, fill=white] {};
\node (Electrode1) [cylinder, shape border rotate=90, draw,
minimum height=1.322cm,minimum width=2.976cm,
above=0.05cm of Electrode0.before top, anchor=after bottom, fill=white] {};
\node (Electrode2) [cylinder, shape border rotate=90, draw,
minimum height=1.322cm,minimum width=2.976cm,
above=0.05cm of Electrode1.before top, anchor=after bottom, fill=white] {};
\node (Electrode3) [cylinder, shape border rotate=90, draw,
minimum height=1.322cm,minimum width=2.976cm,
above=0.05cm of Electrode2.before top, anchor=after bottom, fill=white] {};
\node (Electrode4) [cylinder, shape border rotate=90, draw,
minimum height=1.322cm,minimum width=2.976cm,
above=0.05cm of Electrode3.before top, anchor=after bottom, fill=white] {};
\end{tikzpicture}

My try

Can someone point me into the right direction on how to make the cylinders hollow, and how can I cut them in half like the picture above?

1 Answer 1

12

Just define a pic for this and use some orthographic projection from e.g. the perspective library. The pic half cylinder shell has the parameters r (inner radius), h (height) and dr (thickness of the mantle). It names some coordinates that get prefixed by the name of the pic and can be used from the outside.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,perspective}
\makeatletter
\pgfmathdeclarefunction{az}{0}{\pgfmathparse{\pgf@view@az}}%
\pgfmathdeclarefunction{el}{0}{\pgfmathparse{\pgf@view@el}}%
\makeatother
\begin{document}
\begin{tikzpicture}[bullet/.style={circle,fill,inner sep=1.5pt},
    pics/half cylinder shell/.style={code={
    \tikzset{half cylinder shell/.cd,#1}
    \def\pv##1{\pgfkeysvalueof{/tikz/half cylinder shell/##1}}%
    \pgfmathsetmacro{\alphacrit}{(az < 0 ? 180+az : az)} 
    \pgfmathsetmacro{\alphamax}{(az < 0 ? 180 : 0)} 
    \draw plot[variable=\t,domain=0:180,smooth]
     ({\pv{r}*cos(\t)},{\pv{r}*sin(\t)},-\pv{h}/2)
     plot[variable=\t,domain=180:0,smooth]
     ({\pv{r}*cos(\t)},{\pv{r}*sin(\t)},\pv{h}/2)
     plot[variable=\t,domain=\alphacrit:\alphamax,smooth]
     ({(\pv{r}+\pv{dr})*cos(\t)},{(\pv{r}+\pv{dr})*sin(\t)},-\pv{h}/2)
     plot[variable=\t,domain=180:0,smooth]
     ({(\pv{r}+\pv{dr})*cos(\t)},{(\pv{r}+\pv{dr})*sin(\t)},\pv{h}/2)
     ;
    \foreach \XX/\YY in {-1/L,1/R} 
    {\draw[fill=gray!20]  
        ({\XX*\pv{r}},0,-\pv{h}/2) coordinate (-plate-\YY-ib)
      -- ({\XX*(\pv{r}+\pv{dr})},0,-\pv{h}/2) coordinate (-plate-\YY-ob)
      -- ({\XX*(\pv{r}+\pv{dr})},0,\pv{h}/2) coordinate (-plate-\YY-ot)
      -- ({\XX*\pv{r}},0,\pv{h}/2) coordinate (-plate-\YY-it)
      -- cycle;}
    \draw 
    ({(\pv{r}+\pv{dr})*cos(\alphacrit)},{(\pv{r}+\pv{dr})*sin(\alphacrit)},-\pv{h}/2) 
    -- 
    ({(\pv{r}+\pv{dr})*cos(\alphacrit)},{(\pv{r}+\pv{dr})*sin(\alphacrit)},\pv{h}/2);
    }},
 half cylinder shell/.cd,r/.initial=1,dr/.initial=0.2,h/.initial=1]
 \begin{scope}[3d view={20}{15},scale=1.5,transform shape]
  \path
   (0,0,0) pic(hc1){half cylinder shell={h=0.8}}
   (0,0,0.8) pic(hc2){half cylinder shell={h=0.4}}
   (0,0,1.3) pic(hc3){half cylinder shell={h=0.2}}
   (0,0,1.8) pic(hc4){half cylinder shell={h=0.4}}
   (0,0,2.6) pic(hc5){half cylinder shell={h=0.8}};
  \draw[red,-latex] (0,0,-0.5) -- (0,0,4)node[below right,black]{$z$}; 
  \draw[red] (0,0,2.6) -- ($(hc5-plate-L-it)!0.5!(hc5-plate-L-ib)$)
   node[midway,above,black]{$a$}; 
 \end{scope}
 \draw[red] foreach \X in {1,5}
 {($(hc\X-plate-L-ot)!0.5!(hc\X-plate-L-ob)$) node[bullet,label=above
 left:{$U_2=0$}]{}  -- ++ (-1.5,0)}
  foreach \X in {2,4}
 {($(hc\X-plate-L-ot)!0.5!(hc\X-plate-L-ob)$) node[bullet,label=above
 left:{$U_1$}]{}  -- ++ (-1.5,0)}
  foreach \X in {3}
 {($(hc\X-plate-L-ot)!0.5!(hc\X-plate-L-ob)$) node[bullet,label=above
 left:{$U_0$}]{}  -- ++ (-1.5,0)};  
\end{tikzpicture}
\end{document}

enter image description here

As usual, one can simplify some things and make others fancier. For instance, one can place the half cylinders in a loop, and shade them.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,perspective}
\makeatletter
\pgfmathdeclarefunction{az}{0}{\pgfmathparse{\pgf@view@az}}%
\pgfmathdeclarefunction{el}{0}{\pgfmathparse{\pgf@view@el}}%
\makeatother
\begin{document}
\begin{tikzpicture}[bullet/.style={circle,fill,inner sep=1.5pt},
    pics/half cylinder shell/.style={code={
    \tikzset{half cylinder shell/.cd,#1}
    \def\pv##1{\pgfkeysvalueof{/tikz/half cylinder shell/##1}}%
    \pgfmathsetmacro{\alphacrit}{(az < 0 ? 180+az : az)} 
    \pgfmathtruncatemacro{\alphamax}{(az < 0 ? 180 : 0)} 
    \draw 
     plot[variable=\t,domain=\alphacrit:\alphamax,smooth]
     ({(\pv{r}+\pv{dr})*cos(\t)},{(\pv{r}+\pv{dr})*sin(\t)},-\pv{h}/2)
     plot[variable=\t,domain=180:0,smooth]
     ({(\pv{r}+\pv{dr})*cos(\t)},{(\pv{r}+\pv{dr})*sin(\t)},\pv{h}/2);
    \ifnum\alphamax=180 
     \draw[left color=gray!30,middle color=white,right color=gray!50!black]
      plot[variable=\t,domain=0:\alphacrit,smooth]
      ({\pv{r}*cos(\t)},{\pv{r}*sin(\t)},-\pv{h}/2)
      -- 
      plot[variable=\t,domain=180:0,smooth]
      ({\pv{r}*cos(\t)},{\pv{r}*sin(\t)},\pv{h}/2);  
     \draw[left color=gray,right color=gray!10] 
       plot[variable=\t,domain=\alphamax:\alphacrit,smooth]
       ({(\pv{r}+\pv{dr})*cos(\t)},{(\pv{r}+\pv{dr})*sin(\t)},-\pv{h}/2)
       -- plot[variable=\t,domain=\alphacrit:\alphamax,smooth]
       ({(\pv{r}+\pv{dr})*cos(\t)},{(\pv{r}+\pv{dr})*sin(\t)},\pv{h}/2)
       -- cycle;     
    \else
     \draw[left color=gray!30,middle color=white,right color=gray!50!black]
      plot[variable=\t,domain=180:\alphacrit,smooth]
      ({\pv{r}*cos(\t)},{\pv{r}*sin(\t)},-\pv{h}/2)
      -- 
      plot[variable=\t,domain=0:180,smooth]
      ({\pv{r}*cos(\t)},{\pv{r}*sin(\t)},\pv{h}/2);  
     \draw[left color=gray!10,right color=gray] 
       plot[variable=\t,domain=\alphamax:\alphacrit,smooth]
       ({(\pv{r}+\pv{dr})*cos(\t)},{(\pv{r}+\pv{dr})*sin(\t)},-\pv{h}/2)
       -- plot[variable=\t,domain=\alphacrit:\alphamax,smooth]
       ({(\pv{r}+\pv{dr})*cos(\t)},{(\pv{r}+\pv{dr})*sin(\t)},\pv{h}/2)
       -- cycle;     
    \fi
    \foreach \XX/\YY in {-1/L,1/R} 
    {\draw[fill=gray!20]  
        ({\XX*\pv{r}},0,-\pv{h}/2) coordinate (-plate-\YY-ib)
      -- ({\XX*(\pv{r}+\pv{dr})},0,-\pv{h}/2) coordinate (-plate-\YY-ob)
      -- ({\XX*(\pv{r}+\pv{dr})},0,\pv{h}/2) coordinate (-plate-\YY-ot)
      -- ({\XX*\pv{r}},0,\pv{h}/2) coordinate (-plate-\YY-it)
      -- cycle;}
    \draw 
    ({(\pv{r}+\pv{dr})*cos(\alphacrit)},{(\pv{r}+\pv{dr})*sin(\alphacrit)},-\pv{h}/2) 
    -- 
    ({(\pv{r}+\pv{dr})*cos(\alphacrit)},{(\pv{r}+\pv{dr})*sin(\alphacrit)},\pv{h}/2);
    }},
 half cylinder shell/.cd,r/.initial=1,dr/.initial=0.2,h/.initial=1]
 \begin{scope}[3d view={-20}{15},scale=1.5,transform shape]
  \foreach \X [count=\Y,remember=\X as \LastX (initially 0),
    remember=\TotalX as \TotalX (initially 0)] 
    in {0.8,0.4,0.2,0.4,0.8}% <- heights of the half cylinders
  {\pgfmathsetmacro{\TotalX}{\TotalX+\X/2+\LastX/2+0.15}
    \path (0,0,\TotalX) pic(hc\Y)   {half cylinder shell={h=\X}};}  
  \draw[red,-latex] (0,0,-0.5) -- (0,0,4)node[below right,black]{$z$}; 
  \draw[red] (0,0,2.6) -- ($(hc5-plate-L-it)!0.5!(hc5-plate-L-ib)$)
   node[midway,above,black]{$a$}; 
 \end{scope}
 \draw[red] foreach \X in {1,5}
 {($(hc\X-plate-L-ot)!0.5!(hc\X-plate-L-ob)$) node[bullet,label=above
 left:{$U_2=0$}]{}  -- ++ (-1.5,0)}
  foreach \X in {2,4}
 {($(hc\X-plate-L-ot)!0.5!(hc\X-plate-L-ob)$) node[bullet,label=above
 left:{$U_1$}]{}  -- ++ (-1.5,0)}
  foreach \X in {3}
 {($(hc\X-plate-L-ot)!0.5!(hc\X-plate-L-ob)$) node[bullet,label=above
 left:{$U_0$}]{}  -- ++ (-1.5,0)};  
\end{tikzpicture}
\end{document}

enter image description here

This also shows that one can change the view angles to some extent.

You must log in to answer this question.

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