
% Point and straight line macros, vertical mode.
%
% These are probably very heavy on the memory, so
% go easy on 'em.
%
% Settings: \lineunit{xxx}
%           \linewidth{xxx}
%           \resolution{xxx}
%           \linedotsevery{xxx}
%           \linedotlength{xxx}
% where
%
% \lineunit is the unit size for coordinates, and
%   should be a dimension, default is 1pt;
%
% \linewidth is the thickness of the lines, as a
%   dimension, default is 0.8pt;
%
% \resolution is the number of dots per inch in the
%   pixel-drawing, default is 100 though you'll
%   probably want it higher for good results;
%
% \linedotlength is the length of each dot in a
%   dotted line, in pixels, default 6 (note that both
%   \linedotlength and \linedotsevery produce results
%   dependent on the resolution).
%
% \linedotsevery is the number of points between
%   dots, for dotted lines (eg. the default 6
%   specifies that they're drawn with alternately
%   five blank spaces and \linedotlength dots);
%
% Drawing macros: \moveto(x,y)
%                 \lineto(x,y)
%                 \dottedlineto(x,y)
%                 \bezierto(x0,y0)(x,y)
%                 \nbezierto[n](x0,y0)(x,y)
%                 \dottedbezierto(x0,y0)(x,y)
%                 \ndottedbezierto[n](x0,y0)(x,y)
%                 \put{xxx}
%                 \point
%
% where \moveto moves the virtual drawing point,
% \lineto draws from the point to the specified coords,
% \dottedlineto draws a dotted line (well!), \bezierto
% draws a Bezier curve to (x,y) with the third control
% point being (x0,y0), \dottedbezierto draws a dotted
% Bezier curve, \nbezierto and \ndottedbezierto are
% similar except that n specifies the exact number of
% points to include in the curve, \put places arbitrary
% contents into an \hbox at the current virtual point,
% and \point draws a point at the virtual point.
%
% The coordinates are absolute by default; execute
% \relativecoordstrue for relative coordinates and
% \relativecoordsfalse to get absolute back again.
%
% During processing, the symbol [ __ ] is output to
% the terminal for each full line drawn, [ .. ] for
% each dotted line, and [ :: ] for each Bezier curve,
% whether full or dotted.


\newif\ifn@wdotting\n@wdottingtrue
\newif\ifd@ttedline\d@ttedlinefalse
\newif\ifrelativecoords\relativecoordsfalse

\newdimen\@pixsize\newdimen\@linewidth\newbox\@lineb

\newcount\br@sx\newcount\br@sy\newcount\@brdx\newcount\@brdy
\newcount\d@tcount\newcount\@xcurrent\newcount\@ycurrent \d@tcount=0

\def\@point(#1,#2){\vbox to0pt{\kern-#2\@pixsize \hbox{\kern#1\@pixsize
  \unhcopy\@lineb}\vss}\nointerlineskip}

\def\@@brhline{\vbox to0pt{\count255=\br@sx \advance\count255 by -\@brdx
  \dimen0=\count255\@pixsize \kern-\the\br@sy\@pixsize
  \hbox{\kern\the\@brdx\@pixsize
  \vrule width\dimen0 height\@linewidth depth0pt}\vss}\nointerlineskip}

\def\@@brvline{\vbox to0pt{\count255=\br@sx \advance\count255 by -\@brdx
  \dimen0=\count255\@pixsize \kern-\the\br@sx\@pixsize
  \hbox{\kern\the\br@sy\@pixsize
  \vrule width\@linewidth height\dimen0 depth0pt}\vss}\nointerlineskip}

