您的位置:首页 > 其它

2016夏季练习

2016-08-26 10:45 148 查看
来源:POJ2318

计算几何第一题,二分+叉乘

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 5010;
int n,m;
struct Point{
double x,y;
Point(double xx = 0,double yy = 0):x(xx),y(yy){}
Point operator + (Point a){ return Point(x+a.x,y+a.y); }
Point operator - (Point a){ return Point(x-a.x,y-a.y); }
double operator ^ (Point a){ return x*a.y-y*a.x; }
}ul,dr;
struct Segment{
Point ss,ee;
void get(double xx,double yy,double _x,double _y){
ss.x = xx;
ss.y = yy;
ee.x = _x;
ee.y = _y;
}
}s[MAXN];

int cmp(Segment a,Segment b){
return a.ss.x<b.ss.x;
}
int cnt[MAXN];
const double eps = 1e-10;
int sgn(double x){
if(fabs(x) < eps) return 0;
else if(x<0) return -1;
else return 1;
}
int judge(int mid,Point du){
double aaa = (s[mid].ss-du)^(s[mid].ee-du);
double bbb = (s[mid+1].ss-du)^(s[mid+1].ee-du);
if(sgn(aaa)>0 && sgn(bbb)>0) return 1;
else if(sgn(aaa)>0 && sgn(bbb)<0) return 0;
else return -1;
}
int main(){
double u,v;
while(scanf("%d",&n)!=EOF && n){
scanf("%d",&m);
//cout<<"n = "<<n<<" m = "<<m<<endl;
memset(cnt,0,sizeof cnt);
scanf("%lf%lf%lf%lf",&ul.x,&ul.y,&dr.x,&dr.y);
for(int i=0;i<n;i++){
scanf("%lf%lf",&u,&v);
s[i].get(u,ul.y,v,dr.y);
}
s
.get(ul.x,ul.y,ul.x,dr.y);
s[n+1].get(dr.x,ul.y,dr.x,dr.x);
sort(s,s+n+2,cmp);
/* cout<<"result: "<<endl;
for(int i=0;i<n+2;i++){
cout<<"start: "<<s[i].ss.x<<" "<<s[i].ss.y<<endl;
cout<<"end : "<<s[i].ee.x<<" "<<s[i].ee.y<<endl;
}*/
Point du;
while(m--){
scanf("%lf%lf",&du.x,&du.y);
int l = 0;
int r = n;
int mid = 0;
while(l<=r){
mid = (l+r)>>1;
int tmp = judge(mid,du);
//cout<<"mid = "<<mid<<" tmp = "<<tmp<<endl;
if(tmp>0) l = mid+1;
else if(tmp==0) break;
else r = mid-1;
}
//cout<<"mid = "<<mid<<endl;
cnt[mid]++;
}
for(int i=0;i<=n;i++){
cout<<i<<": "<<cnt[i]<<endl;
}
cout<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: