static char *sccs_id= (sccs_id, "@(#)FloatShape.c	1.2 7/28/92");

/*
 * This file implements a GeoShape which draws maps which
 * are created with the rtree/makefloats script.
 */

#include "Class.h"
#include "OverView.h"
#include "IconShape.h"


class FloatShape: public PlineShape {
public:
  FloatShape(MapView *, int n, char **str);
};


extern "C" FloatShape *make_float_shape();

FloatShape *make_float_shape(MapView *dv, int n, char **str)
{
  FloatShape *s= new FloatShape(dv, n, str);
  return s;
}


inline char *get_fl_field(char **pp)
{
    static char		nstr[100];
    register char	*p= *pp;
    register char	*n;

    for (n= nstr; *p != ',' && *p != '}' && *p; p++)
	*n++= *p;
    if (*p)
	p++;
    *pp= p;
    *n= '\0';
    return nstr;
}

FloatShape::FloatShape(MapView *d, int n, char **str): (d, 0)
{
    char	*p= ++(str[2]);

    closed= str[1][0] == 't';
    npoints= atoi(str[0]);

    if (!npoints) {
	pts= 0;
	return;
    }

    register int	i;
    register int	np= 0;
    Point		lastp, newp;
    // Prevent useless constructor call:
    char		pts_data[10000 * sizeof(Point)];
    register Point	*tpts= (Point*) pts_data;

    for (i= 0; i < npoints && np < 10000; i++) {
      float Long= atof(get_fl_field(&p));
      float Lat= atof(get_fl_field(&p));
      newp= dv->GeoToPoint(Long,Lat);
      if (newp == BADPOINT) {
	np= 1;
	break;
      }
      if (np == 0 || newp != lastp) {
        tpts[np]= lastp= newp;
	np++;
      }

      if (i == 0) {
	start_long= Long;
	start_lat= Lat;
      } else if (i == npoints - 1) {
	end_long= Long;
	end_lat= Lat;
      }
    }

    Rectangle bbox;
    if (np == 1) {
      npoints= 0;
      pts= 0;
      bbox= Rectangle(tpts[0], gPoint0);
    } else {
      npoints= np;
      // Prevent useless constructor call:
      pts= (Point*) new char[sizeof(Point) * npoints];
      bcopy(tpts, pts, npoints * sizeof(Point));
      bbox= NormRect(pts[0],pts[npoints-1]);
    }

    labelpos= bbox.Center();
    width= 1;
}
