#include "ifs.h"
#include "standard.h"

#define JAM1       0
#define JAM2       1
#define COMPLEMENT 2
#define INVERDVID  4

void drawonebox();
void checkswitches();
void autoadj();
int  closeby();
void adjust();
void checkswitches();

void drawvectors(rp, funs, numoffun, currentfun)
   struct   RastPort      *rp;
   float funs[][6];
   int numoffun, currentfun;
{
  int  i, color;
  
  for (i=0; i<=numoffun; i++) {
     if ((color = (i+2)%MAXCOLORS) < 2)
       color = color+2;		/* paint with correct color */
     SetAPen(rp, color);
     drawonebox(rp,funs[i]);
  }
  SetAPen(rp,1);
  drawonebox(rp,funs[currentfun]);
}

void drawonebox(rp, afun)
   struct   RastPort      *rp;
   float afun[6];
{
  short  x[10], j;
 
  Move(rp, (int) (WIDTH/2  + DISPSCALE * afun[4] * WIDTH),
           (int) (HEIGHT/2 - DISPSCALE * afun[5] * HEIGHT));
  j=0;
  x[j++] = WIDTH/2  + DISPSCALE * (afun[0] + afun[4]) * WIDTH;
  x[j++] = HEIGHT/2 - DISPSCALE * (afun[2] + afun[5]) * HEIGHT;
  x[j++] = WIDTH/2  + DISPSCALE * (afun[0] + afun[1] + afun[4]) * WIDTH;
  x[j++] = HEIGHT/2 - DISPSCALE * (afun[2] + afun[3] + afun[5]) * HEIGHT;
  x[j++] = WIDTH/2  + DISPSCALE * (afun[1] + afun[4]) * WIDTH;
  x[j++] = HEIGHT/2 - DISPSCALE * (afun[3] + afun[5]) * HEIGHT;
  x[j++] = WIDTH/2  + DISPSCALE * afun[4] * WIDTH;
  x[j++] = HEIGHT/2 - DISPSCALE * afun[5] * HEIGHT;
  PolyDraw(rp, 4, &x[0]);
  Move(rp, (int) (WIDTH/2  + DISPSCALE * (afun[0]/4 + afun[4]) * WIDTH),
           (int) (HEIGHT/2 - DISPSCALE * (afun[2]/4 + afun[5]) * HEIGHT));
  j=0;			/* draw little box in right hand corner */
  x[j++] = WIDTH/2  + DISPSCALE * (afun[0]/4 + afun[1]/4 + afun[4]) * WIDTH;
  x[j++] = HEIGHT/2 - DISPSCALE * (afun[2]/4 + afun[3]/4 + afun[5]) * HEIGHT;
  x[j++] = WIDTH/2  + DISPSCALE * (afun[1]/4 + afun[4]) * WIDTH;
  x[j++] = HEIGHT/2 - DISPSCALE * (afun[3]/4 + afun[5]) * HEIGHT;
  PolyDraw(rp, 2, &x[0]);  
                             /* and slash in adjacent corner */
  Move(rp, (int) (WIDTH/2  + DISPSCALE * (afun[0]*.75 + afun[4]) * WIDTH),
           (int) (HEIGHT/2 - DISPSCALE * (afun[2]*.75 + afun[5]) * HEIGHT));
  Draw(rp, (int) (WIDTH/2  + DISPSCALE * (afun[0] 
                           + afun[1]/4 + afun[4]) * WIDTH),
           (int) (HEIGHT/2 - DISPSCALE * (afun[2] 
                           + afun[3]/4 + afun[5]) * HEIGHT));
}


int closeby (funs, currentfun, numoffun, mx, my) 

float funs[][6];
int   *currentfun, numoffun;
USHORT mx, my;

