这个题要考虑直线在正方形的内部;顶点给你的顺序不一定就是左上角到右下角;
View Code
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<map> #include<cstring> #include<vector> using namespace std; class Point { public: double x,y; }; class Line { public: Point A,B; }; int dcmp( double x ) { if( fabs( x ) < 1.0e-8) return 0; if( x < 0 ) return -1; return 1; } bool judge( Point p1, Point p2 ,Point q ) { double max_x = p1.x > p2.x ? p1.x : p2.x; double min_x = p1.x < p2.x ? p1.x : p2.x; double max_y = p1.y > p2.y ? p1.y : p2.y; double min_y = p1.y <p2.y ? p1.y : p2.y; if( dcmp( q.x - min_x ) >=0 && dcmp( max_x - q.x )>=0 ) if( dcmp( q.y - min_y ) >=0 && dcmp( max_y - q.y )>=0 ) return true; return false; } double multi( Point p1, Point p2 , Point q ) { return ( p1.x - q.x )*( p2.y - q.y ) - ( p2.x - q.x )*( p1.y - q.y ); } bool segment( Line L , Line L1 ) { int d1 = dcmp(multi( L.A , L.B , L1.A )); int d2 = dcmp(multi( L.A, L.B , L1.B )); int d3 = dcmp(multi( L1.A, L1.B ,L.A )); int d4 = dcmp(multi( L1.A ,L1.B ,L.B )); if( d1*d2 < 0 && d3 *d4 < 0 ) return true; if( d1 ==0 && judge( L.A , L.B , L1.A ) ) return true; if( d2 ==0 && judge( L.A, L.B , L1.B ) ) return true; if( d3 ==0 && judge( L1.A, L1.B ,L.A ) ) return true; if( d4 ==0 && judge( L1.A , L1.B ,L.B ) ) return true; return false; } bool Solve( Line L , Line Re[] ) { for( int i = 0 ; i < 4 ; i ++ ) { if( segment( L ,Re[i] ) ) return true; } return false; } bool judge_again( Line L , Point A[] ) { double max_x = A[0].x > A[2].x ? A[0].x :A[2].x; double min_x = A[0].x < A[2].x ? A[0].x :A[2].x; double max_y = A[0].y > A[2].y ? A[0].y :A[2].y; double min_y = A[0].y < A[2].y ? A[0].y :A[2].y; if( L.A.x >= min_x && L.A.x <= max_x && L.B.x >= min_x && L.B.x <= max_x) if( L.A.y >= min_y && L.A.y <= max_y&&L.B.y >=min_y && L.B.y <= max_y ) return true; return false; } bool cmp( Point a , Point b ) { if( dcmp( a.x - b.x )==0 ) return a.y < b.y; return a.x < b.x; } int main( ) { int n; while( scanf( "%d",&n )==1 ) { while( n-- ) { Line L,Re[4]; Point A[5]; scanf( "%lf %lf",&L.A.x,&L.A.y ); scanf( "%lf %lf",&L.B.x,&L.B.y ); scanf( "%lf %lf %lf %lf",&A[0].x ,&A[0].y ,&A[2].x,&A[2].y ); if( A[0].y < A[2].y ) swap( A[0] ,A[2] ); A[1].x = A[2].x ; A[1].y = A[0].y; A[3].x = A[0].x ; A[3].y = A[2].y; sort( A , A + 4 ,cmp ); A[4] = A[0]; for( int i = 0 ; i < 4 ; i ++ ) { Re[i].A = A[i]; Re[i].B = A[i+1]; } if( Solve( L , Re ) ) { printf( "T\n" ); } else { if( judge_again( L ,A ) ) printf( "T\n" ); else printf( "F\n" ); } } } //system( "pause" ); return 0; }