#include <graphics/display.h>
#include <libraries/dosextens.h>
#include <libraries/diskfont.h>
#include <exec/exec.h>
#include <intuition/intuitionbase.h>
#include <graphics/regions.h>
#include <devices/keymap.h>
#include <stdio.h>
#include <workbench/startup.h>
#include <graphics/gfxmacros.h>
#include <graphics/gfxbase.h>
#include <math.h>

#include "diskicon.h"
#include "struct.h"
#include "plotlim.h"
#include "ffp.h"


extern int MAXVERT;
extern int MAXHORIZ;


extern int GetDat();
extern void plot();
extern struct Screen *screen;
extern struct NewScreen newscreen;
extern struct NewWindow newwindow;
extern struct Window *window;
extern int XMINP;      /* LMARGIN */
extern int YMAXP;   /* MAXVERT - TMARGIN */
extern int YMINP;    /* BMARGIN */
extern int XMAXP;



#define QUIT 0
#define GO 1
extern int QuitFlag;

extern long IconBase;

extern FFP xtic[MAXTICS],ytic[MAXTICS];
extern short xticp[MAXTICS],yticp[MAXTICS];
extern char filename[150];
extern struct RastPort *p;
extern struct ViewPort *vp;

#define P_X_TIC_SIZE X_TIC_SIZE*(P_YREG/MAXVERT)
#define P_Y_TIC_SIZE Y_TIC_SIZE*(P_XREG/MAXHORIZ)
#define P_ERR_DELIM ERR_DELIM*(P_XREG/MAXHORIZ)
#define P_CH_WID 100
#define P_CH_HEI 150

int P_LMARGIN;
int P_BMARGIN;
int P_TMARGIN;
int P_RMARGIN;
int P_YREG;
int P_XREG;

#define MCAD_ERR_DELIM ERR_DELIM*(

static FFP XScale, YScale, XOffset, YOffset;

void fWrtFFP(fp, ffpval)
FILE *fp;
FFP ffpval;
{
   fprintf(fp, "%d", (int)ffpval);
}


void WrtAxes(fp, Tics, Grid, XLabel,YLabel,Title)
FILE *fp;
struct Tics *Tics;
short Grid;
char *XLabel;
char *YLabel;
char *Title;

