
To fit the original request that a chain is built by specifying only angles,
Asymptote
provides an operator overloading. An overloaded operator >
pair operator>(pair pos=(0,0), real phi)
takes a position on the left and an angle on the right,
calculates a proper transform (combined rotation and shift) and calls a procedure bead
that actually draws the bead with radial shading and a link. Thus the chains are constructed
either as pos=pos>0>45>-45>90>135
which updates the position, or just pos>0>45>-45>90>135;
pos>90>90>90;
to draw several branches from the same origin. Note that a numeric comparison
with >
still works, since it corresponds to a different signature of parameters.
beads.asy
:
size(300,300);
real linkLen=1, linkWidth=2pt;
real rl=2+linkLen; // distance between beads
guide g=(1,0)--(1+linkLen,0); // a link
pen beadColor=orange;
pen linkColor=beadColor;
void bead(transform t){
draw(t*g,linkColor+linkWidth);
radialshade(t*unitcircle,
beadColor,shift(t)*(-0.4,0.3),0.01
,black,shift(t)*(-0.4,0.3),1.5);
}
pair operator>(pair pos=(0,0), real phi){
transform t=shift(pos.x,pos.y)*rotate(phi);
bead(t); // draw a bead with a link
pos+=rl*(Cos(phi),Sin(phi)); // Sin, Cos - in degrees, sin, cos - in radians
return pos;
};
pair pos=(0,0); // start point
if(exp(1)>pi){ // note that ">" still works the usual way to compare numbers
// this branch is skipped
}else{
pos=pos>0>45>-45>90>135; // draw a bead chain and update the position
pos>0>45>-45>90>135; // continue the chain without updating the start position
beadColor=lightyellow;
linkColor=lightblue;
pos>90>90>90; // draw another branch from saved position, with different colours
}
A standalone pdf is produced by asy -f pdf beads.asy
Edit: This is an inline version, beads-inline.tex
:
\documentclass[10pt,a4paper]{article}
\usepackage{lmodern}
\usepackage{soul}
\usepackage{caption}
\usepackage[inline]{asymptote}
\def\asydir{}
\begin{asydef}
// Global Asymptote definitions
real linkLen=1, linkWidth=2pt;
real rl=2+linkLen; // distance between beads
guide link=(1,0)--(1+linkLen,0); // a link
pen beadColor=orange;
pen linkColor=beadColor;
void bead(transform t){
draw(t*link,linkColor+linkWidth);
radialshade(t*unitcircle,
beadColor,shift(t)*(-0.4,0.3),0.01
,black,shift(t)*(-0.4,0.3),1.5);
}
pair operator>(pair pos=(0,0), real phi){
transform t=shift(pos)*rotate(phi);
bead(t); // draw a bead with a link
pos+=rl*(Cos(phi),Sin(phi)); // Sin, Cos - in degrees, sin, cos - in radians
return pos;
};
pair pos;
\end{asydef}
\begin{document}
\newcommand\hi[1]{\bf Hello \st{Word} #1!}
\center
\captionof{figure}{Example of bead chain building.}
\begin{asy}
size(150,150);
pos>0>60>0>70;
beadColor=lightyellow;
pos>0>-60>0>-70;
pos= pos>0>0>0>0;
label("\textbf{Example}",pos,SE);
\end{asy}
\center
\captionof{figure}{Another example of bead chain building.}
\begin{asy}
size(150,150);
beadColor=lightyellow;
linkColor=lightblue;
pos=pos>90>42>0>0>0>0>0>0>-130;
label(
"\parbox{8cm}{"
"The following command \texttt{\textbackslash hi} is defined \\"
+"in the main \LaTeX\ document:\\"
"\hi{\LaTeX}}"
,pos);
\end{asy}
\end{document}
To process it with latexmk
, create file latexmkrc
:
sub asy {return system("asy '$_[0]'");}
add_cus_dep("asy","eps",0,"asy");
add_cus_dep("asy","pdf",0,"asy");
add_cus_dep("asy","tex",0,"asy");
and run latexmk -pdf beads-inline.tex
. The result looks like

For more details consult the manual at: asymptote development page at sourceforge
or, in case if you happened to be a lucky TeX Live
user,
just type texdoc asymptote
.