#include "struct.h"
#include "plot.h"
#include <graphics/gfxmacros.h>

extern int debug;
extern FFP fround();

/**********************/
void GetDataLimits(Pict)
struct Pict *Pict;
{
   short i;
   FFP *x, *y, *e;
   struct Plot *Plot;
   FFP xmin, xmax, ymin, ymax;

   xmin = (ymin = FFPLARGE);
   xmax = (ymax = -FFPLARGE);
   
   Plot = Pict->Plot;
   while (Plot) {
      if (Plot->Enabled) {
         i = 0; x = Plot->x; y = Plot->y;
         if (Pict->ShowErr) e = Plot->e;
         while (i++ < Plot->NPts) {
            xmax = max(*x,xmax);
            xmin = min(*x,xmin);
            x++;
            if (Pict->ShowErr) {
               ymax = max((*y + *e), ymax);
               ymin = min((*y - *e), ymin);
               y++; e++;
            }
            else {
               ymax = max(*y,ymax);
               ymin = min(*y,ymin);
               y++;
            }
         }
      }
      Plot = Plot->NextPlot;
   }

   if (!Pict->XRegionLock)
      {Pict->CurrReg->XMax = xmax; Pict->CurrReg->XMin = xmin;}
   if (!Pict->YRegionLock)
      {Pict->CurrReg->YMax = ymax; Pict->CurrReg->YMin = ymin;}
}


/*****************************************/
void AdjustForTics(dmin, dmax, ntics, tics)
FFP *dmin, *dmax, *tics;
short ntics;
{
   FFP tmp, fract, f_ntics;
   short i;
   
   f_ntics = (FFP)(ntics);
   fract = (FFP)(ntics+1) / f_ntics;
   
   /*** adjust data limits to make "nice" numbers for tic marks ***/
   tmp = *dmin - (*dmax - *dmin) / 5;
   *dmin = fround( *dmin, DOWN, tmp);
   tmp = (*dmax - *dmin) / f_ntics;
   tmp = fround( tmp, UP, (tmp * fract) );
   *dmax = *dmin + tmp * f_ntics;

   tics[0] = *dmin;
   if (debug) printf("... tic[0]=%f\n", tics[0]);
   for (i=1; i<ntics; i++) {
      tics[i] = tics[i-1] + tmp;
      if (debug) printf("... tic[%d]=%f\n", i, tics[i]);
   }
}


/**************/
void Scale(Pict)
struct Pict *Pict;
{
   struct Plot *Plot;
   struct PlotRegion *Reg;
   struct Tics *Tics;
   register short i, *xp, *yp;
   register FFP *x, *y, xmin, ymin, xscale, yscale;
   short *ep, err, err_sav = TRUE;
   FFP *e, xmax, ymax;
   
   if (debug) printf("Scale: entry\n");
   Reg = Pict->CurrReg;
   xmin = Reg->XMin; xmax = Reg->XMax;
   ymin = Reg->YMin; ymax = Reg->YMax;

   if (FFPSAME(xmax,xmin))
      {printf("Scale: xmax = xmin, xscale := 0.\n"); xscale = (FFP)0;}
   else 
      Pict->XScale = xscale = (FFP)(XMAXP-XMINP) / (xmax - xmin);
   if (FFPSAME(ymax,ymin))
      {printf("Scale: ymax = ymin, yscale := 0.\n"); yscale = (FFP)0;}
   else 
      Pict->YScale = yscale = (FFP)(YMAXP-YMINP) / (ymax - ymin);

#define NEW_X_REGION ( !FFPSAME(Reg->XMin,xmin) || !FFPSAME(Reg->XMax,xmax) )
#define NEW_Y_REGION ( !FFPSAME(Reg->YMin,ymin) || !FFPSAME(Reg->YMax,ymax) )

   /* DATA */
   Plot = Pict->Plot;
   err = Pict->ShowErr;
   while (Plot) {
      if (Plot->Enabled) {
         Reg = Plot->Reg;

         if (NEW_X_REGION) {
            x = Plot->x; xp = Plot->xp;
            for (i = 0; i < Plot->NPts; i++) {
               *xp++ = XMINP + (short)((*x++ - xmin) * xscale);
               if (debug) printf("*xp=%d\n", *(xp-1));
            }
            Reg->XMin = xmin; Reg->XMax = xmax;
         }

         if ( (NEW_Y_REGION) || (err_sav!=err) ) {
            y = Plot->y; yp = Plot->yp; e = Plot->e; ep = Plot->ep;
            for (i = 0; i < Plot->NPts; i++) {
               *yp++ = YMINP + (short)((*y++ - ymin) * yscale);
               if (debug) printf("*yp=%d", *(yp-1));
               if (err) {
                  *ep++ = (short)(*e++ * yscale);
                  if (debug) printf(", *ep=%d", *(ep-1));
               }
               if (debug) printf("\n");
            }
            Reg->YMin = ymin; Reg->YMax = ymax;
         }

      }
      Plot = Plot->NextPlot;
   }
   err_sav = err;

   /* TICS */
   Tics = Pict->Tics;
   for (i=0; i < Tics->NX; i++)
      Tics->xp[i] = XMINP + (short)((Tics->x[i] - xmin) * xscale);

   for (i=0; i < Tics->NY; i++)
      Tics->yp[i] = YMINP + (short)((Tics->y[i] - ymin) * yscale);

   if (debug) printf("Scale: entry\n");
}


/**********************/
void DrawAxes(Tics,Grid)
struct Tics *Tics; short Grid;
{
   short *xp, *yp, n, tmpx, tmpy, i;
   char tmpstr[20];
      
