Listing 8 Definition of RandWalk

/********** DISPLAYS.C **********/
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
#include <dos.h>
#include "randefs.h"
#include "displays.h"

static int h, w;     /* char height and width */

void NewScreen(char *title)
{ h = textheight("0")-1, w = textwidth("0");
  cleardevice();
  setcolor(BLUE);
  setbkcolor(WHITE);
  rectangle(0, 0, SPAN, FS);    /* Draw boundary    */
  outtextxy(5*w, -7*h,"Hit any key to continue, ESC to exit");
  /***** Display Title: *****/
  outtextxy(5*w, -2*h, title);
  randomize();
}

/*****************************************************
 * RandWalk calls SumNPicks to generate random events
 * per the model in table pointed to by rnd.
 ******************************************************/
int RandWalk(RAND *rnd)
{ int    oldx, oldy, x, y, sf, nsteps, nbin = rnd->nbin;
  double theta;     /* angle in radians */
  
  /************* Display Random Walk *************/
  NewScreen(*(rnd->labls+nbin));
  line(SPAN/2, 0, SPAN/2, FS);/* Vert Centr Line */
  line(0, FS/2,SPAN, FS/2);   /* Horiz C.L.      */
  oldx = x = SPAN/2;          /* Start at Origin */
  oldy = y = FS/2;            /* Ditto           */
  nsteps = 0;
  
  sf = (nbin < 12) ? 22/nbin : 1;  /* Scaling Factor */
  
  while((x<SPAN)&&(x>0)&&(y<FS)&&(y>0)&& (kbhit()==0))
  {  if(strcmp(*(rnd->labls+nbin),"UNIFORM CIRCULAR:"))
     { x = oldx + sf*(2*SumNPicks(rnd) - (nbin-1));
       y = oldy + sf*(2*SumNPicks(rnd) - (nbin-1));
     }
      else
      {  theta = KO*((double)(1+SumNPicks(rnd) ));
         /***** Convert to Cartesian Coord's *****/
         x = oldx + 15 * cos(theta); /* 15 = step len */
         y = oldy + 15 * sin(theta);
     }
     line(oldx, oldy, x, y);
     nsteps++;
     oldx = x;
     oldy = y;
     delay(50);  /* User needs time to discern step */
  }
  ResetTxtCursor();
  printf("\n\n NSTEPS = %d\n", nsteps);
  if (ESC = getch())   /* User's chance to exit */
     return(1);
  return(0);
}

void Erase(int x, int y, int scanlen, int nlins)
{ int i, j;

  moveto(x, y);
  for (i = 0; i < scanlen; i++)
     for (j = 0; j < nlins; j++)
        putpixel(x + i, y + j, BLACK);
}

void ScreenLabels(void)
{ outtextxy(-135, 0.09*FS, " DIFFERENTIATOR");
  outtextxy(-135, 0.31*FS, "LOW-PASS FILTER");
  outtextxy(-135, 0.53*FS, "RUNNING AVERAGE");
  outtextxy(-135, 0.75*FS, "   NOISY SIGNAL");
  outtextxy(-135, 0.97*FS, "         SIGNAL");
}

void ResetTxtCursor(void)
{ union REGS r;

  r.h.ah = 2;   /* Function 2 */
  r.h.bh = 0;   /* Page   = 0 */
  r.h.dh = 0;   /* Row    = 0 */
  r.h.dl = 0;   /* Column = 0 */
  int86(0x10, &r, &r);
}

void DrawAxes(RAND *rnd)
{ int i, h_off, h_step, nbin = rnd->nbin;

  h_step = SPAN/nbin;
  h_off = (nbin < 25) ? h_step/4 : 0;
  line(-1, FS, SPAN, FS);
  line(-1, FS, -1, 0);
  outtextxy(-5*w,        - h, "1.0 _");
  outtextxy(-5*w, 0.2*FS - h, "0.8 _");
  outtextxy(-5*w, 0.4*FS - h, "0.6 _");
  outtextxy(-5*w, 0.6*FS - h, "0.4 _");
  outtextxy(-5*w, 0.8*FS - h, "0.2 _");
  outtextxy(-5*w,     FS - h, "0.0 _");
  outtextxy(30*w, -2*h, "N_TRIALS =");
  for (i = 0; i < nbin; i++)
      outtextxy(h_off+i*h_step,FS+6,*(rnd->labls+i));
}

void DrawData(int *data, int y_offset)
{ int *dat = data;
  int ndat = *dat++;
  int x, y, oldx = 0, oldy = y_offset;
  
  for (x = 0; x < ndat; x++)
  {  y = y_offset + *dat++;
    line(oldx, oldy, x, y);
    oldx = x;
    oldy = y;
  }
}

void DisplayFreq(RAND *rnd, float *frqdst)
{ int i, y, nbin = rnd->nbin;
  int delx = SPAN/nbin;
  char buf[6];
  
  clearviewport();
  if ( !(rnd->ndat%10) )
  {  Erase(41*w, -2*h, 40, 8);
      itoa(rnd->ndat, buf, 10);
      outtextxy(41*w, -2*h, buf);
  }
  
  for (i = 0; i < nbin; i++)
  {  y = (1 - *(frqdst+i))*FS;
      if(y > 0 && y != FS)    /* Stay inside borders */
       line(i*delx, y, (i+1)*delx, y);
  }
}

void DisplayStats(RAND *rnd, float *data)
/************** Print Statistics **************/
{ int i;
  float sum = 0.0;
  
  ResetTxtCursor();
  printf("Event Frequency");
  for (i = 0; i < rnd->nbin; i++)
  {  printf("\n%s\t %.3f\%", *(rnd->labls+i), *(data+i));
     sum += *(data+i)*rnd->delta;
  }
  printf("\nINTEGRAL = %.3f\%", sum);
}

/* End of File */