题目大意:
箱子有一堆木板隔离开不同区域
给定木板的起末点位置,和一堆物品入箱的坐标,最后来求每块区域的物品个数
这里我们可以很容易得知,一个物品的点所在的区域,和前后两个木板形成的叉积值正负性是正好相反的,所以函数如下:
bool inArea(Point a , Line L1 , Line L2){
double t1 = Cross(a-L1.A , L1.B - L1.A) , t2 = Cross(a-L2.A , L2.B - L2.A);
//cout<<"Cross: "<<t1<<" "<<t2<<endl;
return dcmp(t1)*dcmp(t2) < 0;
}
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 using namespace std; 6 #define eps 1e-10 7 #define N 5005 8 struct Point{ 9 double x,y; 10 Point(double x=0,double y=0):x(x),y(y){} 11 }; 12 13 struct Line{ 14 Point A , B; 15 }line[N]; 16 typedef Point Vector; 17 18 int dcmp(double x){ 19 if(abs(x)<eps) return 0; 20 else return x<0?-1:1; 21 } 22 23 bool operator == (const Point &a , const Point &b){ 24 return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; 25 } 26 27 Vector operator+(Vector a , Vector b){ 28 return Vector(a.x+b.x , a.y+b.y); 29 } 30 31 Vector operator-(Point a, Point b){ 32 return Vector(a.x-b.x,a.y-b.y); 33 } 34 35 Vector operator*(Vector a,double b){ 36 return Vector(a.x*b,a.y*b); 37 } 38 39 Vector operator/(Vector a , double b){ 40 return Vector(a.x/b , a.y /b); 41 } 42 43 double Dot(Vector a,Vector b){ 44 return a.x * b.x + a.y * b.y; 45 } 46 47 double Cross(Vector a,Vector b){ 48 return a.x*b.y - a.y*b.x; 49 } 50 51 bool inArea(Point a , Line L1 , Line L2){ 52 double t1 = Cross(a-L1.A , L1.B - L1.A) , t2 = Cross(a-L2.A , L2.B - L2.A); 53 //cout<<"Cross: "<<t1<<" "<<t2<<endl; 54 return dcmp(t1)*dcmp(t2) < 0; 55 } 56 57 int cnt[N]; 58 59 int main() 60 { 61 //freopen("test.in","rb",stdin); 62 int n,m,x1,y1,x2,y2,ui,li; 63 while(scanf("%d",&n)!=EOF){ 64 if(n == 0) break; 65 66 scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2); 67 68 line[0].A = Point(x1,y2),line[0].B = Point(x1,y1); 69 for(int i=1;i<=n;i++){ 70 scanf("%d%d",&ui,&li); 71 line[i].A.x = li , line[i].A.y = y2; 72 line[i].B.x = ui , line[i].B.y = y1; 73 } 74 line[n+1].A = Point(x2,y2) , line[n+1].B = Point(x2,y1); 75 76 memset(cnt,0,sizeof(cnt)); 77 78 for(int i=0;i<m;i++){ 79 scanf("%d%d",&ui,&li); 80 Point tmp = Point(ui,li); 81 for(int j=0;j<=n;j++){ 82 if(inArea(tmp,line[j],line[j+1])){ 83 cnt[j]++; 84 break; 85 } 86 } 87 } 88 89 for(int i=0;i<=n;i++){ 90 printf("%d: %d ",i,cnt[i]); 91 } 92 puts(""); 93 } 94 return 0; 95 }