/* hlcross.c */
#include "tdp.h"
void hlcross(h1_p, v1_p, h2_p, v2_p, npts, inc, dir, pass2)
int *h1_p, *v1_p, *h2_p, *v2_p;
short npts, inc, dir, pass2;
{
   register short ih,i,   /* CURRENT HORIZ PIXEL, COUNTER           */
   h1, v1, h2, v2;        /* PREV, CURRENT DATA POINTS              */
   short vtry, idh, idv;  /* INTERPOLATED V VALUE, DELTA-H, DELTA-V */
   FFP dh, dv;            /* FFP VERSIONS OF IDH, IDV               */

   /*** PLOT LINE SEGMENTS CONNECTING LINES PLOTTED BY HLPLOT() ***/
   for (i=0; i < npts; i++, h1_p+=inc, v1_p+=inc, h2_p+=inc, v2_p+=inc) {

      h1 = *h1_p; h2 = *h2_p; v1 = *v1_p; v2 = *v2_p;
      idh = h2-h1; idv = v2-v1;

      /*** INIT PEN FOR NEW LINE SEGMENT ***/
      pen(RESET,h1,RESET,dir);
      if (!pass2) {
         if (v1 >= vhicum[h1]) {
            if (!vhicum[h1]) vlocum[h1] = v1-1;
            vhicum[h1] = v1; pen(DOWN,h1,DATA,dir,vhicum);
         }
         else
            pen(UP,h1,DATA,dir,vhicum);
      }
      else {
         if (v1 <= vlocum[h1])
            {vlocum[h1] = v1; pen(DOWN,h1,DATA,dir,vlocum);}
         else
            pen(UP,h1,DATA,dir,vlocum);
      }
      
      /*** THE USUAL CASE: V(H) IS INTERPOLABLE IN [h1..h2] ***/
      if (abs(idh) > 2) {
         dv = (FFP)(idv); dh=(FFP)(idh); ih=h1+dir;
         while (ih != h2) {
            vtry = v1 + (short)(dv * (FFP)(ih-h1)/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=v1+idv/2) >= vhicum[(ih=h1+dir)]) {
                  if (!vhicum[ih]) vlocum[ih] = vtry-1;
                  pen(DOWN,h1,DATA,0,vhicum);
                  vhicum[ih] = vtry; pen(DOWN,ih,DATA,0,vhicum);
               }
            }
            else {
               if ((vtry=v1+idv/2) <= vlocum[(ih=h1+dir)]) {
                  pen(DOWN,h1,DATA,0,vlocum);
                  vlocum[ih] = vtry; pen(DOWN,ih,DATA,0,vlocum);
               }
            }
         }
         else {
            if (!pass2)
               {if (v2 >= vhicum[h2]) pen(DOWN,h1,DATA,0,vhicum);}
            else
               {if (v2 <= vlocum[h2]) pen(DOWN,h1,DATA,0,vlocum);}

         }
      }

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

