%
%	TeX macros to plot error bars with the PicTeX macros.
%
%	These macros are largely derived from the examples in the back
%	of the PiCTeX manual.
%
%	To plot with a mark at each data point, simply say
%
%	\plotWith mark M at x1 y1 x2 y2 .... xN yN /
%
%	where "M" is a mark character {e.g. $\bullet$}
%
%	To include error bars in the plot, say:
%
%	\plotWithErrorBars mark M at
%		x1 y1 e1
%		x2 y2 e2 .... xN yN eN
%	/
%
%	The line-breaking isn't needed. Marks are placed on the graph
%	at (xi, yi) for each data point. Vertical error bars are drawn
%	from (xi, yi + ei) to (xi, yi - ei).
%
%	The cross-bar at the top and bottom of the error bars is normally
%	5 pts, but can be changed by setting the \crossbarlength dimen.
%
%
%	Dirk Grunwald Apr 20 1988
%
%
\newdimen\dyposition
\newdimen\crossbarlength
\crossbarlength=5pt
\newdimen\lastPlotX
\newdimen\lastPlotY
\newdimen\tmpPlotX
\newdimen\tmpPlotY
%
%
\def\plotErrorBar#1 #2 #3 #4{%
    \message{ Plot #4 at #1 #2 }%
    \put {#4} at {#1} {#2}
    \dyposition=\Ydistance{#3}
    \put{%
      \vbox{%
	\hsize=\crossbarlength%
	\hrule height \linethickness%
	\vskip -.5\linethickness%
	\centerline{\vrule width \linethickness height 2\dyposition}%
	\nointerlineskip%
	\vskip -.5\linethickness%
	\hrule height \linethickness}%
    } at {#1} {#2} }
%
\def\plotWithErrorBars mark #1 at #2 #3 #4 {%
    \def\errorBarMark{#1}%
    %
    %    plot first point
    %
    \plotErrorBar {#2} {#3} {#4} {\errorBarMark}
    \lastPlotX=\Xdistance{#2}%
    \lastPlotY=\Ydistance{#3}%
    \futurelet\nextcharacter\plotErrorBarLoop}
%
\def\plotErrorBarLoop{%
    \if/\nextcharacter%
	\def\plotErrorBarAction{\plotErrorBarFinish}%
    \else%
	\def\plotErrorBarAction{\plotErrorBarContinued}%
    \fi%
    \plotErrorBarAction}
%
\def\plotErrorBarFinish/ {}
%
\def\plotErrorBarContinued#1 #2 #3 {%
    \plotErrorBar {#1} {#2} {#3} {\errorBarMark}%
    \tmpPlotX = \Xdistance{#1}%
    \tmpPlotY = \Ydistance{#2}%
    \setdimensionmode%
    \plot {\lastPlotX} {\lastPlotY} {\tmpPlotX} {\tmpPlotY} /
    \setcoordinatemode%
    \lastPlotX = \tmpPlotX%
    \lastPlotY = \tmpPlotY%
    \futurelet\nextcharacter\plotErrorBarLoop}
%    
\def\plotWith mark #1 at #2 #3 {%
    \def\plotWithMark{#1}%
    %
    %    plot first point
    %
    \message{Plot {\plotWithMark} at #2 #3}%
    \put {\plotWithMark} at {#2} {#3}
    \lastPlotX=\Xdistance{#2}%
    \lastPlotY=\Ydistance{#3}%
    \futurelet\nextcharacter\plotWithLoop}
%
\def\plotWithLoop{%
    \if/\nextcharacter%
	\def\plotWithAction{\plotWithFinish}%
    \else%
	\def\plotWithAction{\plotWithContinued}%
    \fi%
    \plotWithAction}
%
\def\plotWithFinish/ {}
%
\def\plotWithContinued#1 #2 {%
    \message{ Plot {\plotWithMark} at #1 #2}%
    \put {\plotWithMark} at {#1} {#2}
    \tmpPlotX = \Xdistance{#1}%
    \tmpPlotY = \Ydistance{#2}%
    \setdimensionmode%
    \plot {\lastPlotX} {\lastPlotY} {\tmpPlotX} {\tmpPlotY} /
    \setcoordinatemode%
    \lastPlotX = \tmpPlotX%
    \lastPlotY = \tmpPlotY%
    \futurelet\nextcharacter\plotWithLoop}
