Chains of Rings

Chains of rings

% chain.tex :
%
\begin{filecontents*}{chainofrings.asy}
struct chainOfRings{
  int n;             // number of rings
  real w;
  pair origin;
  pen[] clrA={deepgreen,deepblue};
  pen[] clrB={white,lightyellow,palered};
  guide qring;
  real Rscaled(int);
  real rscaled(int);
  real eps;

  void drawHalf(int i,real Rt,real rt, pair p,real phi){
    qring=rotate(phi)*(arc((0,0),Rt,-eps,90+eps)--reverse(arc((0,0),rt,-eps,90+eps))--cycle);
    radialshade(shift(p)*qring
    ,clrA[i%clrA.length], p, (Rt+rt)*0.382
    ,clrB[i%clrB.length], p, (Rt+rt)*0.618
    );
    radialshade(shift(p)*rotate(180)*qring
    ,clrA[i%clrA.length], p, (Rt+rt)*0.382
    ,clrB[i%clrB.length], p, (Rt+rt)*0.618
    );
  }

  void drawChain(){
    pair p;
    real Rt,rt,dh;
    p=origin; Rt=Rscaled(0); rt=rscaled(0); 
    for(int i=0;irt && rt>0 && w>0);
      p+=(0,-(dh+Rt-w));
    }
    p=origin; Rt=Rscaled(0); rt=rscaled(0); 
    for(int i=0;irt && rt>0 && w>0);
      p+=(0,-(dh+Rt-w));
    }
  }

  void operator init(pair origin=(0,0),int n=3,real Rscaled(int),real rscaled(int)
    ,pen[] clrA={gray(0.5),gray(0.7)}
    ,pen[] clrB={white,black}
    ,real eps=0.1
  ){
    this.origin=origin; this.n=n;  
    this.Rscaled=Rscaled;
    this.rscaled=rscaled;
    this.clrA=copy(clrA);
    this.clrB=copy(clrB);
    this.eps=eps;
  }
}
\end{filecontents*}
%
\documentclass{article}
\usepackage{lmodern}
\usepackage{subcaption}
\usepackage[inline]{asymptote}
\usepackage{lmodern}
\begin{document}
\begin{figure}
\captionsetup[subfigure]{justification=centering}
    \centering
      \begin{subfigure}{0.3\textwidth}
\begin{asy}
settings.outformat="pdf";
size(8cm);
import chainofrings;
real Rscaled(int k){return 10-4/3*k;};
real rscaled(int k){return 7-k;};

chainOfRings(origin=(0,0),n=4,Rscaled,rscaled).drawChain();    
\end{asy}
%
\caption{Using default colors}
\label{fig:1a}
\end{subfigure}
%
\begin{subfigure}{0.3\textwidth}
\begin{asy}
settings.outformat="pdf";
size(8cm);
import chainofrings;
real Rscaled(int k){return 10-4/3*k;};
real rscaled(int k){return 7-k;};

chainOfRings cr=chainOfRings(origin=(30,0),n=6,Rscaled,rscaled
  ,clrA=new pen[]{deepred,deepblue}
  ,clrB=new pen[]{white,lightyellow,palered}
);
cr.drawChain();
\end{asy}
%
\caption{Using custom colors}
\label{fig:1b}
\end{subfigure}
%
\begin{subfigure}{0.3\textwidth}
\begin{asy}
settings.outformat="pdf";
size(8cm);
import chainofrings;
real Rscaled(int k){
  real[] R={20,10,5,10};
  return R[k%R.length];
};
real rscaled(int k){
  real[] r={18,6,4.5,7};
  return r[k%r.length];
};

chainOfRings cr=chainOfRings(origin=(30,0),n=8,Rscaled,rscaled
  ,clrA=new pen[]{deepred,deepblue}
  ,clrB=new pen[]{white,lightyellow,palered}
);
cr.drawChain();
\end{asy}
%
\caption{Using a set of radii}
\label{fig:1c}
\end{subfigure}
\caption{Chains of rings} \label{fig:1}
\end{figure}
\end{document}
%
% Process:
%
% pdflatex chain.tex
% asy chain-*.asy
% pdflatex chain.tex

Source: TeX.SE

Author: g.kov (License)