{
   short n, tmpx, tmpy, i;
   FFP *x, *y;
   char tmpstr[20];

   /*** DRAW TICS/GRID ***/
   fprintf(fp,"SP2;");
   x=Tics->x; y=Tics->y;
   for (i=0; i < Tics->NX; i++) {
      if (Grid) {
         fprintf(fp,"PU"); fWrtFFP(fp,(XScale * (XOffset + x[i])));
         fprintf(fp," %d;PD",P_BMARGIN);
         fWrtFFP(fp,(XScale * (XOffset + x[i])));
         fprintf(fp," %d;\n",P_YREG);
      }
      else {
         fprintf(fp,"PU"); fWrtFFP(fp,(XScale * (XOffset + x[i])));
         fprintf(fp," %d;PD",P_BMARGIN);
         fWrtFFP(fp,(XScale * (XOffset + x[i])));
         fprintf(fp," %d;",P_BMARGIN + P_X_TIC_SIZE);
         fprintf(fp,"PU"); fWrtFFP(fp,(XScale * (XOffset + x[i])));
         fprintf(fp," %d;PD",P_YREG);
         fWrtFFP(fp,(XScale * (XOffset + x[i])));
         fprintf(fp," %d;\n",P_YREG - P_X_TIC_SIZE);
      }
   }
   for (i=0; i < Tics->NY; i++) {
      if (Grid) {
         fprintf(fp,"PU%d ",P_LMARGIN);
         fWrtFFP(fp,(YScale * (YOffset + y[i])));
         fprintf(fp,"PD%d ",P_XREG);
         fWrtFFP(fp,(YScale * (YOffset + y[i])));
         fprintf(fp,";\n");
      }
      else {
         fprintf(fp,"PU%d ",P_LMARGIN);
         fWrtFFP(fp,(YScale * (YOffset + y[i])));
         fprintf(fp,";PD%d ", P_LMARGIN + P_Y_TIC_SIZE);
         fWrtFFP(fp,(YScale * (YOffset + y[i])));
         fprintf(fp,";");
         fprintf(fp,"PU%d ",P_XREG);
         fWrtFFP(fp,(YScale * (YOffset + y[i])));
         fprintf(fp,";PD%d ", P_XREG - P_Y_TIC_SIZE);
         fWrtFFP(fp,(YScale * (YOffset + y[i])));
         fprintf(fp,";\n");
      }
   }

   /*** DRAW BORDER ***/
   fprintf(fp,"SP1;PU%d %d;",P_LMARGIN,P_BMARGIN);
   fprintf(fp,"PD%d %d %d %d %d %d %d %d;\n",P_XREG,P_BMARGIN,P_XREG,P_YREG,
   P_LMARGIN,P_YREG,P_LMARGIN,P_BMARGIN);


   /*** PRINT TIC VALUES ***/
   fprintf(fp,"SP1;");
   for (i=0; i < Tics->NX; i++) {
      n=GetString(&tmpstr,Tics->x[i]);
      tmpx = (short)((XScale * (XOffset + Tics->x[i])));
      tmpx -= (((n+1)/2)-.5) * P_CH_WID;
      tmpy = P_BMARGIN - (2*P_CH_HEI) +1;
      fprintf(fp, "PU%d %d;", tmpx,tmpy);
      fprintf(fp,"LB%s\003", tmpstr);
   }
   /* Write X Label */
   if (XLabel!=NULL){
      tmpx = 0.5 * P_XREG;
      tmpx -= (((strlen(XLabel)+1)/2)-.5) * P_CH_WID;
      tmpy= 1;
      fprintf(fp, "PU%d %d;", tmpx,tmpy);
   }  fprintf(fp,"LB%s\003", XLabel);

   /* Write Title */
   if (Title!=NULL) {
      tmpx = 0.5 * P_XREG;
      tmpx -= (((strlen(Title)+1)/2)-.5) * P_CH_WID;
      tmpy= P_YREG + P_CH_HEI - 1;
      fprintf(fp, "PU%d %d;", tmpx,tmpy);
      fprintf(fp,"LB%s\003", Title);
   }

   for (i=0; i < Tics->NY; i++) {
      n=GetString(&tmpstr,Tics->y[i]);
      tmpy = (short)((YScale * (YOffset + Tics->y[i])));
      tmpy -= .5 * P_CH_WID;
      tmpx = P_LMARGIN-( (n+1) * P_CH_WID );
      fprintf(fp, "PU%d %d;",tmpx, tmpy);
      fprintf(fp,"LB%s\003", tmpstr);
   }
   /* Write YLabel */
   if (YLabel!=NULL) {
      fprintf(fp,"SR%f %f;\n",
         (float)(100*P_CH_WID*4/3) / (P_XREG - P_LMARGIN - P_RMARGIN),
         (float)(100*P_CH_HEI*3/4) / (P_YREG - P_BMARGIN));
      tmpx = 1;
      tmpy= 0.5 * P_YREG;
      tmpy -= strlen(YLabel) * P_CH_WID ;
      fprintf(fp, "DR0,1; PU%d %d;", tmpx,tmpy);
      fprintf(fp,"LB%s\003", YLabel);
   }

}

extern USHORT chip WaitSprite[];
extern char *stpchr();

WrtPlt(Pict,Dest)
USHORT Dest;
struct Pict *Pict;
{
struct Process  *OurTask;
struct Window   *old_pr_WindowPtr;
FILE *fp;
short i,n=3;
struct Plot *Plot;
FFP *x, *y, *e;
short xptsiz, yptsiz;
static char plotname[150], def_drive[150]="PLT:", def_path[100], def_node[30],def_extn[20];
static char PlotNum = 0;
void PlotBox(), PlotTri(), PlotEggs(), PlotPlus(), PlotKite();
struct Tics *Tics;
char tmpstr[20];

