% ecldig.sty for Directed Graphs by Hideki Isozaki
%
% \documentstyle[epic,eepic,eclarith,ecldig]{article}
%
\typeout{ecldig.sty ver. 1.0 for Directed Graphs}

\def\digradius#1{\@killglue\def\dig@rad{#1}\ignorespaces}

\def\dignode(#1,#2)#3#4{\@killglue
	\Mul\dig@rad{2}\dig@diam
	\node(#1,#2){#3}{#4}%
	\put(#1,#2){\circle{\dig@diam}}\ignorespaces}

\def\digedge{\@ifnextchar[{\dig@spedge}{\dig@stedge}}

\def\dig@stedge#1#2{\@killglue
	\edef\dig@circi{\@nameuse{ND!#1}}%
	\edef\dig@circii{\@nameuse{ND!#2}}%
	\vecsub\dig@circii\dig@circi\dig@circdif
	\unitvec\dig@circdif\dig@unitdif
	\scaledvec\dig@rad\dig@unitdif\dig@arcdif
	\vecadd\dig@circi\dig@arcdif\dig@edgei
	\vecsub\dig@circii\dig@arcdif\dig@edgeii
	\edef\dig@arg{\dig@edgei\dig@edgeii}%
	\expandafter\dig@pen\dig@arg
	\ignorespaces}

\def\dig@spedge[#1]#2#3{\@killglue
	\edef\dig@circi{\@nameuse{ND!#2}}%
	\edef\dig@circii{\@nameuse{ND!#3}}%
	\vecsub\dig@circii\dig@circi\dig@circdif
        \scaledvec{0.5}\dig@circdif\dig@half
        \vecadd\dig@half\dig@circi\dig@mid
        \vecorth\dig@half\dig@orth
        \unitvec\dig@orth\dig@dist
        \scaledvec{#1}\dig@dist\dig@dist
        \vecadd\dig@mid\dig@dist\dig@dist
	\unitvec\dig@circdif\dig@unitdif
	\scaledvec\dig@rad\dig@unitdif\dig@arcdif
	\vecadd\dig@circi\dig@arcdif\dig@edgei
	\vecsub\dig@circii\dig@arcdif\dig@edgeii
	\edef\dig@arg{\dig@edgei\dig@dist\dig@edgeii}%
	{\let\arrow=\@iden\let\revarrow=\@iden
        \expandafter\dig@pen\dig@arg}%
	\edef\dig@arg{\dig@dist\dig@edgeii}%
	\def\dig@temp{\arrow\@noline}%
        \expandafter\dig@temp\dig@arg
	\ignorespaces}

\def\vecorth#1#2{%
	\let\dig@x=\relax \let\dig@y=\relax
	\edef\dig@arg{#1(\dig@x,\dig@y)}%
	\expandafter\@vecorth\dig@arg
	\edef#2{(\dig@x,\dig@y)}}

\def\@vecorth(#1,#2)(#3,#4){\edef#3{#2}\edef#4{-#1}}

\def\vecsub#1#2#3{%
	\let\dig@x=\relax \let\dig@y=\relax
	\edef\dig@arg{#1#2(\dig@x,\dig@y)}%
	\expandafter\@vecsub\dig@arg
	\edef#3{(\dig@x,\dig@y)}}

\def\@vecsub(#1,#2)(#3,#4)(#5,#6){%
	\Sub{#1}{#3}{#5}\Sub{#2}{#4}{#6}}

\def\vecadd#1#2#3{%
	\let\dig@x=\relax \let\dig@y=\relax
	\edef\dig@arg{#1#2(\dig@x,\dig@y)}%
	\expandafter\@vecadd\dig@arg
	\edef#3{(\dig@x,\dig@y)}}
\def\@vecadd(#1,#2)(#3,#4)(#5,#6){%
	\Add{#1}{#3}{#5}\Add{#2}{#4}{#6}}

\def\unitvec#1#2{%
	\let\dig@x=\relax \let\dig@y=\relax
	\edef\dig@arg{#1(\dig@x,\dig@y)}%
	\expandafter\Unit\dig@arg
	\edef#2{(\dig@x,\dig@y)}}

\def\scaledvec#1#2#3{%
	\let\dig@x=\relax \let\dig@y=\relax
	\edef\dig@arg{{#1}#2(\dig@x,\dig@y)}%
	\expandafter\@scaledvec\dig@arg
	\edef#3{(\dig@x,\dig@y)}}
\def\@scaledvec#1(#2,#3)(#4,#5){%
	\Mul{#1}{#2}{#4}\Mul{#1}{#3}{#5}}

\def\digpen#1{\@killglue\def\dig@pen{#1}\ignorespaces}

\def\node(#1,#2)#3#4{\@killglue\@namedef{ND!#3}{(#1,#2)}%
	\put(#1,#2){#4}\ignorespaces}

\def\@nodeput#1#2{\@killglue
  \edef\dig@vecarg{\@nameuse{ND!#1}}%
  \vecadd\dig@vecarg\dig@sh\dig@vecarg
  \expandafter\put\dig@vecarg{#2}\ignorespaces}

\def\nodeput{\@ifnextchar[{\@nodeshift}{\@nodeshiftc}}

\def\@nodeshift[#1]{%
  \DegRad{#1}\dig@radian
  \Cos\dig@radian\dig@x \Sin\dig@radian\dig@y
  \edef\dig@sh{(\dig@x,\dig@y)}%
  \scaledvec\dig@rad\dig@sh\dig@sh
  \@nodeput}

\def\@nodeshiftc{\edef\dig@sh{(\dig@x,\dig@y)}\@nodeput}

\digpen{\arrow\path}

\def\digloop{%
   \scaledvec{2}\dig@sh\dig@sh
   \edef\dig@argvec{(0,0)\dig@sh}\expandafter\dig@loop\dig@argvec}

\edef\orig@colon{\the\catcode`\!}

\catcode`\!=11

\def\dig@loop(#1,#2)(#3,#4){\@killglue
  \Sub{#3}{#1}\arr!Dx \Sub{#4}{#2}\arr!Dy
  \Mul\arr!Dx{0.4}\arr!Sx \Mul\arr!Dy{0.4}\arr!Sy
  \Mul\arr!Sx{-0.6}\arr!Tx \Mul\arr!Sy{-0.8}\arr!Vx
  \Mul\arr!Sx{0.8}\arr!Ty \Mul\arr!Sy{-0.6}\arr!Vy
  \Add\arr!Tx\arr!Vx\arr!Hx \Add\arr!Ty\arr!Vy\arr!Hy
  \Add\arr!Hx{#3}\arr!Lx \Add\arr!Hy{#4}\arr!Ly
  \Sub\arr!Tx\arr!Vx\arr!Hx \Sub\arr!Vy\arr!Ty\arr!Hy
  \Add\arr!Hx{#3}\arr!Rx \Add\arr!Hy{#4}\arr!Ry
  {\let\arrow=\@iden \let\revarrow=\@iden
  \spline(#1,#2)(\arr!Lx,\arr!Ly)(#3,#4)(\arr!Rx,\arr!Ry)(#1,#2)}
  \arrow\@noline(\arr!Rx,\arr!Ry)(#1,#2)\ignorespaces}

\def\@noline(#1,#2)(#3,#4){}

\catcode`\!=\orig@colon


