/* $Header:zip2.c 12.0$ */
/* $ACIS:zip2.c 12.0$ */
/* $Source: /ibm/acis/usr/src/usr.lib/libaed/examples/RCS/zip2.c,v $ */

#ifndef lint
static char *rcsid = "$Header:zip2.c 12.0$";
#endif

#include "stdio.h"
/* See zip.c for general description of this program */

#define xnd(L,H) (L + (rand() % (H-L+1)))
#define then
#define begin {
#define end }
#define forever -1

static int x1,x2,y1,y2;
static int X1,X2,Y1,Y2;
static int min,max;
static int Min,Max;
static int dx1,dx2,dy1,dy2;
static int dX1,dX2,dY1,dY2;
typedef struct nd { int xa,xb,ya,yb;
                    struct nd *next;
                  } node, *nodeptr;
static nodeptr root;
static nodeptr Root;

main(argc,argv)
int argc;
char *argv[];
{ int i,j,k;
  int n,N;
  n = 30;
  min = 2;
  max = 20;
  N = 150;
  Min = 1;
  Max = 4;
  k=0;
  if (argc>=2) n   = atoi(*++argv);
  if (argc>=3) min = atoi(*++argv);
  if (argc>=4) max = atoi(*++argv);
  if (argc>=5) N   = atoi(*++argv);
  if (argc>=6) Min = atoi(*++argv);
  if (argc>=7) Max = atoi(*++argv);
  if (argc>=8) k   = atoi(*++argv);
  VI_Init(&i,&j);
  VI_Tile(1024,800,1,1,&k);
  VI_Merge(6);  /* EXOR */
  srand(getpid());
  initstack(n,N);
  x1 = xnd(0,1023);
  x2 = xnd(0,1023);
  y1 = xnd(0,800);
  y2 = xnd(0,800);
  dx1 = xnd(min,max);
  dx2 = xnd(min,max);
  dy1 = xnd(min,max);
  dy2 = xnd(min,max);
  X1 = xnd(0,1023);
  X2 = xnd(0,1023);
  Y1 = xnd(0,800);
  Y2 = xnd(0,800);
  dX1 = xnd(Min,Max);
  dX2 = xnd(Min,Max);
  dY1 = xnd(Min,Max);
  dY2 = xnd(Min,Max);

  while(forever)
    { VI_AMove(x1,y1);
      VI_ALine(x2,y2);
      VI_AMove(root->xa,root->ya);
      VI_ALine(root->xb,root->yb);
      VI_AMove(X1,Y1);
      VI_ALine(X2,Y2);
      VI_AMove(Root->xa,Root->ya);
      VI_ALine(Root->xb,Root->yb);
      VI_Force();
      rollstack();
      newpoints();
    }
}

newpoints()
{ x1 += dx1;
  x2 += dx2;
  y1 += dy1;
  y2 += dy2;
  if (x1 > 1023) {x1=1023; newd(&dx1);}
  if (x2 > 1023) {x2=1023; newd(&dx2);}
  if (y1 >  800) {y1= 800; newd(&dy1);}
  if (y2 >  800) {y2= 800; newd(&dy2);}
  if (x1 <    0) {x1=   0; newd(&dx1);}
  if (x2 <    0) {x2=   0; newd(&dx2);}
  if (y1 <    0) {y1=   0; newd(&dy1);}
  if (y2 <    0) {y2=   0; newd(&dy2);}
  X1 += dX1;
  X2 += dX2;
  Y1 += dY1;
  Y2 += dY2;
  if (X1 > 1023) {X1=1023; Newd(&dX1);}
  if (X2 > 1023) {X2=1023; Newd(&dX2);}
  if (Y1 >  800) {Y1= 800; Newd(&dY1);}
  if (Y2 >  800) {Y2= 800; Newd(&dY2);}
  if (X1 <    0) {X1=   0; Newd(&dX1);}
  if (X2 <    0) {X2=   0; Newd(&dX2);}
  if (Y1 <    0) {Y1=   0; Newd(&dY1);}
  if (Y2 <    0) {Y2=   0; Newd(&dY2);}
}

newd(delta)
int *delta;
{ int d;
  d = xnd(min,max);
  if (*delta > 0) d = -d;
  *delta = d;
}

Newd(delta)
int *delta;
{ int d;
  d = xnd(Min,Max);
  if (*delta > 0) d = -d;
  *delta = d;
}

initstack(n,N) 
int n,N;
{  int i;
   nodeptr ptr,*lastptr;
   char *malloc();
   lastptr = &root;
   for (i=0; i<n; i++)
      { ptr = (nodeptr) malloc(sizeof(node));
        ptr->xa = ptr->xb = ptr->ya = ptr->yb = 0;
        ptr->next = root;  
        *lastptr = ptr;
        lastptr = &(ptr->next);
      }
   lastptr = &Root;
   for (i=0; i<N; i++)
      { ptr = (nodeptr) malloc(sizeof(node));
        ptr->xa = ptr->xb = ptr->ya = ptr->yb = 0;
        ptr->next = Root;  
        *lastptr = ptr;
        lastptr = &(ptr->next);
      }
}

rollstack()
{ root->xa = x1;
  root->xb = x2;
  root->ya = y1;
  root->yb = y2;
  root = root->next;
  Root->xa = X1;
  Root->xb = X2;
  Root->ya = Y1;
  Root->yb = Y2;
  Root = Root->next;
}
