/*
  convexhull.cc
  Wes Huang
  January 2001

  This is a sample program that uses the CGAL library.  It reads in a
  simple data file of points and calculates their convex hull.

  There is a sample data file that goes along with this program.

  There is also a Makefile that should compile this program on the new
  CS department Suns.
*/

#include "stdafx.h"

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <list>
using namespace std;

// CGAL setup
#include <CGAL/Cartesian.h>
#include <CGAL/Point_2.h>
#include <CGAL/convex_hull_2.h>
typedef CGAL::Cartesian<float> ptRep;
typedef CGAL::Point_2<ptRep> Point_2;
typedef list<Point_2> ptList;

int main(int argc, char* argv[])
{
	
  ifstream inpFile("testpoints.txt");
  if (!inpFile) {
    cerr << "Cannot open file \"" << argv[1] << "\".\n";
    exit(-1);
  }
  
  // OK, this isn't pretty, but it works.  I used the ifstream so that
  // I wouldn't have to write my own little "read a line" function or
  // remember where I could find one.
  ptList p;
  const int MAXLEN = 100;
  char lineBuffer[MAXLEN];
  while (true) {
    inpFile.getline(lineBuffer, MAXLEN);
    if (inpFile.eof())
      break;

    float x,y;
    if (sscanf(lineBuffer, "%f %f", &x, &y) != 2) {
		break;
      cerr << "Error reading file...\n";
      exit(-1);
    }
    cout << "Read the point (" << x << ", " << y << ")\n";
    
    p.push_back(Point_2(x,y));
  }

  // now that we've got the points, compute the convex hull
  ptList ch;
  CGAL::convex_hull_points_2(p.begin(), p.end(), back_inserter(ch));

  cout << "The points on the convex hull are:\n";
  for (ptList::iterator k = ch.begin(); k != ch.end(); k++) {
    cout << "  (" << (*k).x() << ", " << (*k).y() << ")\n";
  }

  return 0;
}

