/* nag_2d_panel_sort (e02zac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 8, 2004.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nage02.h>

int main(void)
{
  /* Scalars */
  Integer  exit_status, i, iadres, m, nadres, npoint, px, py;

  /* Arrays */
  double   *lamda = 0, *mu = 0, *x = 0, *y = 0;
  Integer  *point = 0;

  /* Nag types */
  NagError fail;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_2d_panel_sort (e02zac) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  while (scanf("%ld%*[^\n] ", &m) != EOF)
    {
      if (m > 0)
        {
          /* Allocate memory */
          if (!(x = NAG_ALLOC(m, double)) ||
              !(y = NAG_ALLOC(m, double)))
            {
              printf("Allocation failure\n");
              exit_status = -1;
              goto END;
            }
        }
      else
        {
          printf("Invalid m.\n");
          exit_status = 1;
          return exit_status;
        }
      scanf("%ld%ld%*[^\n] ", &px, &py);
      nadres = (px - 7) * (py - 7);
      npoint = m+nadres;
      if (px >= 8 && py >= 8)
        {
          /* Allocate memory */
          if (!(lamda = NAG_ALLOC(px, double)) ||
              !(mu = NAG_ALLOC(py, double)) ||
              !(point = NAG_ALLOC(npoint, Integer)))
            {
              printf("Allocation failure\n");
              exit_status = -1;
              goto END;
            }
        }
      else
        {
          printf("Invalid px or py.\n");
          exit_status = 1;
          goto END;
        }

      /* Read data points and intercepts of panel sides */
      for (i = 1; i <= m; ++i)
        {
          scanf("%lf%lf", &x[i - 1], &y[i - 1]);
        }
      scanf("%*[^\n] ");

      if (px > 8)
        {
          for (i = 5; i <= px - 4; ++i)
            {
              scanf("%lf", &lamda[i - 1]);
            }
          scanf("%*[^\n] ");
        }
      if (py > 8)
        {
          for (i = 5; i <= py - 4; ++i)
            {
              scanf("%lf", &mu[i - 1]);
            }
          scanf("%*[^\n] ");
        }
      /* Sort points into panel order */

      /* nag_2d_panel_sort (e02zac).
       * Sort two-dimensional data into panels for fitting bicubic
       * splines
       */
      nag_2d_panel_sort(px, py, lamda, mu, m, x, y, point, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_2d_panel_sort (e02zac).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      /* Output points in panel order */
      for (i = 1; i <= nadres; ++i)
        {
          printf("\n%s%4ld\n\n", "Panel", i);
          iadres = m + i;
          while ((iadres = point[iadres - 1]) > 0)
            {
              printf("%7.2f%7.2f\n", x[iadres - 1], y[iadres - 1]);
            }
        }
 END:
      NAG_FREE(lamda);
      NAG_FREE(mu);
      NAG_FREE(x);
      NAG_FREE(y);
      NAG_FREE(point);
    }
  return exit_status;
}