   if (Dest)
      {
          strcpy(plotname,filename);
          strsfn(plotname,def_drive,def_path,def_node,def_extn);
          strcat(def_drive,def_path);
          strcat(def_node,".plt");
          strcat(def_node,PlotNum);

          OurTask = (struct Process *)FindTask(0L);
          old_pr_WindowPtr = (struct Window *)OurTask->pr_WindowPtr;
          OurTask->pr_WindowPtr = (APTR)window;
          if (get_fname(window,screen,"Save File As...",def_node,def_drive)==NULL)
               {
                  OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
                  return(TRUE);
                }

           OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
           strmfp(plotname,def_drive,def_node);
           PutDiskObject(plotname,&IconDiskObject);
       }
   else
       {
          if (!(fp = fopen("l:plt-handler","r") ))
             {
                Message("Printing needs the PLT: Device"); return(FALSE);
             }
          fclose(fp);

          Execute("c:failat >Nil: <Nil: 20 \n c:mount >Nil: <Nil: plt:",0,0);
          strcpy(plotname,"PLT:");
       }
   if (!(fp = fopen(plotname,"w") ))
      {Message("  Can't write file to PLT:"); return(FALSE);}
   SetPointer(window,WaitSprite,26,14,-4,-4);
   Tics = Pict->Tics;
   for (i=0; i < Tics->NY; i++) { n=max(n,GetString(&tmpstr,Tics->y[i]));}
   P_LMARGIN=(n+2)*P_CH_WID;
   if (Pict->YLabel!=NULL) P_LMARGIN=P_LMARGIN+P_CH_HEI;
   P_XREG = 10000;
   if (Pict->XLabel!=NULL) P_BMARGIN = P_CH_HEI*4;
   else P_BMARGIN = P_CH_HEI*2;
   if (Pict->Title!=NULL) P_TMARGIN = P_CH_HEI*2;
   else P_TMARGIN=0;
   P_RMARGIN = 0;
   P_YREG = 10000-P_TMARGIN;

   fprintf(fp,"IN;SC 0 %d 0 %d;\n", P_XREG+100, P_YREG+100+P_TMARGIN);
   fprintf(fp,"VS 20.0;\n");  /* VELOCITY cm/s */
   fprintf(fp,"\033.N;19:\033.I81;;17:\n"); /* XON/XOFF */
   XScale = (FFP)(P_XREG - P_LMARGIN - P_RMARGIN) /
            (Pict->CurrReg->XMax - Pict->CurrReg->XMin);
   YScale = (FFP)(P_YREG - P_BMARGIN) /
            (Pict->CurrReg->YMax - Pict->CurrReg->YMin);
   XOffset = (FFP)(P_LMARGIN) / XScale - Pict->CurrReg->XMin;
   YOffset = (FFP)(P_BMARGIN) / YScale - Pict->CurrReg->YMin;

