      SUBROUTINE VALR2(X,Y,N0,P,IOP,A,IND,KO)
C     -------------------
      DIMENSION X(3),Y(3),G(2),H(2)
      DIMENSION E(5),E2(10),E3(15)
      DIMENSION APH1(3),APH2(3),APH4(3)
      DIMENSION RSQ(3),A3D8(3),CST(3)
      REAL KOM,L
C     -------------------
      DATA PI/3.1415926535898/
      DATA TWOPI/6.28318530717958/
      DATA ALNPI/1.14472988584940/
      DATA RTPI/1.77245385090552/
      DATA RTPII/.56418958354776/
C     -------------------
      DATA E(1)/.885777518572895E+00/,  E(2)/-.981151952778050E+00/,
     1     E(3)/.759305502082485E+00/,  E(4)/-.353644980686977E+00/,
     2     E(5)/.695232092435207E-01/
      DATA E2(1) /.886226470016632E+00/,  E2(2) /-.999950714561036E+00/,
     1     E2(3) /.885348820003892E+00/,  E2(4) /-.660611239043357E+00/,
     2     E2(5) /.421821197160099E+00/,  E2(6) /-.222898055667208E+00/,
     3     E2(7) /.905057384150449E-01/,  E2(8) /-.254906111884287E-01/,
     4     E2(9) /.430895168984138E-02/,  E2(10)/-.323377239693247E-03/
      DATA E3(1) /.886226924931465E+00/,  E3(2) /-.999999899776252E+00/,
     1     E3(3) /.886223733186722E+00/,  E3(4) /-.666626670510907E+00/,
     2     E3(5) /.442851899328569E+00/,  E3(6) /-.265638206366025E+00/,
     3     E3(7) /.145060043403014E+00/,  E3(8) /-.714909837799889E-01/,
     4     E3(9) /.309199295521210E-01/,  E3(10)/-.112323532148441E-01/,
     5     E3(11)/.324944543171185E-02/,  E3(12)/-.704260243309096E-03/,
     6     E3(13)/.105787574480633E-03/,  E3(14)/-.971864864160461E-05/,
     7     E3(15)/.408335517232165E-06/
      DATA APH1(1)/2.02E-7/, APH1(2)/2.08E-13/, APH1(3)/2.71E-19/
      DATA APH2(1)/1.22E-2/, APH2(2)/1.23E-4/, APH2(3)/1.34E-6/
      DATA APH4(1)/.6962E-1/, APH4(2)/.6990E-2/, APH4(3)/.7311E-3/
      DATA RSQ(1)/6.0516/, RSQ(2)/12.60605/, RSQ(3)/19.201924/
      DATA A3D8(1)/0.28125E-4/, A3D8(2)/0.285E-7/, A3D8(3)/0.32625E-10/
      DATA CST(1)/.5625E-4/, CST(2)/.57E-7/, CST(3)/.6512E-10/
C     -------------------
C     TAU IS A MACHINE DEPENDENT TOLERANCE. IT IS ASSUMED THAT A
C     7 OR MORE DIGIT FLOATING POINT ARITHMETIC IS BEING USED.
C     -------------------
      TAU=2.0*SPMPAR(1)
      IF (TAU.LT.3.E-11) TAU=AMAX1(5.0*TAU,1.E-14)
C     -------------------
      N=N0
      IF (N.EQ.2.OR.N.LT.1) GO TO 4021
      TAUSQ=TAU*TAU
C
      P=0.0
      IND=0
      A=0.0
      KOM=0.0
      K=1
      IF (N.NE.1) GO TO 10
C
      W=X(2)-X(1)
      Z=Y(2)-Y(1)
      U=X(3)-X(1)
      V=Y(3)-Y(1)
      XK=0.0
      PSI1=V*W-U*Z
      IF (PSI1.GE.0.0) GO TO 21
C
      P=-1.0
      T1=W
      W=U
      U=T1
      T1=V
      V=Z
      Z=T1
      GO TO 21
C
   10 KO=0
      X(N+1)=X(1)
      Y(N+1)=Y(1)
      U=X(2)-X(1)
      V=Y(2)-Y(1)
      XK=X(1)
      YK=Y(1)
C
   20 W=X(1)-X(N)
      Z=Y(1)-Y(N)
   21 D1SQ=W*W+Z*Z
      IF (D1SQ.GT.TAUSQ) GO TO 30
      IF (N.EQ.1) GO TO 4011
      N=N-1
      IF (N.EQ.2) RETURN
      GO TO 20
C
   30 D2SQ=U*U+V*V
      IF (D2SQ.GT.TAUSQ) GO TO 40
      IF (N.EQ.1) GO TO 4011
   31 K=K+1
      U=X(K+1)-XK
      V=Y(K+1)-YK
      D2SQ=U*U+V*V
      IF (D2SQ.LE.TAUSQ) GO TO 31
      IF (K.EQ.N-1) RETURN
C
   40 A=XK*(Y(K+1)-Y(N))
      BGD1=SQRT(D1SQ+D1SQ)
      BGD2=SQRT(D2SQ+D2SQ)
C
C             PROCESSING VERTEX (XK,YK)
C
   50 PSI1=V*W-U*Z
      CEE=U*W+V*Z
      AJ0=ATAN2(PSI1,CEE)
      KOM=KOM+AJ0
      L=0.0
      B=.5*(X(K)*X(K)+Y(K)*Y(K))
      IF (B.GT.APH1(IOP)) GO TO 60
      P1=AJ0/TWOPI
      GO TO 3621
