View Code
1 /* 2 几何+判断点在四边形内 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 #include<iostream> 9 #include<queue> 10 //#include<map> 11 #include<math.h> 12 using namespace std; 13 typedef long long ll; 14 //typedef __int64 int64; 15 const int maxn = 5405; 16 const int inf = 0x7fffffff; 17 const double pi=acos(-1.0); 18 const double eps = 1e-8; 19 struct point { 20 double x,y; 21 }; 22 struct Rect{ 23 point a,b,c,d; 24 }; 25 Rect rect[ maxn ]; 26 double pos[ maxn ]; 27 int cnt[ maxn ]; 28 double xmult( point sp,point ep,point op ){ 29 return ( sp.x-op.x )*( ep.y-op.y )-( sp.y-op.y )*( ep.x-op.x ); 30 } 31 bool point_in_rect( point p,Rect s ){ 32 if( xmult( s.d,s.a,p )<-eps ) return false; 33 if( xmult( s.a,s.b,p )<-eps ) return false; 34 if( xmult( s.b,s.c,p )<-eps ) return false; 35 if( xmult( s.c,s.d,p )<-eps ) return false; 36 return true; 37 }//判断点是否在Rect这个四边形中 38 int main(){ 39 int n; 40 int ca = 0; 41 while( scanf("%d",&n)==1 ){ 42 if( n==0 ) break; 43 if( ca==0 ) ca++; 44 else printf("\n"); 45 int m; 46 scanf("%d",&m); 47 double x1,x2,y1,y2; 48 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 49 double tx1,tx2; 50 point pre_up,pre_down; 51 pre_up.x = x1; 52 pre_up.y = y1; 53 pre_down.x = x1; 54 pre_down.y = y2; 55 for( int i=0;i<n;i++ ){ 56 scanf("%lf%lf",&tx1,&tx2); 57 rect[ i ].a.x = pre_up.x; 58 rect[ i ].a.y = pre_up.y; 59 rect[ i ].b.x = pre_down.x; 60 rect[ i ].b.y = pre_down.y; 61 pre_up.x = tx1; 62 pre_down.x = tx2; 63 rect[ i ].d.x = pre_up.x; 64 rect[ i ].d.y = pre_up.y; 65 rect[ i ].c.x = pre_down.x; 66 rect[ i ].c.y = pre_down.y; 67 pos[ i ] = min( rect[ i ].a.x,rect[ i ].b.x );//pos记录每个四边形的x的最小值!!! 68 } 69 rect[ n ].a.x = pre_up.x; 70 rect[ n ].a.y = pre_up.y; 71 rect[ n ].b.x = pre_down.x; 72 rect[ n ].b.y = pre_down.y; 73 pre_up.x = x2; 74 pre_down.x = x2; 75 rect[ n ].d.x = pre_up.x; 76 rect[ n ].d.y = pre_up.y; 77 rect[ n ].c.x = pre_down.x; 78 rect[ n ].c.y = pre_down.y; 79 pos[ n ] = min( rect[ n ].a.x,rect[ n ].b.x ); 80 memset( cnt,0,sizeof( cnt ) ); 81 point tmp; 82 /* 83 for( int i=0;i<=n;i++ ){ 84 printf("%d:\n",i); 85 printf("[%lf %lf][%lf %lf]\n [%lf %lf][%lf %lf]\n\n",rect[i].a.x,rect[i].a.y,rect[i].b.x,rect[i].b.y,rect[i].c.x,rect[i].c.y,rect[i].d.x,rect[i].d.y); 86 } 87 88 for( int i=0;i<=n;i++ ) 89 printf("%lf\n",pos[ i ]); 90 */ 91 for( int num=1;num<=m;num++ ){ 92 scanf("%lf%lf",&tmp.x,&tmp.y); 93 /* 94 for( int i=0;i<=n;i++ ){ 95 if( (tmp.x>=min(rect[i].a.x,rect[i].b.x))&&point_in_rect( tmp,rect[ i ] )==true ){ 96 cnt[ i ]++; 97 //printf("%d:%d\n",num,i); 98 break; 99 } 100 } 101 */ 102 int pos_s; 103 pos_s = lower_bound( pos,pos+n+1,tmp.x ) - pos; 104 //printf("num:%d %d\n",num,pos_s); 105 for( int i=min(n,pos_s+1);i>=0;i-- ){ 106 if( point_in_rect( tmp,rect[ i ] )==true ){ 107 cnt[ i ]++; 108 //printf("%d:%d\n",num,i); 109 break; 110 } 111 } 112 } 113 114 for( int i=0;i<=n;i++ ){ 115 printf("%d: %d\n",i,cnt[ i ]); 116 } 117 //if( ca ) printf("\n"); 118 } 119 return 0; 120 }