   Plot = Pict->Plot;
   while (Plot) {
      if (Plot->Enabled) {
         fprintf(fp,"PU;SP%d;\n", 1+Plot->Color-PLOTCOLORBASE);

         /* PLOT LINES */
         if (Plot->Lines != FALSE) {
            x=Plot->x; y=Plot->y;
            fprintf(fp, "PU"); fWrtFFP(fp,(XScale * (XOffset + *x)));
            fprintf(fp," ");
            fWrtFFP(fp,(YScale * (YOffset + *y))); fprintf(fp,";");
            x++; y++;
            fprintf(fp, "PD");
            for (i=1; i<Plot->NPts; i++, x++, y++) {
               if (i>1) fprintf(fp, " ");
               fWrtFFP(fp,(XScale * (XOffset + *x)));
               fprintf(fp," ");
               fWrtFFP(fp,(YScale * (YOffset + *y)));
            }
            fprintf(fp,";\n");
         }

         /* PLOT POINTS */
         if (Plot->PointSize != 0) {
            xptsiz = abs(Plot->PointSize) * (P_XREG / 1100);
            yptsiz = abs(Plot->PointSize) * (P_YREG / 850);
            x=Plot->x; y=Plot->y;
            for (i=0; i<Plot->NPts; i++, x++, y++) {
               fprintf(fp,"PU");
               fWrtFFP(fp,(XScale * (XOffset + *x)));
               fprintf(fp," ");
               fWrtFFP(fp,(YScale * (YOffset + *y)));
               fprintf(fp,";");
               switch (Plot->PointType) {
               case 1:  PlotTri(fp,xptsiz/2,yptsiz/2);  break;
               case 2:  PlotKite(fp,xptsiz/2,yptsiz/2); break;
               case 3:  PlotEggs(fp,xptsiz/2,yptsiz/2); break;
               case 4:  PlotEggs(fp,xptsiz/2,yptsiz/2); /* Fall through (fancy, huh?) */
               case 5:  PlotPlus(fp,xptsiz/2,yptsiz/2); break;
               case 6:
               default: PlotBox(fp,xptsiz/2,yptsiz/2); break;
               }
            }
         }

         /* PLOT ERROR BARS */
         if (Pict->ShowErr) {
            x=Plot->x; y=Plot->y; e=Plot->e;
            for (i=0; i<Plot->NPts; i++, x++, y++, e++) {
               if (*e > FFPSMALL)
                 {
                    fprintf(fp,"PU");
                    fWrtFFP(fp,(XScale * (XOffset + *x)));
                    fprintf(fp," ");
                    fWrtFFP(fp,(YScale * (YOffset + (*y - *e))));
                    fprintf(fp,"; PD");
                    fWrtFFP(fp,(XScale * (XOffset + *x)));
                    fprintf(fp," ");
                    fWrtFFP(fp,(YScale * (YOffset + (*y + *e))));
                    fprintf(fp,";\n");

                    /* PLOT DELIMITERS ON ERROR BARS */

                    fprintf(fp,"PU");
                    fWrtFFP(fp,(XScale * (XOffset + *x) - P_ERR_DELIM));
                    fprintf(fp," ");
                    fWrtFFP(fp,(YScale * (YOffset + (*y - *e))));
                    fprintf(fp,"; PD");
                    fWrtFFP(fp,(XScale * (XOffset + *x)+ P_ERR_DELIM));
                    fprintf(fp," ");
                    fWrtFFP(fp,(YScale * (YOffset + (*y - *e))));
                    fprintf(fp,";\n");
                    fprintf(fp,"PU");
                    fWrtFFP(fp,(XScale * (XOffset + *x) - P_ERR_DELIM));
                    fprintf(fp," ");
                    fWrtFFP(fp,(YScale * (YOffset + (*y + *e))));
                    fprintf(fp,"; PD");
                    fWrtFFP(fp,(XScale * (XOffset + *x)+ P_ERR_DELIM));
                    fprintf(fp," ");
                    fWrtFFP(fp,(YScale * (YOffset + (*y + *e))));
                    fprintf(fp,";\n");
                 }
            }
         }
      }
      Plot = Plot->NextPlot;
   }

   fprintf(fp,"SR%f %f;\n",
         (float)(100*P_CH_WID*3/4) / (P_XREG),
         (float)(100*P_CH_HEI) / (P_YREG));
   WrtAxes(fp, Pict->Tics, Pict->Grid, Pict->XLabel, Pict->YLabel, Pict->Title);

   fprintf(fp, "SP;");
   (void) fclose(fp);
   PlotNum++;
   ClearPointer(window);
   return(TRUE);
}




void header(n, c, t, fp)
int n, c, t;
FILE *fp;
{
   UBYTE bdummy;
   bdummy = n;
   fwrite((char *)&bdummy, 1, 1, fp);
   bdummy = c;
   fwrite((char *)&bdummy, 1, 1, fp);
   bdummy = 0;
   fwrite((char *)&bdummy, 1, 1, fp); /* LineType */
   bdummy = t;
   fwrite((char *)&bdummy, 1, 1, fp);
   bdummy = 0;
   fwrite((char *)&bdummy, 1, 1, fp); /* padding for future use */
   fwrite((char *)&bdummy, 1, 1, fp);
}

