/* Sample solution to extreme edge. */ #include #include #include #define MAX_P_SIZE 100 #define MIN_P_SIZE 3 #define MAX_XY_VALUE 10000 static int PX[MAX_P_SIZE]; static int PY[MAX_P_SIZE]; int LeftOf( int a, int b, int c ) { if( (((PX[b] - PX[a]) * (PY[c] - PY[a])) - ((PX[c] - PX[a]) * (PY[b] - PY[a]))) > 0 ) { return( 1 ); } else { return( 0 ); } } int main( int argc, char* argv[] ) { char buffer[256]; int n = 0; int i = 0; int j = 0; int bottomRightVertex = MAX_P_SIZE; int extremeEdgeVertex = MAX_P_SIZE; int checkVertex = MAX_P_SIZE; int edgePos = -1; /* Input. */ fgets( buffer, 256, stdin ); sscanf( buffer, "%d", &n ); assert( (MIN_P_SIZE <= n) && (n <= MAX_P_SIZE) ); printf( "I got %d points.\n", n ); for( i=0; i < n; i+=1 ) { fgets( buffer, 256, stdin ); sscanf( buffer, "%d %d", PX + i, PY + i ); assert( (*(PX + i) < MAX_XY_VALUE) && (*(PY + i) < MAX_XY_VALUE) ); printf( "(%d, %d)\n", PX[i], PY[i] ); } /* Step 1: Find bottom right vertex v. */ bottomRightVertex = 0; for( i=1; i < n; i+=1 ) { if( (PY[i] < PY[bottomRightVertex]) || ((PY[i] == PY[bottomRightVertex]) && (PX[i] > PX[bottomRightVertex])) ) { bottomRightVertex = i; } } printf( "Bottom right vertex is %d = (%d, %d)\n", bottomRightVertex, PX[bottomRightVertex], PY[bottomRightVertex] ); /* Step 2: Form an edge vx, for some x in P\{v}. Determine whether LeftOf(v,x,y_1)=LeftOf(v,x,y_2)=...=LeftOf(v,x,y_(n-2). If so, vx is an extreme edge. */ for( i=1; i < n; i+=1 ) { extremeEdgeVertex = (bottomRightVertex + i) % n; edgePos = -1; for( j=1; j < n; j+=1 ) { checkVertex = (j + i) % n; if( (checkVertex != extremeEdgeVertex) ) { printf( "Checking %d, %d, %d.\n", bottomRightVertex, extremeEdgeVertex, checkVertex ); if( edgePos < 0 ) { edgePos = LeftOf( bottomRightVertex, extremeEdgeVertex, checkVertex ); printf( "This is the reference point. LeftOf = %d\n", edgePos ); } else if ( edgePos != LeftOf( bottomRightVertex, extremeEdgeVertex, checkVertex ) ) { printf( "Mismatch, try next point.\n\n" ); edgePos = -1; break; } } } if( edgePos >= 0 ) { /* Output extreme edge. */ printf( "%d\n%d\n", bottomRightVertex, extremeEdgeVertex ); break; } } return( 0 ); }