
#if PROTOTYPE_ALLOWED
int axis_crossed(LONLAT_TYPE exit, LONLAT_TYPE reentry,
                 RANGE_TYPE lon_range, RANGE_TYPE lat_range)
#else
int axis_crossed(exit, reentry, lon_range, lat_range)
                 LONLAT_TYPE exit, reentry;
                 RANGE_TYPE lon_range, lat_range;
#endif
{
   if (exit.lon <= lon_range.min)
   {
      if (reentry.lon <= lon_range.min) return (LEFT);
      if (reentry.lon >= lon_range.max) return (HORIZONTAL);
      if (reentry.lat <= lat_range.min) return (BOTTOM_LEFT);
      if (reentry.lat >= lat_range.max) return (TOP_LEFT);
   }
   if (exit.lon >= lon_range.max)
   {
      if (reentry.lon >= lon_range.max) return (RIGHT);
      if (reentry.lon <= lon_range.min) return (HORIZONTAL);
      if (reentry.lat <= lat_range.min) return (BOTTOM_RIGHT);
      if (reentry.lat >= lat_range.max) return (TOP_RIGHT);
   }
   if (exit.lat <= lat_range.min)
   {
      if (reentry.lat <= lat_range.min) return (BOTTOM);
      if (reentry.lat >= lat_range.max) return (VERTICAL);
      if (reentry.lon <= lon_range.min) return (BOTTOM_LEFT);
      if (reentry.lon >= lon_range.max) return (BOTTOM_RIGHT);
   }
   if (exit.lat >= lat_range.max)
   {
      if (reentry.lat >= lat_range.max) return (TOP);
      if (reentry.lat <= lat_range.min) return (VERTICAL);
      if (reentry.lon <= lon_range.min) return (TOP_LEFT);
      if (reentry.lon >= lon_range.max) return (TOP_RIGHT);
   }
   return (-1);
}

#if PROTOTYPE_ALLOWED
void connect(FILE * fpout, 
             LONLAT_TYPE from, LONLAT_TYPE to,
             RANGE_TYPE lon_range, RANGE_TYPE lat_range,
             char delimiter)
#else
void connect(fpout, from, to, lon_range, lat_range, delimiter)
   FILE *fpout;
   LONLAT_TYPE from, to;
   RANGE_TYPE lon_range, lat_range;
   char delimiter;
#endif
{
   LONLAT_TYPE from_i, to_i;
   RANGE_TYPE lat_range_i, lon_range_i;

   if (equal(from.lon, to.lon) & equal(from.lat, to.lat))
      return;			/* connected */

   from_i.lat = LatToIn(from.lat);
   from_i.lon = LonToIn(from.lon);
   to_i.lat = LatToIn(to.lat);
   to_i.lon = LonToIn(to.lon);
   lat_range_i.min = LatToIn(lat_range.min);
   lat_range_i.max = LatToIn(lat_range.max);
   lon_range_i.min = LonToIn(lon_range.min);
   lon_range_i.max = LonToIn(lon_range.max);

   switch (axis_crossed(from, to, lon_range, lat_range))
   {
      case RIGHT:
      case LEFT:
      case TOP:
      case BOTTOM:
         fprintf(fpout, "%9.4f %9.4f %c\n", to_i.lon, to_i.lat, delimiter);
         break;
      case TOP_RIGHT:
         fprintf(fpout, "%9.4f %9.4f %c %9.4f %9.4f %c\n",
            lon_range_i.max, lat_range_i.max, delimiter, to_i.lon, to_i.lat, delimiter);
         break;
      case BOTTOM_RIGHT:
         fprintf(fpout, "%9.4f %9.4f %c %9.4f %9.4f %c\n",
            lon_range_i.max, lat_range_i.min, delimiter, to_i.lon, to_i.lat, delimiter);
         break;
      case TOP_LEFT:
         fprintf(fpout, "%9.4f %9.4f %c %9.4f %9.4f %c\n",
            lon_range_i.min, lat_range_i.max, delimiter, to_i.lon, to_i.lat, delimiter);
         break;
      case BOTTOM_LEFT:
         fprintf(fpout, "%9.4f %9.4f %c %9.4f %9.4f %c\n",
            lon_range_i.min, lat_range_i.min, delimiter, to_i.lon, to_i.lat, delimiter);
         break;
      case VERTICAL:		/* go toward nearer y-axis */
         if (fabs(to.lon - lon_range.min) < fabs(to.lon - lon_range.max))
            fprintf(fpout,
               "%9.4f %9.4f %c %9.4f %9.4f %c %9.4f %9.4f %c\n",
               lon_range_i.min, from_i.lat, delimiter, lon_range_i.min, to_i.lat,
               delimiter, to_i.lon, to_i.lat, delimiter);
         else
            fprintf(fpout,
               "%9.4f %9.4f %c %9.4f %9.4f %c %9.4f %9.4f %c\n",
               lon_range_i.max, from_i.lat, delimiter, lon_range_i.max, to_i.lat,
               delimiter, to_i.lon, to_i.lat, delimiter);
         break;
      case HORIZONTAL:		/* go toward nearer x-axis */
         if (fabs(to.lat - lat_range.min) < fabs(to.lat - lat_range.max))
            fprintf(fpout,
               "%9.4f %9.4f %c %9.4f %9.4f %c %9.4f %9.4f %c\n",
               from_i.lon, lat_range_i.min, delimiter, to_i.lon, lat_range_i.min,
               delimiter, to_i.lon, to_i.lat, delimiter);
         else
            fprintf(fpout,
               "%9.4f %9.4f %c %9.4f %9.4f %c %9.4f %9.4f %c\n",
               from_i.lon, lat_range_i.max, delimiter, to_i.lon, lat_range_i.max,
               delimiter, to_i.lon, to_i.lat, delimiter);
         break;
      default:
         fprintf(fpout, "%9.4f %9.4f M\n", to_i.lon, to_i.lat);
         break;
   }				/* end switch */
}

#define STACKSIZE 50
LONLAT_TYPE stack[STACKSIZE];
int stack_index = -1;

#if PROTOTYPE_ALLOWED
int push(LONLAT_TYPE lonlat)
#else
int push(lonlat)
   LONLAT_TYPE lonlat;
#endif
{
   if (++stack_index >= STACKSIZE)
   {
      fputs("\n ERROR: Stack overflow in push(lonlat).\n", stderr);
      return (1);
   }
   stack[stack_index] = lonlat;
   return (0);
}

#if PROTOTYPE_ALLOWED
LONLAT_TYPE *pop(void)
#else
LONLAT_TYPE *pop()
#endif
{
   if (stack_index < 0)
      return (NULL);
   return (&stack[stack_index--]);
}