To_mCAD(Pict,Dest)
struct Pict *Pict;
USHORT Dest;
{

   struct Process  *OurTask;
   struct Window   *old_pr_WindowPtr;

   FILE *fp;
   short i;
   struct Plot *Plot;
   FFP *x, *y, *e, xtmp, ytmp, xtmp1, ytmp1;
   FFP xptsiz, yptsiz, XReg, YReg, XScale, XOffset;
   char plotname[150], def_drive[150], def_path[100], def_node[30],def_extn[20];
   static char mPlotNum = 0;
   void CADBox(), CADKite(), CADTri(), CADEggs(), CADPlus();

  if (Dest)
    {
     strcpy(plotname,filename);
     strsfn(plotname,def_drive,def_path,def_node,def_extn);
     strcat(def_drive,def_path);
     strcat(def_node,".txt");
     strcat(def_node,mPlotNum);

           OurTask = (struct Process *)FindTask(0L);
           old_pr_WindowPtr = (struct Window *)OurTask->pr_WindowPtr;
           OurTask->pr_WindowPtr = (APTR)window;
           if (get_fname(window,screen,"Save File As...",def_node,def_drive)==NULL)
               {
                  OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
                  return(TRUE);
                }

     OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
     strmfp(plotname,def_drive,def_node);
     PutDiskObject(plotname,&IconDiskObject);
    }
   else
       {
          if (!(fp = fopen("T:tempfile","w") ))
             {
                Message("Save needs the T: Device"); return(FALSE);
             }
          fclose(fp);
          strcpy(plotname,"T:tempfile");
       }

   if (!(fp = fopen(plotname,"w") ))
      {Message("   Can't write tempory file  "); return(FALSE);}

   XReg = Pict->CurrReg->XMax - Pict->CurrReg->XMin;
   YReg = Pict->CurrReg->YMax - Pict->CurrReg->YMin;
   XScale = YReg/XReg * 1.4434; XOffset = Pict->CurrReg->XMin;

   Plot = Pict->Plot;
   while (Plot) {
      if (Plot->Enabled) {

         /* PLOT LINES */
         if (Plot->Lines != FALSE) {
            x=Plot->x; y=Plot->y;
            for (i=0; i<Plot->NPts; i++, x++, y++) {
               xtmp = XScale * (*x - XOffset);
               fprintf(fp, "%f %f\n", xtmp, *y);
            }
            fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);
         }

         /* PLOT POINTS */
         if (Plot->PointSize != 0) {
            xptsiz = abs(Plot->PointSize) * (XReg / 582.);
            xptsiz = XScale * xptsiz;
            yptsiz = abs(Plot->PointSize) * (YReg / 360.);
            x=Plot->x; y=Plot->y;
            for (i=0; i<Plot->NPts; i++, x++, y++) {
               switch (Plot->PointType) {
               case 1:  CADTri(fp,Plot,(XScale * (*x - XOffset)),*y,xptsiz/2,yptsiz/2); break;
               case 2:  CADKite(fp,Plot,(XScale * (*x - XOffset)),*y,xptsiz/2,yptsiz/2); break;
               case 3:  CADEggs(fp,Plot,(XScale * (*x - XOffset)),*y,xptsiz/2,yptsiz/2); break;
               case 4:  CADEggs(fp,Plot,(XScale * (*x - XOffset)),*y,xptsiz/2,yptsiz/2);
               case 5:  CADPlus(fp,Plot,(XScale * (*x - XOffset)),*y,xptsiz/2,yptsiz/2); break;
               case 6:
               default: CADBox(fp,Plot,(XScale * (*x - XOffset)),*y,xptsiz/2,yptsiz/2); break;
               }
            }
         }

         /* PLOT ERROR BARS */
         if (Pict->ShowErr) {
            x=Plot->x; y=Plot->y; e=Plot->e;
            xtmp1=XScale*(ERR_DELIM*(XReg/582));     /* SCALED ERROR DELIMITER */
            for (i=0; i<Plot->NPts; i++, x++, y++, e++) {
              if (*e > FFPSMALL)
                {
                   xtmp = XScale * (*x - XOffset);
                   ytmp = *y - *e;
                   fprintf(fp,"%f %f\n",xtmp-xtmp1,ytmp);
                   fprintf(fp,"%f %f\n*>\n*C %d\n\n",xtmp+xtmp1,ytmp,1+Plot->Color-PLOTCOLORBASE);
                   fprintf(fp,"%f %f\n", xtmp, ytmp);
                   ytmp = *y + *e;
                   fprintf(fp,"%f %f\n", xtmp, ytmp);
                   fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);
                   fprintf(fp,"%f %f\n",xtmp-xtmp1,ytmp);
                   fprintf(fp,"%f %f\n*>\n*C %d\n\n",xtmp+xtmp1,ytmp,1+Plot->Color-PLOTCOLORBASE);
                }
            }
         }
      }
      Plot = Plot->NextPlot;
   }

   /*** DRAW TICS/GRID ***/
   x = Pict->Tics->x; y = Pict->Tics->y;
   for (i=0; i < Pict->Tics->NX; i++) {
      xtmp = XScale * (x[i] - XOffset);
      if (Pict->Grid) {
         PToU(Pict, 0, YMINP, &xtmp1, &ytmp);
         PToU(Pict, 0, YMAXP, &xtmp1, &ytmp1);
         fprintf(fp,"%f %f\n%f %f\n*>\n*C 2\n\n", xtmp, ytmp, xtmp, ytmp1);
      }
      else {
         PToU(Pict, 0, YMINP, &xtmp1, &ytmp);
         fprintf(fp,"%f %f\n", xtmp, ytmp);
         PToU(Pict, 0, YMINP+X_TIC_SIZE, &xtmp1, &ytmp1);
         fprintf(fp,"%f %f\n*>\n*C 3\n\n", xtmp, ytmp1);
         PToU(Pict, 0, YMAXP, &xtmp1, &ytmp);
         fprintf(fp,"%f %f\n", xtmp, ytmp);
         PToU(Pict, 0, YMAXP-X_TIC_SIZE, &xtmp1, &ytmp1);
         fprintf(fp,"%f %f\n*>\n*C 3\n\n", xtmp, ytmp1);
      }
   }
   for (i=0; i < Pict->Tics->NY; i++) {
      if (Pict->Grid) {
         PToU(Pict, XMINP, YMINP, &xtmp, &ytmp);
         xtmp = XScale * (xtmp - XOffset);
         PToU(Pict, XMAXP, YMAXP, &xtmp1, &ytmp1);
         xtmp1 = XScale * (xtmp1 - XOffset);
         fprintf(fp,"%f %f\n%f %f\n*>\n*C 2\n\n", xtmp, y[i], xtmp1, y[i]);
      }
      else {
         PToU(Pict, XMINP, 0, &xtmp, &ytmp);
         xtmp = XScale * (xtmp - XOffset);
         fprintf(fp,"%f %f\n", xtmp, y[i]);
         PToU(Pict, XMINP+Y_TIC_SIZE, 0, &xtmp1, &ytmp1);
         xtmp1 = XScale * (xtmp1 - XOffset);
         fprintf(fp,"%f %f\n*>\n*C 3\n\n", xtmp1, y[i]);
         PToU(Pict, XMAXP, 0, &xtmp, &ytmp);
         xtmp = XScale * (xtmp - XOffset);
         fprintf(fp,"%f %f\n", xtmp, y[i]);
         PToU(Pict, XMAXP-Y_TIC_SIZE, 0, &xtmp1, &ytmp1);
         xtmp1 = XScale * (xtmp1 - XOffset);
         fprintf(fp,"%f %f\n*>\n*C 3\n\n", xtmp1, y[i]);
      }
   }

   /*** PRINT TIC VALUES ***/
