      PROGRAM fieldline
*     draw electric field lines in two dimensions
      IMPLICIT NONE
      REAL*8 a, q(10), qtotal
      REAL x(10), y(10)
      INTEGER IR_, N
      DATA qtotal/0.0D0/
      CALL GWopen(IR_, 0)
      CALL screen(a)
      CALL charges(N,x,y,q,qtotal,a)
*     draw field lines
      CALL draw_lines(N,x,y,q,qtotal,a)
      CALL GWquit(IR_)
      END
C
      SUBROUTINE screen(a)
      IMPLICIT NONE
      REAL*8 a
      REAL xwin, ywin
      INTEGER IR_, L
      L = 10
      CALL compute_aspect_ratio(REAL(L),xwin,ywin)
      CALL GWindow(IR_, -xwin, -ywin, xwin, ywin)
      a = 0.2D0                             ! "radius" of visual image of charges
      CALL GWsetmrk(IR_, 6, REAL(a*2), -1, -1, -1)
      END
C
      SUBROUTINE charges(N,x,y,q,qtotal,a)
*     input charge values and location
      IMPLICIT NONE
      CHARACTER*80 Stmp1_
      REAL*8 charge, q(10), qtotal, a
      REAL x(10), y(10)
      INTEGER IR_, N
      LOGICAL Ltmp1_
      N = 0                                 ! # of point charges
      Ltmp1_ = .TRUE.
      DO WHILE(Ltmp1_)
         CALL draw_charges(N,x,y,q,a)
         CALL GWinput(IR_, 'charge (0 to exit input mode) = ', Stmp1_)
         IF(IR_ .EQ. 0) THEN
            charge = 0
         ELSE
            READ(Stmp1_,*) charge
         ENDIF
         IF(charge .NE. 0.0D0) THEN
            CALL GWsetmsg(IR_, 
     +           'place mouse at charge location and click.')
            N = N + 1
            CALL GWcappnt(IR_, x(N), y(N), '')  ! location of charge
            q(N) = charge
            qtotal = qtotal + charge
         END IF
         Ltmp1_ = charge .NE. 0.0D0
      END DO
*     redraw charges
      CALL draw_charges(N,x,y,q,a)
      END
C
      SUBROUTINE draw_charges(N,x,y,q,a)
      IMPLICIT NONE
      REAL*8 q(10), a
      REAL x(10), y(10)
      INTEGER IR_, i, N
      DO i = 1, N
         IF(q(i) .GT. 0) THEN
            CALL GWsetmrk(IR_, -1, -1.0, 16, -1, -1)
         ELSE
            CALL GWsetmrk(IR_, -1, -1.0, 13, -1, -1)
         END IF
         CALL GWputmrk(IR_, x(i), y(i))
      END DO
      END
C
      SUBROUTINE draw_lines(N,x,y,q,qtotal,a)
*     number of lines per unit charge leaving positive charges
      IMPLICIT NONE
      REAL*8 E, E0, Emin, Ex, Ey, ds_big, ds_small, dtheta, dx, dy,
     +       pi, r, sgn, theta, xline, yline, q(10), qtotal, a
      PARAMETER(pi = 3.141592653589793D0)
      REAL x(10), y(10)
      INTEGER IR_, i, j, key, lpc, N
      CHARACTER stop_plot*80
      LOGICAL Ltmp1_, TBkeyinput
      DATA r/0.0D0/
      lpc = 8                               ! lines per charge
      Emin = 0.01D0                         ! if E < Emin stop drawing fieldline
      sgn = sign(1.0D0,qtotal)
      IF(sgn .EQ. 0) sgn = 1
      ds_small = 0.01D0*sgn
      ds_big = sgn
      DO i = 1, N
         IF(q(i)*sgn .GT. 0) THEN
            dtheta = 2*pi/(lpc*abs(q(i)))
            DO theta = 0, 2*pi, dtheta
               CALL GWmove2(IR_, x(i), y(i))
               stop_plot = 'no'
               xline = x(i) + a*cos(theta)
               yline = y(i) + a*sin(theta)
               Ltmp1_ = .TRUE.
               DO WHILE(Ltmp1_)
                  Ex = 0
                  Ey = 0
                  DO j = 1, N
*                    x-distance from point to charge j
                     dx = xline - x(j)
*                    y-distance from point to charge j
                     dy = yline - y(j)
                     r = sqrt(dx*dx + dy*dy)
                     IF(r .GT. 0.9D0*a) THEN
                        E0 = q(j)/r**3
                        Ex = Ex + E0*dx
                        Ey = Ey + E0*dy
                     ELSE
*                       field line reached another charge
                        stop_plot = 'yes'
                     END IF
                  END DO
                  E = sqrt(Ex*Ex + Ey*Ey)
                  IF(E .GT. Emin .OR. r .LT. 20) THEN
*                    new position on fieldline
                     xline = xline + ds_small*Ex/E
                     yline = yline + ds_small*Ey/E
                  ELSE
*                    new position on fieldline
                     xline = xline + ds_big*Ex/E
                     yline = yline + ds_big*Ey/E
                  END IF
                  CALL GWline2(IR_, REAL(xline), REAL(yline))
                  IF(TBkeyinput()) THEN
*                    user can stop drawing field line
                     CALL TBgetkey(key)
                     stop_plot = 'yes'
                  END IF
                  Ltmp1_ = stop_plot .NE. 'yes'
               END DO
            END DO
         END IF
      END DO
      END
