/*
C-----------------------------------------------------------------------
C Demonstration program for PGPLOT: travelling sine wave.
C
C This program illustrates how animated displays can be generated with
C PGPLOT, although PGPLOT is not optimized for such use.
C
C To create an animated display:
C
C  (1) Do not call PGPAGE (or PGENV, which calls PGPAGE) between frames;
C  (2) Enclose all the calls required to generate each frame between
C      PGBBUF and PGEBUF calls;
C  (3) Either: erase the entire previous frame by calling PGERAS before
C      drawing the next frame; or: erase the parts of the frame that
C      have changed by overwriting with the background color (color
C      index 0).
C
C This program demonstrated both approaches. Using PGERAS is usually
C slower, because more has to be redrawn in each frame. Erasing selected
C parts of the display can be faster, but it may be difficult to avoid
C erasing parts that should remain visible.
C
C This program requires an interactive display that supports writing
C in color index 0.
C-----------------------------------------------------------------------
C Parameters:
C   NT is the number of frames in the animation.
*/
      N = 50
      NT = 100
      PI=pi()
      A = 2.0*PI/N
      B = 2.0*PI/NT
// Variables:
      X = Y = floatarray(N+1)
      STR = ""
      L = 0
//-----------------------------------------------------------------------

      print "Demonstration of animation with PGPLOT"
      print "This program requires an interactive display that"
      print "supports writing in color index 0."

      if (pgbeg(0,plotdevice,1,1) != 1) break

      pgqinf "HARDCOPY", STR, L
      if (STR != "NO") print "Warning: device is not interactive"

      print "1: erasing the entire screen between frames"

      pgpage
      pgvstd
      pgwnad -A, A*(N+1), -1.1, 1.1
      
      for T=0,NT {
        pgbbuf
        pgeras
        pgsci 1
        pgbox "bcnst", 0.0, 0, "bcnst", 0.0, 0
        for I=0,N {
          X[I+1] = I*A 
          Y[I+1] = sin(I*A-T*B)
        }
        pgsci 3
        pgline N+1,X,Y
        STR = string(T)
        pgmtxt "T", 2.0, 0.0, 0.0, STR
        pgebuf
      }

      pgpage
      print "2: erasing only the line between frames"

      pgvstd
      pgwnad -A, A*(N+1), -1.1, 1.1
      pgbbuf
      pgsci 1
      pgbox "bcnst", 0.0, 0, "bcnst", 0.0, 0
      for I=0,N {
         X[I+1] = I*A 
         Y[I+1] = sin(I*A)
      }
      pgebuf
   
      for T=0,NT {
        pgbbuf
        pgsci 0
        pgline N+1,X,Y
        pgsci 3
        for I=0,N {
          X[I+1] = I*A 
          Y[I+1] = sin(I*A-T*B)  
        }
        pgline N+1,X,Y
        pgebuf
      }

      pgend
