/* plot3d.c */
#include "tdp.h"
void plot3d()
{
   register short i;
   short pdir, ystart, yinc, xstart, xinc;
   short pass, color, color_start, color_inc;
   short nhpts, nvpts, hinc, vinc;
   int *xthis, *ythis, *xlast, *ylast;
   short i_phi, i_theta, ycross;
   
   calc();

   /*** SET PLOTTING ORDER, DIRECTION SO THAT HIDDEN LINE ALGORITHM WORKS ***/
   i_phi = (int)(phi/d2r); if (debug) printf("i_phi=%d\n",i_phi);
      /* assume, for the moment, that abs(theta) <= 90 degrees */
   if (i_phi < -135)
      {xstart=0; ystart=nypts-1; pdir=-1; ycross=TRUE;}
   else if (i_phi < -90)
      {xstart=0; ystart=nypts-1; pdir=1; ycross=FALSE;}
   else if (i_phi < -45)
      {xstart=0; ystart=0; pdir=-1; ycross=FALSE;}
   else if (i_phi <   0)
      {xstart=0; ystart=0; pdir=1; ycross=TRUE;}
   else if (i_phi <  45)
      {xstart=nxpts-1; ystart=0; pdir=-1; ycross=TRUE;}
   else if (i_phi <  90)
      {xstart=nxpts-1; ystart=0; pdir=1; ycross=FALSE;}
   else if (i_phi < 135)
      {xstart=nxpts-1; ystart=nypts-1; pdir=-1; ycross=FALSE;}
   else
      {xstart=nxpts-1; ystart=nypts-1; pdir=1; ycross=TRUE;}
   color_start = 0;

      /* handle abs(theta) > 90 degrees */
   i_theta = (int)(theta/d2r); if (debug) printf("i_theta=%d\n",i_theta);
   if (abs(i_theta) > 90) {
      color_start = nsides-1;
      ystart = ((ystart) ? 0 : nypts-1);
      xstart = ((xstart) ? 0 : nxpts-1);
      pdir = -pdir;
   }

   color_inc = ((color_start) ? -1 : 1);
   xinc = ((xstart) ? -1 : 1); yinc = ((ystart) ? -1 : 1);
   xthis = &x[ystart][xstart]; ythis = &y[ystart][xstart];
   if (ycross)
      {nhpts=nxpts; nvpts=nypts; hinc=xinc; vinc=yinc*MAXCHAN;}
   else
      {nhpts=nypts; nvpts=nxpts; hinc=yinc*MAXCHAN; vinc=xinc;}
   
   /*** PLOT ***/
   for (i = 0; i < MAXHORIZ; i++) {vhicum[i] = 0; vlocum[i] = 400;}

   if (debug) {printf("hit return for plot\n"); while (!gets(str));}
   initwind();
   if (PlotFile) {
      fprintf(pfp,"IN; SC 0 %d 0 %d;\n",MAXHORIZ+2,MAXVERT+2);
      /* fprintf(pfp,"VS 30.0;\n"); */ /* VELOCITY cm/s */
      fprintf(pfp,"\033.N;19:\033.I81;;17:\n"); /* xON/xOFF handshake */
      fprintf(pfp,"SP;\n");
   }
   if (inverse_vid)
      {SetRGB4(vp,BACKGND_COLOR,0,0,0); SetRGB4(vp,PASS_0_COLOR,15,15,15);}

   for (pass=0, color=color_start; pass<nsides; pass++, color+=color_inc) {
      SetAPen(rp, ((color) ? PASS_1_COLOR : PASS_0_COLOR) );
      if (PlotFile) fprintf(pfp,"SP %d;\n",pass+1);
      if (To_mCAD) fprintf(mfp,"*C %d\n",pass+1);
      Move(rp, *xthis, TOPVERT-*ythis);
      if (PlotFile) fprintf(pfp,"PU %d %d;\n",*xthis+1, 1+TOPVERT-*ythis);
/*      if (To_mCAD) fprintf(mfp,"\n%d %d\n", *xthis, *ythis); */
      for (i=0; i < nvpts-1; i++) {
         hlplot(xthis, ythis, nhpts, hinc, pdir, pass);
         xlast=xthis; ylast=ythis;
         xthis += vinc; ythis += vinc;
         if ((netplot=='y') || (netplot=='Y'))
            hlcross(xlast, ylast, xthis, ythis, nhpts, hinc, -pdir, pass);
      }
      hlplot(xthis, ythis, nhpts, hinc, pdir, pass);
      xthis = &x[ystart][xstart]; ythis = &y[ystart][xstart];
   }

   if ((axes == 'y') || (axes == 'Y')) {
      SetAPen(rp, AXIS_COLOR);
      Move(rp, xA_i[0], TOPVERT - yA_i[0]);
      Draw(rp, xA_i[1], TOPVERT - yA_i[1]);
      Move(rp, xA_i[0], TOPVERT - yA_i[0]);
      Draw(rp, xA_i[2], TOPVERT - yA_i[2]);
      Move(rp, xA_i[0], TOPVERT - yA_i[0]);
      Draw(rp, xA_i[3], TOPVERT - yA_i[3]);
   }
   if (PlotFile) {fprintf(pfp,"SP;\n"); (void) fclose(pfp);}
   if (To_mCAD) (void) fclose(mfp);
}
