// This program reads in the vertices of a polygon from standard // input, and determines whether the vertices have been entered in // cw or ccw order. It finds the lowest and rightmost vertex, and // computes the cross-product of the vectors along its incident // edges. // Written by Joseph O'Rourke, 25 August 1995. // orourke@cs.smith.edu #include #include #include #define NMAX 100 // Maximum number of polygon vertices #define X 0 #define Y 1 int ReadPoly( void ); int FindLR( int n ); int Ccw( int n, int m ); int poly[NMAX][2]; int main() { int n, m; n = ReadPoly(); m = FindLR( n ); if ( Ccw( n, m ) == 1 ) cout << "The polygon is oriented counterclockwise" << endl; else cout << "The polygon is oriented clockwise" << endl; } int ReadPoly() { int i,n; i = 0; while( !cin.eof() ) { cin >> poly[i][X] >> poly[i][Y]; i++; } n = i-1; // Echo polygon coordinates. cout << "n=" << n << " vertices read." << endl; for( i=0; i min[X]) ) ) { m = i; min[X] = poly[m][X]; min[Y] = poly[m][Y]; } //cout << "i=" << i << " m=" << m; } cout << " min = " << min[X] << ", " << min[Y] << endl; return m; } int Ccw( int n, int m ) { int m1, area2; int i; int a[2], b[2], c[2]; //Just renaming m1 = (m + (n-1)) % n; //= m - 1 //cout << "m=" << m << ", m1=" << m1 << endl; // assign a[0] to point to poly[m1][0] etc. for( i = 0; i < 2; i++ ) { a[i] = poly[m1][i]; b[i] = poly[m][i]; c[i] = poly[(m+1)%n][i]; } area2 = a[0] * b[1] - a[1] * b[0] + a[1] * c[0] - a[0] * c[1] + b[0] * c[1] - c[0] * b[1]; cout << "Ccw: area = " << area2 << endl; if ( area2 > 0 ) return 1; else if ( area2 < 0 ) return -1; else return 0; }