/*
*   for (i=0; i < Pict->Tics->NX; i++) {
*      n = sprintf(tmpstr, "%-.4f", Pict->Tics->x[i]);
*      n = min(n, 7);
*      while (tmpstr[n-1] == '0') n--;
*      tmpstr[n]=0;
*   }
*   for (i=0; i < Pict->Tics->NY; i++) {
*      n = sprintf(tmpstr, "%-.4f", Pict->Tics->y[i]);
*      n = min(n, 7);
*      while (tmpstr[n-1] == '0') n--;
*      tmpstr[n]=0;
*   }
*/

   /*** DRAW AXES ***/
   PToU(Pict, XMINP, YMINP, &xtmp, &ytmp);
   xtmp = XScale * (xtmp - XOffset);
   PToU(Pict, XMAXP, YMAXP, &xtmp1, &ytmp1);
   xtmp1 = XScale * (xtmp1 - XOffset);
   fprintf(fp, "%f %f\n", xtmp, ytmp);
   fprintf(fp, "%f %f\n", xtmp1, ytmp);
   fprintf(fp, "%f %f\n", xtmp1, ytmp1);
   fprintf(fp, "%f %f\n", xtmp, ytmp1);
   fprintf(fp, "%f %f\n", xtmp, ytmp);
   fprintf(fp,"*C 1\n\n");

   (void) fclose(fp);
   mPlotNum++;
   return(TRUE);
}


