/* 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 #include #include #include #include using namespace std; // CGAL setup #include #include #include typedef CGAL::Cartesian ptRep; typedef CGAL::Point_2 Point_2; typedef list ptList; int main(int argc, char* argv[]) { if (argc != 2) { cerr << "Usage: " << argv[0] << " \n"; exit(-1); } ifstream inpFile(argv[1]); 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) { 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; }