POJ 2318 点在凸四边形内的判断(叉积+二分)

题目链接:http://poj.org/problem?id=2318


题解:叉积+二分 求出每一个toy所在的区域


#include<cstdio>
#include<cmath>
#include<cstring>
struct Point
{
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) {}
};

typedef Point Vector; //Vector 为 Point的别名

//向量+向量=向量    点+向量=点
Vector operator + (Vector A, Vector B) {return Vector(A.x+B.x, A.y+B.y);}

//点-点=向量
Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}

//向量*数=向量
Vector operator * (Vector A, double p) {return Vector(A.x*p, A.y*p);}

//向量/数=向量
Vector operator / (Vector A, double p) {return Vector(A.x/p, A.y/p);}

bool operator < (const Point & a, const Point & b)
{
	return a.x < b.x || (a.x == b.x && a.y < b.y);
}

const double eps = 1e-10;
int dcmp(double x)
{
	if(fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}

//叉积:两向量v和w的叉积等于v和w组成的三角形的有向面积的两倍 XaYb - XbYa
double Cross(Vector A, Vector B)
{
	return A.x*B.y - A.y*B.x;
}

#define MAXN 5050
Point up[MAXN], low[MAXN];
int main ()
{
    int n, m, x1, y1, x2, y2;
    while(scanf("%d", &n), n)
    {
        scanf("%d %d %d %d %d", &m, &x1, &y1 , &x2, &y2);

        up[0].x    = x1,  up[0].y    = y1;
        low[0].x   = x1,  low[0].y   = y2;
        up[n+1].x  = x2,  up[n+1].y  = y1;
        low[n+1].x = x2,  low[n+1].y = y2;

        for(int i = 1; i <= n; i++)
        {
            scanf("%lf %lf", &up[i].x, &low[i].x);
            up[i].y = y1;
            low[i].y = y2;
        }

        int ans[MAXN] = {0};
        for(int i = 1; i <= m; i++)
        {
            Point tmp;
            scanf("%lf %lf", &tmp.x, &tmp.y);

            //二分
            int l = 0, r = n+1;
            while(l < r)
            {
                int M = l + (r-l)/2;
                if(dcmp(Cross(up[M]-low[M], tmp-low[M]))>0) r = M;
                else l = M+1;
            }
            ans[l-1]++;
        }
         for(int i = 0; i <= n; i++)
            printf("%d: %d\n", i, ans[i]);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值