码迷,mamicode.com
首页 > 其他好文 > 详细

TOYS - POJ 2318(计算几何,叉积判断)

时间:2015-09-05 17:46:14      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数。

 
分析:做的第一道计算几何题目....使用叉积判断方向,然后使用二分查询找到点所在的区域。
 
代码如下:
============================================================================================================================
#include<stdio.h>
#include<math.h>
using namespace std;

const int MAXN = 5e3+7;
const double PI = acos(-1.0);

struct point
{
    double x, y;

    point(int x=0, int y=0):x(x), y(y){}
};
struct Vector
{
    point a, b;

    void InIt(point t1, point t2){a=t1, b=t2;}
    double operator * (const point &p) const
    {
        return (p.x-b.x)*(a.y-b.y) - (p.y-b.y)*(a.x-b.x);
    }
};

Vector line[MAXN];

int Find(int N, point a)
{
    int L=0, R=N;

    while(L <= R)
    {
        int Mid = (L+R) >> 1;

        if(line[Mid] * a < 0)
            R = Mid - 1;
        else
            L = Mid + 1;
    }

    return R;
}

int main()
{
    int M, N;
    double x1, x2, y1, y2, ui, li;

    while(scanf("%d", &N) != EOF && N)
    {
        scanf("%d%lf%lf%lf%lf", &M, &x1, &y1, &x2, &y2);

        int ans[MAXN]={0};

        line[0].InIt(point(x1, y1), point(x1, y2));
        for(int i=1; i<=N; i++)
        {
            scanf("%lf%lf", &ui, &li);
            line[i].InIt(point(ui, y1), point(li, y2));
        }
        while(M--)
        {
            scanf("%lf%lf", &ui, &li);
            int i = Find(N, point(ui, li));

            ans[i] += 1;
        }

        for(int i=0; i<=N; i++)
            printf("%d: %d\n", i, ans[i]);
        printf("\n");
    }

    return 0;
}

 

TOYS - POJ 2318(计算几何,叉积判断)

标签:

原文地址:http://www.cnblogs.com/liuxin13/p/4783452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!