C
   60 G(1)=(W*X(K)+Z*Y(K))/BGD1
      G(2)=(U*X(K)+V*Y(K))/BGD2
      H(1)=(-Y(K)*W+X(K)*Z)/BGD1
      H(2)=(-Y(K)*U+X(K)*V)/BGD2
      IF (ABS(PSI1).GT.BGD1*BGD2*A3D8(IOP)) GO TO 80
      IF (CEE.LT.0.0) GO TO 70
      IF (ABS(AJ0).GT.TAU.AND.G(1).LT.0.0) GO TO 80
      P1=0.0
      GO TO 3621
C
   70 IF (ABS(PSI1).LE.(.5*TAU*BGD1*BGD2)) IND=2
      IF (PSI1.LT.0.0) GO TO 71
      P1=.5*ERFC1(0,H(2))
      GO TO 3621
   71 P1=-.5*ERFC1(0,H(1))
      GO TO 3621
C
   80 IF (B.GT.APH2(IOP)) GO TO 90
      C=RTPI*(H(2)-H(1))-(G(2)*H(2)-G(1)*H(1))
      P1=(AJ0-C)/TWOPI
      GO TO 3621
C
C                 COMPUTATION OF L
C
   90 IF (G(1).LT.0.0) GO TO 100
      IF (G(2).GE.0.0) GO TO 130
      G(2)=-G(2)
      H(2)=-H(2)
      IF (ABS(H(2)).LE.APH4(IOP)) GO TO 91
      L=.5*ERFC1(0,-H(2))
      GO TO 120
   91 L=.5+RTPII*H(2)
      GO TO 120
C
  100 G(1)=-G(1)
      H(1)=-H(1)
      IF (G(2).LT.0.0) GO TO 110
      IF (ABS(H(1)).LE.APH4(IOP)) GO TO 101
      L=.5*ERFC1(0,H(1))
      GO TO 120
  101 L=.5-RTPII*H(1)
      GO TO 120
C
  110 G(2)=-G(2)
      H(2)=-H(2)
      IF (ABS(H(1)).LE.APH4(IOP)) GO TO 112
      IF (ABS(H(2)).LE.APH4(IOP)) GO TO 111
      L=.5*(ERFC1(0,H(1))-ERFC1(0,H(2)))
      GO TO 130
  111 L=RTPII*H(2)-.5*ERF(H(1))
      GO TO 130
  112 IF (ABS(H(2)).LE.APH4(IOP)) GO TO 113
      L=.5*ERF(H(2))-RTPII*H(1)
      GO TO 130
  113 L=RTPII*(H(2)-H(1))
      GO TO 130
C
  120 PSI1=-PSI1
      IF (PSI1.LE.0.0) GO TO 121
      L=L-1.0
      AJ0=AJ0+PI
      GO TO 130
  121 AJ0=AJ0-PI
C
C                 SERIES EVALUATION
C
  130 IF (B.GE.RSQ(IOP)) GO TO 171
      CAPE=AJ0
      CAPH=.5*AJ0
      M=1
      F=0.0
      AJ1=H(2)-H(1)
      CIRCM=AJ1
      IF (IOP-2) 140,150,160
C
  140 SUM=E(M)*AJ1
  141 M=M+1
      H(1)=H(1)*G(1)
      H(2)=H(2)*G(2)
      T=H(2)-H(1)
      F=F+B
      CAPV=(F*CAPE+T)/M
      SUM=SUM+E(M)*CAPV
      IF (M.GE.5) GO TO 170
      CAPE=CIRCM
      CIRCM=CAPV
      GO TO 141
C
  150 SUM=E2(M)*AJ1
  151 M=M+1
      H(1)=H(1)*G(1)
      H(2)=H(2)*G(2)
      T=H(2)-H(1)
      F=F+B
      CAPV=(F*CAPE+T)/M
      SUM=SUM+E2(M)*CAPV
      IF (M.GE.10) GO TO 170
      CAPE=CIRCM
      CIRCM=CAPV
      GO TO 151
C
  160 SUM=E3(M)*AJ1
  161 M=M+1
      H(1)=H(1)*G(1)
      H(2)=H(2)*G(2)
      T=H(2)-H(1)
      F=F+B
      CAPV=(F*CAPE+T)/M
      SUM=SUM+E3(M)*CAPV
      IF (M.GE.15) GO TO 170
      CAPE=CIRCM
      CIRCM=CAPV
      GO TO 161
C
  170 P1=L+EXP(-(B+ALNPI))*(CAPH-SUM)
      GO TO 3621
  171 P1=L
C
C               STANDARD TERMINATION
C
 3621 IF (K.NE.N) GO TO 3651
      IF (N.NE.1) GO TO 3631
      P=ABS(P+ABS(P1))
      RETURN
C
 3631 P=P-P1
      KOM=KOM/TWOPI
      A=.5*A
      IF (KOM.LT.0.0) GO TO 3641
      KO=INT(KOM+.125)
      GO TO 3645
 3641 KO=INT(KOM-.125)
 3645 P=P+FLOAT(KO)
      RETURN
C
C              SET UP THE NEXT VERTEX
C
 3651 W=U
      Z=V
      BGD1=BGD2
      XK=X(K+1)
      YK=Y(K+1)
      YKM1=Y(K)
 3661 K=K+1
      U=X(K+1)-XK
      V=Y(K+1)-YK
      D2SQ=U*U+V*V
      IF (D2SQ.LE.TAUSQ) GO TO 3661
      BGD2=SQRT(D2SQ+D2SQ)
      P=P-P1
      A=A+XK*(Y(K+1)-YKM1)
      GO TO 50
C
C                  ERROR RETURN
C
 4011 IND=1
      P=5.0
      RETURN
 4021 IND=3
      RETURN
      END
