[Tex/LaTex] Drawing chain with TikZ

asymptotepstrickstikz-pgf

I was answering a set of problems and found this picture. Since I am just starting to learn about TikZ, I have not been able to draw it.

enter image description here

Best Answer

Clipping, and it needs my paths.ortho library.

Improvements/Problems

  • Line widths are not accounted for.
  • Keys instead of arguments.
  • Keys also would allow to calculate a few things.
  • Recursion.
  • Adding missing upper and lower ring.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{paths.ortho}
\newcommand*\chainy[8]{% #1 = upper center       | #2 = lower center
                       % #3 = upper outer radius | #5 = lower outer radius
                       % #4 = upper inner radius | #6 = lower inner radius
                       % #7 = upper options      | #8 = lower options
\scope
  \clip (#1) -- ++(right:{#4}) arc[radius={#4}, start angle=0, delta angle=-90] --cycle
    ([shift=(right:{#3})] #1) coordinate (@)
    arc[radius={#3}, start angle=0, delta angle=-90] -- (#2) -- ++(right:{#5}) rl (@);
  \path[even odd rule, #8] (#2) circle [radius={#5}] (#2) circle [radius={#6}];
\endscope
\scope
  \clip (#2) -- ++(left:{#6}) arc[radius={#6}, start angle=180, delta angle=-90] --cycle
    ([shift=(left:{#5})] #2) coordinate (@)
    arc[radius={#5}, start angle=180, delta angle=-90] -- (#1) -- ++(left:{#3}) lr (@);
  \path[even odd rule, #7] (#1) circle [radius={#3}] (#1) circle [radius={#4}];
\endscope
\scope
  \clip (#1) rectangle ++ ({#3},{-#3});
  \path[even odd rule, #7] (#1) circle [radius={#3}] (#1) circle [radius={#4}];
\endscope
\scope
  \clip (#2) rectangle ++ ({-#5},{#5});
  \path[even odd rule, #8] (#2) circle [radius={#5}] (#2) circle [radius={#6}];
\endscope
}
\begin{document}
\begin{tikzpicture}[
  udlr/rl distance=+0pt, udlr/lr distance=+0pt,
  y=5mm, x=5mm, a/.style={fill=blue}, b/.style={fill=red}]
  \chainy{0,0}  {0,-9} {6}{5}{5}{4}{a}{b}
  \chainy{0,-9} {0,-16}{5}{4}{4}{3}{b}{a}
  \chainy{0,-16}{0,-21}{4}{3}{3}{2}{a}{b}
  \chainy{0,-21}{0,-24}{3}{2}{2}{1}{b}{a}
\end{tikzpicture}
\end{document}

Output

enter image description here