/* hlplot.c */
#include "tdp.h"
void hlplot(h, v, nhpts, inc, dir, pass2)
int *h, *v;
short nhpts, inc, dir, pass2;
{
   register short ih,i,     /* CURRENT HORIZ PIXEL, COUNTER           */
   hlast,vlast,hthis,vthis; /* PREV, CURRENT DATA POINTS              */
   short vtry, idh, idv;    /* INTERPOLATED V VALUE, DELTA-H, DELTA-V */
   FFP dh, dv;              /* FFP VERSIONS OF IDH, IDV               */

   hlast = (short)*h; vlast = (short)*v;
   
   /*** INIT PEN FOR NEW LINE ***/
   pen(RESET,hlast,RESET,dir);
   if (!pass2) {
      if (vlast >= vhicum[hlast]) {
         if (!vhicum[hlast]) vlocum[hlast] = vlast-1;
         vhicum[hlast] = vlast; pen(DOWN,hlast,DATA,dir,vhicum);
      }
   }
   else {
      if (vlast <= vlocum[hlast])
         {vlocum[hlast] = vlast; pen(DOWN,hlast,DATA,dir,vlocum);}
   }
   
   /*** PLOT V(H) IN "DIR" DIRECTION ***/
   for (i=0; i < nhpts-1; i++) {
      hthis = (short)*(h += inc); vthis = (short)*(v += inc);
      idh = hthis-hlast; idv = vthis-vlast;

      /*** THE USUAL CASE: V(H) IS INTERPOLABLE IN [hlast..hthis] ***/
      if (abs(idh) > 2) {
         dv = (FFP)(idv); dh = (FFP)(idh); ih = hlast+dir;
         while (ih != hthis) {
            vtry = vlast + (short)(dv * (FFP)(ih-hlast)/dh);
            if (!pass2) {
               if (vtry >= vhicum[ih]) {
                  if (!vhicum[ih]) vlocum[ih] = vtry-1;
                  vhicum[ih] = vtry; pen(DOWN,ih,INTERP,dir,vhicum);
               }
               else
                  pen(UP,ih,INTERP,dir,vhicum);
            }
            else {
               if (vtry <= vlocum[ih])
                  {vlocum[ih] = vtry; pen(DOWN,ih,INTERP,dir,vlocum);}
               else
                  pen(UP,ih,INTERP,dir,vlocum);
            }
            ih += dir;
         }
      }
      else {
         /*** SPECIAL CASE: (NEARLY) VERTICAL LINE SEGMENT ***/
         if (abs(idh) == 2) {
            if (!pass2) {
               if ((vtry=vlast+idv/2) >= vhicum[(ih=hlast+dir)]) {
                  pen(DOWN,hlast,DATA,0,vhicum);
                  if (!vhicum[ih]) vlocum[ih] = vtry-1;
                  vhicum[ih] = vtry; pen(DOWN,ih,DATA,0,vhicum);
               }
            }
            else {
               if ((vtry=vlast+idv/2) <= vlocum[(ih=hlast+dir)]) {
                  pen(DOWN,hlast,DATA,0,vlocum);
                  vlocum[ih] = vtry; pen(DOWN,ih,DATA,0,vlocum);
               }
            }
         }
         else {
            if (!pass2)
               {if (vthis >= vhicum[hthis]) pen(DOWN,hlast,DATA,0,vhicum);}
            else
               {if (vthis <= vlocum[hthis]) pen(DOWN,hlast,DATA,0,vlocum);}
         }
      }

      /*** FINISH A LINE SEGMENT ***/
      if (!pass2) {
         if (vthis >= vhicum[hthis]) {
            if (!vhicum[hthis]) vlocum[hthis] = vthis-1;
            vhicum[hthis] = vthis; pen(DOWN,hthis,DATA,dir,vhicum);
         }
         else
            pen(UP,ih,DATA,dir,vhicum);
      }
      else {
         if (vthis <= vlocum[hthis])
            {vlocum[hthis] = vthis; pen(DOWN,hthis,DATA,dir,vlocum);}
         else
            pen(UP,ih,DATA,dir,vlocum);
      }

      hlast = hthis; vlast = vthis;
   }
}

