% Pictures (a subset of \LaTeX's conventions, plus squines)
\catcode`@=11 % enable private control sequences
\def\wlog#1{} % don't put allocation info into the log

\newskip\hsssglue \hsssglue=0pt plus 1fill minus 1fill \def\hsss{\hskip\hsssglue}

\newdimen\unitlength \newdimen\linethickness
\newdimen\@picheight \newdimen\@xdim \newdimen\@ydim \newdimen\@len \newdimen\@save
\newcount\@multicount \newcount\@xarg \newcount\@yarg
\newbox\@picbox \newbox\@mpbox

\font\tenln=line10     \font\tenlnw=linew10
\font\tencirc=lcircle10 \font\tencircw=lcirclew10

\def\thinlines{\let\linefont=\tenln \let\circlefont=\tencirc
  \linethickness=\fontdimen8\linefont}
\def\thicklines{\let\linefont=\tenlnw \let\circlefont=\tencircw
  \linethickness=\fontdimen8\linefont}
\thinlines

\def\beginpicture(#1,#2)(#3,#4){\@picheight=#2\unitlength
  \setbox\@picbox=\hbox to#1\unitlength\bgroup \let\line=\@line
    \kern-#3\unitlength \lower#4\unitlength\hbox\bgroup\ignorespaces}
\def\endpicture{\egroup\hss\egroup
  \ht\@picbox=\@picheight \dp\@picbox=\z@
  \leavevmode\box\@picbox}

\def\put(#1,#2)#3{\raise#2\unitlength\rlap{\kern#1\unitlength #3}\ignorespaces}

\def\multiput(#1,#2)(#3,#4)#5#6{\@multicount=#5\relax
 \@xdim=#1\unitlength \@ydim=#2\unitlength \setbox\@mpbox=\hbox{#6}%
 \loop\ifnum\@multicount>0
   \raise\@ydim\rlap{\kern\@xdim \unhcopy\@mpbox}%
   \advance\@xdim#3\unitlength \advance\@ydim#4\unitlength
   \advance\@multicount\m@ne \repeat\ignorespaces}

\def\makebox(#1,#2)#3{\setbox\@picbox=\hbox to#1\unitlength{\hss#3\hss}%
  \@ydim=\ht\@picbox \advance\@ydim-\dp\@picbox
  \ht\@picbox=#2\unitlength \dp\@picbox=\z@
  \leavevmode\lower.5\@ydim\box\@picbox}

\newif\ifneg
\def\@line(#1,#2)#3{\@xarg=#1 \@yarg=#2 \@len=#3\unitlength \leavevmode
 \ifnum\@xarg<0 \reverseline \else \negfalse \@ydim=\z@\fi
 \ifnum\@xarg=0 \@vline
 \else\ifnum\@yarg=0 \@hline \else\@sline\fi\fi
 \ifneg\kern-\@len\else\@save=\@ydim\fi}
\def\reverseline{\negtrue \kern-\@len \@xarg=-\@xarg
 \@ydim=\@len \multiply\@ydim\@yarg \divide\@ydim\@xarg \@yarg=-\@yarg}

\def\@hline{\vrule height.5\linethickness depth.5\linethickness width\@len}
\def\@vline{\kern-.5\linethickness\vrule width\linethickness
  \ifnum\@yarg<0 height\z@ depth\else depth\z@ height\fi\@len
  \kern-.5\linethickness}

\def\@sline{\setbox\@picbox=\hbox{\linefont \count@=\@xarg \multiply\count@ 8
 \ifnum\@yarg>0 \advance\count@\@yarg \advance\count@-9
 \else \advance\count@-\@yarg \advance\count@ 55 \fi \char\count@}%
 \ifnum\@yarg<0 \@picheight=-\ht\@picbox \advance\@ydim\@picheight
 \else \@picheight=\ht\@picbox \fi
 \@xdim=\wd\@picbox \@save=\@ydim
 \loop\ifdim\@xdim<\@len \raise\@ydim\copy\@picbox
  \advance\@xdim\wd\@picbox \advance\@ydim\@picheight \repeat
 \advance\@xdim-\@len \kern-\@xdim
 \multiply\@xdim\@yarg \divide\@xdim\@xarg \advance\@ydim-\@xdim
 \raise\@ydim\box\@picbox}

\def\vector(#1,#2)#3{\@line(#1,#2){#3}%
 \ifnum\@xarg=0 \@vvector \else\ifnum\@yarg=0 \@hvector \else\@svector\fi\fi}
\def\@hvector{\ifneg\rlap{\linefont\char27}\else
 \smash{\llap{\linefont\char45}}\fi} % we have to smash because of font bug
\def\@vvector{\ifnum\@yarg<0 \raise-\@len\rlap{\linefont\char63}%
 \else\setbox\@picbox=\rlap{\linefont\char54}\advance\@len-\ht\@picbox
 \raise\@len\box\@picbox\fi}

\def\@svector{\setbox\@picbox=\hbox to\z@{\linefont
 \ifnum\@yarg<0 \count@=55 \@yarg=-\@yarg \else\count@=-9 \fi
 \ifneg\multiply\@xarg16 \multiply\@yarg2
 \else\hss % \llap
  \ifnum\@xarg>2 \multiply\@xarg9 \multiply\@yarg2 \advance\count@29
  \else\ifnum\@yarg>2 \multiply\@xarg16 \multiply\@yarg9 \advance\count@-20
   \else\multiply\@xarg24 \multiply\@yarg3 \fi\fi\fi
  \advance\count@\@xarg \advance\count@\@yarg \char\count@
  \ifneg\hss\fi}% \rlap
 \raise\@save\box\@picbox}

\def\disk#1{\@len=#1\unitlength \count@='160 \@diskcirc}
\def\circle#1{\@len=#1\unitlength \count@='140 \@diskcirc}
\def\@diskcirc{\setbox\@picbox=\hbox{\circlefont\char\count@}\@xdim=\wd\@picbox
 \leavevmode \ifdim\@len>15.499\@xdim \@bigdc \else \@smalldc\fi}
\def\@bigdc{\ifnum\count@<'160 \@bigcirc
 \else \@len=15\@xdim \@diskcirc\fi}
\def\@smalldc{{\advance\@len-.5\@xdim
 \loop\ifdim\@xdim<\@len \advance\count@\@ne \advance\@xdim\wd\@picbox\repeat
 \hbox{\circlefont\char\count@}}}
\def\@bigcirc{{\circlefont\count@=15
 \setbox\@picbox=\hbox{\char\count@}\@xdim=\wd\@picbox
 \ifdim\@len>2.5\@xdim \@len=2.5\@xdim\fi
 \advance\@len-.125\wd\@picbox
 \loop\ifdim\@xdim<\@len \advance\count@ 4 \advance\@xdim.25\wd\@picbox\repeat
 \@ydim=.5\@xdim \advance\@ydim.5\linethickness
 \setbox\@picbox=\vbox{\hbox{\char\count@\advance\count@-3\char\count@}%
  \nointerlineskip
  \hbox{\advance\count@\m@ne\char\count@\advance\count@\m@ne\char\count@}}%
 \kern-\@ydim\lower\@ydim\box\@picbox}}

\newif\ifovaltl \newif\ifovaltr \newif\ifovalbl \newif\ifovalbr
\ovaltltrue \ovaltrtrue \ovalbltrue \ovalbrtrue
\def\oval(#1,#2){\@xdim=#1\unitlength \@ydim=#2\unitlength
 {\circlefont \setbox\@picbox=\hbox{\char0}
 \ifdim\@xdim<\wd\@picbox \@xdim=\wd\@picbox\fi
 \ifdim\@ydim<\wd\@picbox \@ydim=\wd\@picbox\fi
 \@save=\@xdim \ifdim\@ydim<\@save \@save=\@ydim \fi
 \count@=39
 \loop \setbox\@picbox=\hbox{\char\count@}\ifdim\@save<\wd\@picbox
  \advance\count@-4 \repeat
 \setbox\strutbox=\hbox{\vrule height\ht\@picbox depth\dp\@picbox width\z@
   \kern\wd\@picbox}%
 \@save=.5\wd\@picbox \advance\@save-.5\linethickness
 \setbox0=\hbox to\@xdim{\ifovaltl\char\count@\else\strut\fi
  \kern-\@save\leaders\hrule height\ifovaltl\linethickness\else\z@\fi\hfil
  \leaders\hrule height\ifovaltr\linethickness\else\z@\fi\hfil\kern\@save
  \ifovaltr\advance\count@-3\char\count@\else\strut\fi\kern-\wd\@picbox}%
  \advance\count@\m@ne
 \setbox2=\hbox to\@xdim{\ifovalbl\char\count@\else\strut\fi
  \kern-\@save\leaders\hrule height\ifovalbl\linethickness\else\z@\fi\hfil
  \leaders\hrule height\ifovalbr\linethickness\else\z@\fi\hfil\kern\@save
  \ifovalbr\advance\count@\m@ne\char\count@\else\strut\fi\kern-\wd\@picbox}%
 \@save=\@ydim \advance\@save-\wd\@picbox \divide\@save 2
 \setbox\@picbox=\vbox{\box0\nointerlineskip
  \hbox to\@xdim{\vrule height\@save width\ifovaltl\linethickness\else\z@\fi
    \hfil\ifovaltr\vrule width\linethickness\kern-\linethickness\fi}%
  \nointerlineskip
  \hbox to\@xdim{\vrule height\@save width\ifovalbl\linethickness\else\z@\fi
    \hfil\ifovalbr\vrule width\linethickness\kern-\linethickness\fi}%
  \nointerlineskip\box2}%
  \@save=.5\@ydim \advance\@save.5\linethickness \leavevmode
  \kern-.5\@xdim \kern-.5\linethickness \lower\@save\box\@picbox}}

\def\cpic#1\endcpic{\vcenter{\hbox{\beginpicture#1\endpicture}}}

% Squines (quadratic splines)
% example of use: to plot f(x) between x0 and x1, you can say
% \put(0,0){\squine(x0,xm,x1,y0,ym,y1)}, where y0=f(x0), y1=f(x1)
% xm=(y0-y1+s1x1-s0x0)/(s1-s0), ym=(s0(s1x1-y1)-s1(s0x0-y0))/(s1-s0),
% s0=f'(x0), and s1=f'(x1).

\newdimen\@xi \newdimen\@xii \newdimen\@xiii \newdimen\@xiv
\newdimen\@xpt \newdimen\@xoldpt
\newdimen\@yi \newdimen\@yii \newdimen\@yiii \newdimen\@yiv
\newdimen\@ypt \newdimen\@yoldpt
\def\squine(#1,#2,#3,#4,#5,#6){\setbox\@picbox\hbox{\tencirc q}%
 \global\@xoldpt=#1\unitlength \global\@yoldpt=#4\unitlength \kern\@xoldpt
 \@xi=\@xoldpt \@xii=#2\unitlength \@xiii=#3\unitlength
 \@yi=\@yoldpt \@yii=#5\unitlength \@yiii=#6\unitlength
 \squinerec
 \@xpt=#3\unitlength \@ypt=#6\unitlength \@addpoint
 \raise\@ypt\copy\@picbox}
\newif\iffar
\def\squinerec{\farfalse \testnear\@xi\@xiii \testnear\@yi\@yiii
 \iffar \decast \fi}
\def\testnear#1#2{\@save=#1\advance\@save-#2%
 \ifdim\@save<\z@ \@save=-\@save\fi \ifdim\@save>\p@ \fartrue \fi}
\def\decast{\@xpt=\@xi \advance\@xpt\@xii \divide\@xpt2
 \advance\@xii\@xiii \divide\@xii2
 \@xiv=\@xpt \advance\@xiv\@xii \divide\@xiv2
 \@ypt=\@yi \advance\@ypt\@yii \divide\@ypt2
 \advance\@yii\@yiii \divide\@yii2
 \@yiv=\@ypt \advance\@yiv\@yii \divide\@yiv2
 \begingroup\@xii=\@xpt \@xiii=\@xiv
  \@yii=\@ypt \@yiii=\@yiv \squinerec\endgroup
 \@xpt=\@xiv \@ypt=\@yiv \@addpoint
 \@xi=\@xiv \@yi=\@yiv \squinerec}
\def\@addpoint{%\message{(\the\@xpt,\the\@ypt)}%
 \global\advance\@xoldpt-\@xpt \wd\@picbox=-\@xoldpt
 \raise\@yoldpt\copy\@picbox \global\@xoldpt=\@xpt \global\@yoldpt=\@ypt}

\catcode`\@=12 % disable private control sequences
