% Block diagrams in TeXdraw

% $Id: blockdiagram.tex 1.15 2019/04/18 TeXdraw-v2r3 $

%   Copyright (C) 1993-2019  Peter Kabal

% This work is licensed under the Creative Commons Attribution (CC-BY)
% License, any version. To view the licenses, visit
% creativecommons.org/licenses/by or send a letter to
% Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

%  Peter Kabal
%  Department of Electrical & Computer Engineering
%  McGill University

%  peter dot kabal at mcgill dot ca
%  http://www-mmsp.ece.mcgill.ca/MMSP/Documents/Software/

% ===============================================================

\input txdtools

% size of sum and product circles
\def\cradius{0.08}

% \bdot
% big dot
\def\bdot{\fcir f:0 r:0.02 }

% centred, stacked items
\def\hcp#1{$\vcenter{\halign{\hss ##\hss\cr #1\crcr}}$}

% left justified, stacked items
\def\hlp#1{$\vcenter{\halign{##\hss\cr #1\crcr}}$}

% right justified, stacked items
\def\hrp#1{$\vcenter{\halign{\hss ##\cr #1\crcr}}$}

% signal labels - Top text, Bottom text, Left text, Right text,
% and Center text
\def\Ttext#1{\bsegment
               \textref h:C v:B  \htext (0 +0.06){\hcp{#1}}
             \esegment}
\def\Btext#1{\bsegment
               \textref h:C v:T  \htext (0 -0.06){\hcp{#1}}
             \esegment}
\def\Ltext#1{\bsegment
               \textref h:R v:C  \htext (-0.06 0){\hrp{#1}}
             \esegment}
\def\Rtext#1{\bsegment
               \textref h:L v:C  \htext (+0.06 0){\hlp{#1}}
             \esegment}
\def\Ctext#1{\bsegment
               \textref h:C v:C  \htext{\hcp{#1}}
             \esegment}

% ruled box (horizontal) with centered label,
% position set to the end of the box
% \Fbox W H text
\def\Fbox#1#2#3{\bsegment
                  \bsegment
                    \setsegscale 0.5
                    \textref h:C v:C  \htext (#1 0){\hcp{#3}}
                  \esegment
                  \setsegscale 0.5 \lvec (0 #2)
                  \setsegscale 1
                  \rlvec (#1 0) \rlvec (0 -#2) \rlvec (-#1 0) \lvec (0 0)
                  \savepos (#1 0)(*@x *@y)
                \esegment
                \move (*@x *@y)}

% ruled box (vertical) with centered label, position set to the end of the box
% \Gbox W H text
\def\Gbox#1#2#3{\bsegment
                  \bsegment
                    \setsegscale 0.5
                    \textref h:C v:C  \htext (0 #2){\hcp{#3}}
                  \esegment
                  \setsegscale 0.5 \lvec (#1 0)
                  \setsegscale 1
                  \rlvec (0 #2) \rlvec (-#1 0) \rlvec (0 -#2) \lvec (0 0)
                  \savepos (0 #2)(*@x *@y)
                \esegment
                \move (*@x *@y)}

% ruled triangle (horizontal) with centered label,
% position set to the end of the box
% \Ftri W H text
\def\Ftri#1#2#3{\bsegment
                  \bsegment
                    \setsegscale 0.3333
                    \textref h:C v:C  \htext (#1 0){\hcp{#3}}
                  \esegment
                  \bsegment
                    \savepos (#1 0)(*@x *@y)
                  \esegment
                  \setsegscale 0.5 \lvec (0 #2)
                  \setsegscale 1   \lvec (#1 0)
                  \setsegscale 0.5 \lvec (0 -#2) \lvec (0 0)
                \esegment
                \move (*@x *@y)}

% \pluss
% plus sign
\def\pluss {\bsegment
              \setsegscale {\cradius}
              \move (-0.5 0) \lvec (+0.5 0)
              \move (0 -0.5) \lvec (0 +0.5)
            \esegment}

% \minuss
% minus sign
\def\minuss {\bsegment
               \setsegscale {\cradius}
               \move (-0.5 0) \lvec (+0.5 0)
             \esegment}
% \mults
% multiplication sign
\def\mults {\bsegment
              \setsegscale {\cradius}
              \realmult \cradius {0.354} \tmpa
              \move (-0.354 -0.354) \lvec (+0.354 +0.354)
              \move (-0.354 +0.354) \lvec (+0.354 -0.354)
            \esegment}

% \pcir
% circle of given radius with a plus sign
\def\pcir {\lcir r:{\cradius}  \pluss}

% \mcir
% circle of given radius with a multiplication sign
\def\mcir {\lcir r:{\cradius}  \mults}

% \putn, \putnne, etc
% places text at an offset from the center of a circle, with
% the position of the text specified in compass directions
\def\puttext (#1 #2)#3{\bsegment
                         \setsegscale {\cradius}
                         \textref h:C v:C \htext (#1 #2){#3}
                       \esegment}
\def\putn   #1{\puttext ( 0   +2  ){#1}}
\def\putnne #1{\puttext (+1.2 +1.7){#1}}
\def\putene #1{\puttext (+1.7 +1.2){#1}}
\def\putese #1{\puttext (+1.7 -1.2){#1}}
\def\putsse #1{\puttext (+1.2 -1.7){#1}}
\def\puts   #1{\puttext ( 0   -2  ){#1}}
\def\putssw #1{\puttext (-1.2 -1.7){#1}}
\def\putwsw #1{\puttext (-1.7 -1.2){#1}}
\def\putwnw #1{\puttext (-1.7 +1.2){#1}}
\def\putnnw #1{\puttext (-1.2 +1.7){#1}}


% \avectoc
% arrow vector to a circle
\def\avectoc (#1 #2){\currentpos \xa\ya
                     \cossin ({\xa} \ya)(#1 #2)\cosa\sina
                     \savepos (#1 #2)(*@x *@y)%
                     \bsegment
                       \move (*@x *@y)%
                       \setsegscale {\cradius}
                       \rmove ({-\cosa} -\sina)%
                       \savecurrpos (*@x *@y)%
                     \esegment
                     \avec (*@x *@y)%
                     \move (#1 #2)}
% \avecfrc
% arrow vector from a circle
\def\avecfrc (#1 #2){\currentpos \xa\ya
                     \cossin ({\xa} \ya)(#1 #2)\cosa\sina
                     \bsegment
                       \setsegscale {\cradius}
                       \move ({\cosa} \sina)%
                       \savecurrpos (*@x *@y)%
                     \esegment
                     \move (*@x *@y)%
                     \avec (#1 #2)}
% \avecfrctoc
% arrow vector from a circle to a circle
\def\avecfrctoc (#1 #2){\currentpos \xa\ya
                        \cossin ({\xa} \ya)(#1 #2)\cosa\sina
                        \bsegment
                          \setsegscale {\cradius}
                          \move ({\cosa} \sina)%
                          \savecurrpos (*@x *@y)%
                        \esegment
                        \move (*@x *@y)%
                        \avectoc (#1 #2)}
% \lvecfrc
% line vector from a circle
\def\lvecfrc (#1 #2){\currentpos \xa\ya
                     \cossin ({\xa} \ya)(#1 #2)\cosa\sina
                     \bsegment
                       \setsegscale {\cradius}
                       \move ({\cosa} {\sina})%
                       \savecurrpos (*@x *@y)%
                     \esegment
                     \move (*@x *@y)%
                     \lvec (#1 #2)}
