2016夏季练习

来源: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[n].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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值