    PROGRAM e02zafe

!      E02ZAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : e02zaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, iadres, ifail, m, nadres, npoint, &
                                          px, py
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: lamda(:), mu(:), x(:), y(:)
       INTEGER, ALLOCATABLE            :: adres(:), point(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'E02ZAF Example Program Results'

!      Skip heading in data file
       READ (nin,*)

       READ (nin,*) m
       READ (nin,*) px, py
       nadres = (px-7)*(py-7)
       npoint = m + nadres
       ALLOCATE (adres(nadres),lamda(px),mu(py),x(m),y(m),point(npoint))

!      Read data points and intercepts of panel sides

       READ (nin,*) (x(i),y(i),i=1,m)

       IF (px>8) THEN
          READ (nin,*) lamda(5:(px-4))
       END IF

       IF (py>8) THEN
          READ (nin,*) mu(5:(py-4))
       END IF

!      Sort points into panel order

       ifail = 0
       CALL e02zaf(px,py,lamda,mu,m,x,y,point,npoint,adres,nadres,ifail)

!      Output points in panel order

       DO i = 1, nadres
          WRITE (nout,*)
          WRITE (nout,99999) 'Panel', i
          iadres = m + i

LOOP:     DO
             iadres = point(iadres)

             IF (iadres<=0) THEN
                EXIT LOOP
             END IF

             WRITE (nout,99998) x(iadres), y(iadres)
          END DO LOOP

       END DO

99999  FORMAT (1X,A,I4)
99998  FORMAT (1X,2F7.2)
    END PROGRAM e02zafe
