/*
 * points.c
 * This program reads several input points, computes their centre of mass
 * and locates the points which are closest and farthest from this centre.
 */

#include <stdio.h>
#include <math.h>

/* Definition of a point: */
struct _Point
{
  float x;
  float y;
};

typedef struct _Point Point;

/* Prototypes: */
float point_dist (const Point *p, const Point *q);

int main ()
{
  int    n_points;    /* The number of points. */
  Point  *points;     /* The points themselves. */
  Point  cm;          /* The centre of mass. */
  float  dist;        /* The distance of the current point from the centre. */
  float  min_dist;    /* The minimal distance so far. */
  float  max_dist;    /* The maximal distance so far. */
  int    i_close;     /* The index of the closest point so far. */
  int    i_far;       /* The index of the farthest point so far. */
  int    i;

  /* Read the number of input points: */
  do
  {
    printf ("Please enter the number of points: ");
    scanf ("%d", &n_points);
  } while (n_points <= 0);

  /* Allocate memory to store the points. */
  points = (Point *) malloc (sizeof(Point) * n_points);
 
  /* Read the points. */
  for (i = 0; i < n_points; i++)
  {
    printf ("Please enter point no. %d: ", i+1);
    scanf ("%f %f", &(points[i].x), &(points[i].y));
  }

  /* Compute the centre of mass by averaging the point coordinates. */
  cm.x = 0;
  cm.y = 0;

  for (i = 0; i < n_points; i++)
  {
    cm.x += points[i].x;
    cm.y += points[i].y;
  }
  cm.x /= (float)n_points;
  cm.y /= (float)n_points;

  /* Print the result. */
  printf ("The centre of the %d points is (%g,%g).\n", 
          n_points, cm.x, cm.y);
  
  /* Find the closest and the farthest point. */
  min_dist = max_dist = point_dist (points, &cm);
  i_close = i_far = 0;

  for (i = 1; i < n_points; i++)
  {
    dist = point_dist (points + i, &cm);
    
    /* Check if the current point is closer to the centre of mass than
     * the closest point so far. */
    if (dist < min_dist)
    {
      min_dist = dist;
      i_close = i;
    }
    /* Check if the current point is farther from the centre of mass than
     * the farthest point so far. */
    else if (dist > max_dist)
    {
      max_dist = dist;
      i_far = i;
    }
  }

  /* Print the closest and farthest points. */
  printf ("The point closest to the centre is (%g,%g).\n",
          points[i_close].x, points[i_close].y);
  printf ("The point farthest from the centre is (%g,%g).\n",
          points[i_far].x, points[i_far].y);

  /* Free memory. */
  free (points);

  return (0);
}

/* ------------------------------------------------------------------------
 * Function: point_dist
 * Purpose : Compute the Euclidean distance between two points.
 * Input   : p - The first point.
 *           q - The second point.
 * Returns : The distance.
 */
float point_dist (const Point *p, const Point *q)
{
  return ((float) sqrt ((p->x - q->x)*(p->x - q->x) + 
                        (p->y - q->y)*(p->y - q->y)));
}