\def\@brline(#1,#2)(#3,#4){
  \@brdx=#1 \advance\@brdx by -#3 \relax\ifnum\@brdx<0 \@brdx=-\the\@brdx \fi
  \@brdy=#2 \advance\@brdy by -#4 \relax\ifnum\@brdy<0 \@brdy=-\the\@brdy \fi
  \relax\ifnum\@brdy>\@brdx % Why is this \relax necessary?
    \edef\@brxa{#2}\edef\@brya{#1}\edef\@brxb{#4}\edef\@bryb{#3}%
    \count255=\@brdy \@brdy=\@brdx \@brdx=\count255
    \let\@brhline=\@@brvline\def\@brpoint{\@point(\the\br@sy,\the\br@sx)}
  \else\edef\@brxa{#1}\edef\@brya{#2}\edef\@brxb{#3}\edef\@bryb{#4}
    \let\@brhline=\@@brhline\def\@brpoint{\@point(\the\br@sx,\the\br@sy)}\fi
  \multiply\@brdy by2 \edef\c@nsta{\the\@brdy }\advance\@brdy by-\@brdx
  \count255=\c@nsta\advance\count255 by-\@brdx \advance\count255 by -\@brdx
  \def\@bryinc{\advance\br@sy by1 }\edef\c@nstb{\the\count255 }
  \ifnum\@brxa>\@brxb\ifnum\@brya<\@bryb\def\@bryinc{\advance\br@sy by -1 }
    \fi\br@sx=\@brxb\br@sy=\@bryb\let\xend=\@brxa
  \else\ifnum\@brya>\@bryb\edef\@bryinc{\advance\br@sy by -1 }
    \fi\br@sx=\@brxa\br@sy=\@brya\let\xend=\@brxb\fi
  \ifd@ttedline\@brpoint \else\@brdx=\br@sx \fi\@@brlineloop}

\def\@@brlineloop{\ifd@ttedline\loop{\ifnum\br@sx<\xend \advance\br@sx by1
    \ifnum\@brdy<0 \advance\@brdy by\c@nsta \relax\else
      \@bryinc \advance\@brdy by\c@nstb \relax\fi
    \advance\d@tcount by1 \ifn@wdotting\@brpoint\ifnum\d@tcount=\@linedotlength
      \d@tcount=0 \n@wdottingfalse\fi\else\ifnum\d@tcount=\@linedotsevery
      \d@tcount=0 \n@wdottingtrue\fi\fi}\repeat
  \else\loop{\ifnum\br@sx<\xend \advance\br@sx by1 % brdy plays the part of p
    \ifnum\@brdy<0 \advance\@brdy by\c@nsta \relax\else  % & brdx is old bresx
      \@brhline \@brdx=\br@sx \@bryinc
      \advance\@brdy by\c@nstb \relax\fi}\repeat\@brhline\fi}

\newdimen\@bezx \newdimen\@bezxa \newdimen\@bezxb
\newdimen\@bezy \newdimen\@bezya \newdimen\@bezyb

\def\@@nbezierto[#1](#2,#3)(#4,#5)(#6,#7){\br@sx=#1 \relax\@brdx=\br@sx
  \advance\@brdx by1 \@bezxb=#2\@pixsize \advance\@bezxb by
  -#6\@pixsize \multiply\@bezxb by2 \@bezxa=#4\@pixsize
  \advance\@bezxa by -#6\@pixsize \advance\@bezxa by -\@bezxb
  \divide\@bezxa by\br@sx \@bezyb=#3\@pixsize \advance\@bezyb by
  -#7\@pixsize \multiply\@bezyb by2 \@bezya=#5\@pixsize
  \advance\@bezya by -#7\@pixsize \advance\@bezya by -\@bezyb
  \divide\@bezya by\br@sx \count255=0 \relax\vbox to 0pt{%
    \kern-#7\@pixsize \hbox{\kern#6\@pixsize \n@wdottingtrue
    \loop{\ifnum\count255<\@brdx \ifd@ttedline\advance\d@tcount by1
      \ifn@wdotting \ifnum\d@tcount=\@linedotlength \d@tcount=0
        \n@wdottingfalse\fi\else \ifnum\d@tcount=\@linedotsevery
        \d@tcount=0 \n@wdottingtrue\fi\fi\fi
      \ifn@wdotting \@bezx=\count255\@bezxa \advance\@bezx
        by\@bezxb \divide\@bezx by\br@sx \multiply\@bezx by\count255
        \@bezy=\count255\@bezya \advance\@bezy by\@bezyb \divide\@bezy by\br@sx
        \multiply\@bezy by\count255 \raise\@bezy \hbox to 0pt{\hskip \@bezx
          \unhcopy\@lineb\hss}\fi\advance\count255 by1 }\repeat
        }\vss}\nointerlineskip}

\def\@@bezierto(#1,#2)(#3,#4)(#5,#6){\br@sx=#1 \advance\br@sx by-#5
  \ifnum\br@sx<0 \br@sx=-\br@sx \fi \br@sy=#2 \advance\br@sy by-#6
  \ifnum\br@sy<0 \br@sy=-\br@sy \fi \ifnum\br@sx>\br@sy \count255=\br@sx
  \else\count255=\br@sy \fi  \br@sx=#3 \advance\br@sx by-#1
  \ifnum\br@sx<0 \br@sx=-\br@sx \fi \br@sy=#4 \advance\br@sy by-#2
  \ifnum\br@sy<0 \br@sy=-\br@sy \fi \ifnum\br@sx>\br@sy
  \advance\count255 by\br@sx \else\advance\count255 by\br@sy \fi
  \@@nbezierto[\the\count255 ](#1,#2)(#3,#4)(#5,#6)}

\def\@nbezierto[#1](#2,#3)(#4,#5){\edef\@bezsi{\the\@xcurrent }\edef
  \@bezsii{\the\@ycurrent}\moveto(#2,#3)\br@sx=\@xcurrent \br@sy=\@ycurrent
  \moveto(#4,#5)\message{[ ::}\edef\@bezsiii{\the\br@sx}\edef\@bezsiv
  {\the\br@sy}\@@nbezierto[#1](\@bezsiii,\@bezsiv)(\the\@xcurrent,\the
  \@ycurrent)(\@bezsi,\@bezsii)\message{]}}

\def\@bezierto(#1,#2)(#3,#4){\edef\@bezsi{\the\@xcurrent }\edef
  \@bezsii{\the\@ycurrent }\moveto(#1,#2)\br@sx=\@xcurrent \br@sy=\@ycurrent
  \moveto(#3,#4)\message{[ ::}\edef\@bezsiii{\the\br@sx}\edef\@bezsiv
  {\the\br@sy}\@@bezierto(\@bezsiii,\@bezsiv)(\the\@xcurrent,\the
  \@ycurrent)(\@bezsi,\@bezsii)\message{]}}

\def\point{\@point(\the\@xcurrent,\the\@ycurrent)}

\def\lineto(#1,#2){\br@sx=\@xcurrent \br@sy=\@ycurrent \moveto(#1,#2)
  \message{[ __}\d@ttedlinefalse
  \@brline(\the\br@sx,\the\br@sy)(\the\@xcurrent,\the\@ycurrent)\message{]}}
\def\dottedlineto(#1,#2){\br@sx=\@xcurrent \br@sy=\@ycurrent \moveto(#1,#2)
  \message{[ ..}\d@ttedlinetrue
  \@brline(\the\br@sx,\the\br@sy)(\the\@xcurrent,\the\@ycurrent)\message{]}}

\def\bezierto(#1,#2)(#3,#4){\d@ttedlinefalse\@bezierto(#1,#2)(#3,#4)}
\def\dottedbezierto(#1,#2)(#3,#4){\d@ttedlinetrue\@bezierto(#1,#2)(#3,#4)}
\def\nbezierto[#1](#2,#3)(#4,#5){\d@ttedlinefalse\@nbezierto[#1](#2,#3)(#4,#5)}
\def\ndottedbezierto[#1](#2,#3)(#4,#5){%
  \d@ttedlinetrue\@nbezierto[#1](#2,#3)(#4,#5)}

\def\moveto(#1,#2){\@brdx=#1 \@brdy=#2 \dimen0=\@currentunit
  \multiply\dimen0 by\@brdx \divide\dimen0 by\@pixsize
  \ifrelativecoords\advance\@xcurrent by\dimen0 \else\@xcurrent=\dimen0 \fi
  \dimen0=\@currentunit \multiply\dimen0 by\@brdy \divide\dimen0 by\@pixsize
  \ifrelativecoords\advance\@ycurrent by\dimen0 \else\@ycurrent=\dimen0 \fi
  \relax}

\def\put#1{{\setbox\@lineb\hbox{\kern\the
  \@xcurrent\@pixsize #1}\dimen0=\the\@ycurrent\@pixsize \advance\dimen0
  by\ht\@lineb \vbox to0pt{\kern-\dimen0 \box\@lineb\vss}\nointerlineskip}}

\def\resolution#1{\@pixsize=1in \divide\@pixsize by #1 }
\def\lineunit#1{\dimen0=#1 \xdef\@currentunit{\the\dimen0 }\relax}
\def\linedotsevery#1{\xdef\@linedotsevery{#1 }}
\def\linedotlength#1{\xdef\@linedotlength{#1 }}
\def\linewidth#1{\@linewidth=#1 \setbox\@lineb\hbox {\vrule width\@linewidth
  height\@linewidth depth0pt }}

\resolution{100}\lineunit{1pt}\linewidth{0.8pt}
\linedotsevery{6}\linedotlength{6}\moveto(0,0)

\def\@M{100}
\catcode`@=12