   /*** DRAW BORDER ***/
   SetAPen(rp,2); SetOPen(rp,1);
   PRectFill(XMINP-1,YMINP-1,XMAXP+1,YMAXP+1);
   PRectFill(XMINP,YMINP,XMAXP,YMAXP);

   /*** DRAW TICS/GRID ***/
   SetAPen(rp,3);
   xp=Tics->xp; yp=Tics->yp;
   if (Grid) SetRGB4(vp,3,0,15,15);
   for (i=1; i < Tics->NX; i++) {
      if (Grid) {
         PMove(xp[i],YMINP+1); PDraw(xp[i],YMAXP-1);
      }
      else {
         PMove(xp[i],YMINP+1); PDraw(xp[i],YMINP+X_TIC_SIZE);
         PMove(xp[i],YMAXP-1); PDraw(xp[i],YMAXP-X_TIC_SIZE);
      }
   }
   for (i=1; i < Tics->NY; i++) {
      if (Grid) {
         PMove(XMINP+1,yp[i]); PDraw(XMAXP-1,yp[i]);
      }
      else {
         PMove(XMINP+1,yp[i]); PDraw(XMINP+Y_TIC_SIZE,yp[i]);
         PMove(XMAXP-1,yp[i]); PDraw(XMAXP-Y_TIC_SIZE,yp[i]);
      }
   }

   /*** PRINT TIC VALUES ***/
   SetAPen(rp,1);
   for (i=0; i < Tics->NX; i++) {
      n = sprintf(&tmpstr, "%-.4f", Tics->x[i]);
      n = min(n, 7);
      while (tmpstr[n-1] == '0') n--;
      tmpx = Tics->xp[i] - ((n/2) - (FFP)1/2) * CHARWIDTH;
      tmpy = max(0, YMINP-2*CHARHEIGHT);
      PMove(tmpx, tmpy); Text(rp,tmpstr,n);
   }
   for (i=0; i < Tics->NY; i++) {
      n = sprintf(&tmpstr, "%-.4f", Tics->y[i]);
      n = min( n, (LMARGIN/CHARWIDTH)-1 );
      while (tmpstr[n-1] == '0') n--;
      tmpx = max(0, XMINP-(n+1)*CHARWIDTH);
      PMove(tmpx, Tics->yp[i]-CHARHEIGHT/2); Text(rp,tmpstr,n);
   }
}


/*****************/
void DrawPlot(Pict)
struct Pict *Pict;
{
   struct Plot *Plot;
   short *x, *y, *e, n, m, i;

   Plot = Pict->Plot;
   
   while (Plot) {
      if (Plot->Enabled) {
         SetAPen(rp, Plot->Color);
  
         /* PLOT POINTS */
         if (Plot->PointSize != 0) {
            n = Plot->PointSize;
            if (abs(n) > 2) {m = (abs(n)/2)-1; SetOPen(rp, Plot->Color);}
            x = Plot->xp; y = Plot->yp;
            for (i=0; i<Plot->NPts; i++, x++, y++) {
               switch (abs(n)) {
               case 0: break;
               case 1: PWritePixel(*x, *y); break;
               case 2: PWritePixel(*x, *y); PWritePixel(*x, *y+1); break;
               case 3: PRectFill(*x, *y, *x+1, *y+1); break;
               default: PRectFill(*x-m, *y-m, *x+m, *y+m); break;
               }
            }
         }
   
         /* PLOT LINES */
         if (Plot->PointSize <= 0) {
            PMove(*Plot->xp, *Plot->yp);
            x = &(Plot->xp[1]); y = &(Plot->yp[1]);
            for (i=1; i<Plot->NPts; i++, x++, y++) PDraw(*x, *y);
         }
         
         /* PLOT ERROR BARS */
         if (Pict->ShowErr) {
            x = Plot->xp; y = Plot->yp; e = Plot->ep;
            for (i=0; i < Plot->NPts; i++, x++, y++, e++) {
               PMove(*x, *y - *e); PDraw(*x, *y + *e);
            }
         }

      }
      Plot = Plot->NextPlot;
   }
}


/*************************************************************/
extern int CheckUser();
extern int GetHowTo();

void plot(Pict)
struct Pict *Pict;
{
   struct PlotRegion *Reg;
   struct Tics *Tics;
   int req = GETHOWTO;

   Tics = Pict->Tics;
   do {
      switch(req) {
      case GETHOWTO: GetHowTo(Pict); /* fall thru */
      case GETDATALIMITS: GetDataLimits(Pict);
      case REPLOT:
      default: ;
      }
      Reg = Pict->CurrReg;
      if (!Pict->XRegionLock)
         AdjustForTics(&Reg->XMin, &Reg->XMax, Tics->NX, Tics->x);
      if (!Pict->YRegionLock)
         AdjustForTics(&Reg->YMin, &Reg->YMax, Tics->NY, Tics->y);
      
      Scale(Pict);
      InitWind();
      if (Pict->Axes) DrawAxes(Pict->Tics,Pict->Grid);
      DrawPlot(Pict);
      req = CheckUser(Pict);
   } while (req != QUIT);
}


/***************************/
void PToU(Pict, xp, yp, x, y)
struct Pict *Pict;
short xp, yp;
FFP *x, *y;
{
   if (debug) printf("PToU: entry, xp=%d, yp=%d\n", xp, yp);
   *x = Pict->CurrReg->XMin + (FFP)(xp-XMINP) / Pict->XScale;
   *y = Pict->CurrReg->YMin + (FFP)(yp-YMINP) / Pict->YScale;
   if (debug) printf("PToU: exit, *x=%f, *y=%f\n", *x, *y);
}
