计算几何样例

//还不错的计算几何模板,mark一下~ http://www.cnblogs.com/luyi0619/archive/2011/02/21/1959775.html

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct POINT
{
    double x,y;
    POINT() {}
    POINT(double d1,double d2)
    {
        x=d1;
        y=d2;
    }
    POINT operator - (const POINT p) const
    {
        POINT ans;
        ans.x= x-p.x;
        ans.y =y-p.y;
        return ans;
    }
    double operator * (const POINT p) const
    {
        return x*p.y-y*p.x;
    }
};
struct Line
{
    POINT px,py;
    Line() {}
    Line (POINT p1,POINT p2)
    {
        px=p1;
        py=p2;
    }
} line[1010];
double direction(POINT pi,POINT pj,POINT pk)
{
    return (pk-pi)*(pj-pi);
}
bool on_segments(POINT pi,POINT pj,POINT pk)
{
    if(min(pi.x,pj.x)<=pk.x && pk.x<=max(pi.x,pj.x) && min(pi.y,pj.y)<=pk.y && pk.y<=max(pi.y,pj.y))
        return true;
    else
        return false;
}
bool segments_intersect(POINT p1,POINT p2,POINT p3,POINT p4)
{
    double d1=direction(p3,p4,p1);
    double d2=direction(p3,p4,p2);
    double d3=direction(p1,p2,p3);
    double d4=direction(p1,p2,p4);
    if(((d1>0 && d2<0)||(d1<0 && d2>0))&&((d3>0 && d4<0)||(d3<0 && d4>0)))
        return true;
    else if(d1==0 && on_segments(p3,p4,p1))
        return true;
    else if(d2==0 && on_segments(p3,p4,p2))
        return true;
    else if(d3==0 && on_segments(p1,p2,p3))
        return true;
    else if(d4==0 && on_segments(p1,p2,p4))
        return true;
    else
        return false;
}
int ans[1010],to[1010];
bool cmp(Line l1,Line l2)
{
    return l1.px.x < l2.px.x;
}
int main()
{
    int n,m;
    double x1,y1,x2,y2;
    while(scanf("%d %d %lf %lf %lf %lf",&n,&m,&x1,&y1,&x2,&y2)==6)
    {
        memset(ans,0,sizeof(ans));
        memset(to,0,sizeof(to));
        line[0] = Line(POINT(x1,y2),POINT(x1,y1));
        for(int i=1; i<=n; i++)
        {
            double ui,li;
            scanf("%lf %lf",&ui,&li);
            line[i] = Line(POINT(li,y2),POINT(ui,y1));
        }
        line[n+1] = Line(POINT(x2,y2),POINT(x2,y1));
        sort(line,line+n+2,cmp);
        for(int i=1; i<=m; i++)
        {
            double x,y;
            scanf("%lf %lf",&x,&y);
            Line now(POINT(x1,y2),POINT(x,y));
            int low = 0,high = n,mid,res;
            while(low<=high)
            {
                mid = (low + high)/2;
                if(segments_intersect(now.px,now.py,line[mid].px,line[mid].py))
                {
                    low = mid + 1;
                    res = mid;
                }
                else
                    high = mid-1;
 
            }
            ans[res]++;
        }
        for(int i=0; i<=n; i++)
            if(ans[i]>0)
                to[ans[i]]++;
        printf("Box\n");
        for(int i=0; i<=n; i++)
        {
            if(to[i]>0)
                printf("%d: %d\n",i,to[i]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值