{
  int xc[4], yc[4], i, j, thecorner = 0, hit=FALSE, thefun;
    
  for (j=0;j<=numoffun;j++) {
     xc[0] = WIDTH/2  + DISPSCALE * funs[j][4] * WIDTH;
     yc[0] = HEIGHT/2 - DISPSCALE * funs[j][5] * HEIGHT;

     xc[1] = WIDTH/2  + DISPSCALE * (funs[j][0] + funs[j][4]) * WIDTH;
     yc[1] = HEIGHT/2 - DISPSCALE * (funs[j][2] + funs[j][5]) * HEIGHT;
  
     xc[2] = WIDTH/2  + DISPSCALE 
             * (funs[j][0] + funs[j][1] + funs[j][4]) * WIDTH;
     yc[2] = HEIGHT/2 - DISPSCALE 
             * (funs[j][2] + funs[j][3] + funs[j][5]) * HEIGHT;
     xc[3] = WIDTH/2  + DISPSCALE * (funs[j][1] + funs[j][4]) * WIDTH;
     yc[3] = HEIGHT/2 - DISPSCALE * (funs[j][3] + funs[j][5]) * HEIGHT;

     for (i=0;i<4;i++) {
       xc[i] = mx - xc[i];
       yc[i] = my - yc[i];
       if (xc[i] < 0) xc[i] = - xc[i];
       if (yc[i] < 0) yc[i] = - yc[i];
       if (xc[i] < EPSILON && yc[i] < EPSILON) 
          if (hit == FALSE) {
             thecorner = i+1;		        /* is closeby */
             thefun = j;
             hit = TRUE;
          }
          else				        /* two at same point */
             if (j == *currentfun) {            /* select currentfun */
                thecorner = i+1;
                thefun = j;
	     }
          
     }
  }
  if (hit == TRUE) *currentfun = thefun;
  return (thecorner);
  }       /* end closeby */

void adjust(corner, rp, funs, currentfun, mx, my) /* returns 1 if moved */
  int corner;
  struct   RastPort      *rp;
  float funs[][6];
  int currentfun;
  USHORT mx, my;

{
  float ta, tb, tc, td, te, tf;

  ta = funs[currentfun][0];
  tb = funs[currentfun][1];
  tc = funs[currentfun][2];
  td = funs[currentfun][3];
  te = funs[currentfun][4];
  tf = funs[currentfun][5];

  switch (corner) {
    case 1:
      te = (mx - WIDTH/2)/(DISPSCALE * WIDTH);
      tf = (HEIGHT/2 - my)/(DISPSCALE * HEIGHT);
      break;
    case 2:
      ta = (mx - WIDTH/2)/(DISPSCALE * WIDTH)   - te; 
      tc = (HEIGHT/2 - my)/(DISPSCALE * HEIGHT) - tf;
      break;
    case 3:
      ta = (mx - WIDTH/2)/(DISPSCALE * WIDTH)   - te - tb; 
      td = (HEIGHT/2 - my)/(DISPSCALE * HEIGHT) - tf - tc;
      break;
    case 4: 
      tb = (mx - WIDTH/2)/(DISPSCALE * WIDTH)   - te;
      td = (HEIGHT/2 - my)/(DISPSCALE * HEIGHT) - tf;
      break;
  }
  if (ta >= MINCOFF && ta <= MAXCOFF &&
      tb >= MINCOFF && tb <= MAXCOFF &&
      tc >= MINCOFF && tc <= MAXCOFF &&
      td >= MINCOFF && td <= MAXCOFF &&
      te >= MINCOFF && te <= MAXCOFF &&
      tf >= MINCOFF && tf <= MAXCOFF) {
   
      funs[currentfun][0] = ta;
      funs[currentfun][1] = tb;
      funs[currentfun][2] = tc;
      funs[currentfun][3] = td;
      funs[currentfun][4] = te;
      funs[currentfun][5] = tf;
      SetDrMd(rp,COMPLEMENT);
      drawonebox(rp, funs[currentfun]);
      drawonebox(rp, funs[currentfun]);
      SetDrMd(rp,JAM1);
      }
}

void checkswitches(rp,funs,percent,currentfun,numoffun,vectorsw,displaynumsw)
  struct   RastPort      *rp;
  float funs[][6], percent[];
  int currentfun, numoffun;
  short vectorsw, displaynumsw;
{
  if (vectorsw) 
     drawvectors(rp, funs, numoffun, currentfun);
  if (displaynumsw) 
     displaynums(rp, funs, percent, currentfun, numoffun);
}