void PlotPlus(fp,x,y)
FILE *fp;
SHORT x,y;
{
   fprintf(fp,"PR %d 0;",-x);
   fprintf(fp,"PD %d 0;",2*x);
   fprintf(fp,"PU %d %d;",-x,-y);
   fprintf(fp,"PD 0 %d;PA;",2*y);
}

void PlotBox(fp,x,y)
FILE *fp;
SHORT x,y;
{
   fprintf(fp,"PR%d %d;", -x, -y);
   fprintf(fp,"PD%d 0 0 %d %d 0 0 %d;PA;\n",2*x,2*y, -2*x, -2*y);
}

void PlotEggs(fp,x,y)
FILE *fp;
SHORT x,y;
{
   fprintf(fp,"PR %d %d;",-x,-y);
   fprintf(fp,"PD %d %d;",2*x,2*y);
   fprintf(fp,"PU 0 %d;",-2*y);
   fprintf(fp,"PD %d %d;",-2*x,2*y);
   fprintf(fp,"PU %d %d;PA\n;",x,-y);
}


void PlotKite(fp,x,y)
FILE *fp;
SHORT x,y;
{
   fprintf(fp,"PR%d 0;",-x);
   fprintf(fp,"PD%d %d %d %d %d %d %d %d;PA;\n",x,y,x,-y,-x,-y,-x,y);
}


void PlotTri(fp,x,y)
FILE *fp;
SHORT x,y;
{
   fprintf(fp,"PR%d %d;", -x, -y);
   fprintf(fp,"PD%d %d %d %d %d 0;PA;\n",x,2*y,x,-2*y,-2*x);

}


void CADBox(fp,Plot,x,y,dx,dy)
FILE *fp;
struct Plot *Plot;
FFP x,y,dx,dy;
{
   x = x - dx;
   y = y - dy;
   fprintf(fp,"%f %f\n", x, y); x += 2*dx;
   fprintf(fp,"%f %f\n", x, y); y += 2*dy;
   fprintf(fp,"%f %f\n", x, y); x -= 2*dx;
   fprintf(fp,"%f %f\n", x, y); y -= 2*dy;
   fprintf(fp,"%f %f\n", x, y);
   fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);

}

void CADPlus(fp,Plot,x,y,dx,dy)
FILE *fp;
struct Plot *Plot;
FFP x,y,dx,dy;
{
   x = x - dx;
   fprintf(fp,"%f %f\n", x, y); x += 2*dx;
   fprintf(fp,"%f %f\n", x, y);
   fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);

   x -= dx; y -= dy;
   fprintf(fp,"%f %f\n", x, y); y += 2*dy;
   fprintf(fp,"%f %f\n", x, y);
   fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);
}

void CADEggs(fp,Plot,x,y,dx,dy)
FILE *fp;
struct Plot *Plot;
FFP x,y,dx,dy;
{
   x = x - dx; y = y - dy;
   fprintf(fp,"%f %f\n", x, y); x += 2*dx; y += 2*dy;
   fprintf(fp,"%f %f\n", x, y);
   fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);

   y -= 2*dy;
   fprintf(fp,"%f %f\n", x, y); x -= 2*dx; y += 2*dy;
   fprintf(fp,"%f %f\n", x, y);
   fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);
}


void CADKite(fp,Plot,x,y,dx,dy)
FILE *fp;
struct Plot *Plot;
FFP x,y,dx,dy;
{
   x = x - dx;
   fprintf(fp,"%f %f\n", x, y); x += dx; y += dy;
   fprintf(fp,"%f %f\n", x, y); x += dx; y -= dy;
   fprintf(fp,"%f %f\n", x, y); x -= dx; y -= dy;
   fprintf(fp,"%f %f\n", x, y); x -= dx; y += dy;
   fprintf(fp,"%f %f\n", x, y);
   fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);
}

void CADTri(fp,Plot,x,y,dx,dy)
FILE *fp;
struct Plot *Plot;
FFP x,y,dx,dy;
{
   x = x - dx;
   y = y - dy;
   fprintf(fp,"%f %f\n", x, y); x += dx; y += 2*dy;
   fprintf(fp,"%f %f\n", x, y); x += dx; y -= 2*dy;
   fprintf(fp,"%f %f\n", x, y); x -= 2*dy;
   fprintf(fp,"%f %f\n", x, y);
   fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);
}

