POJ2318(计算几何)

题意是给你n个边界和m个点,求出每一块区域里面点的个数。

对于每一个点,算出每条边界在这个y坐标下对应的x坐标,二分查找就好了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 5111
#define x1 x_1
#define x2 x_2
#define y1 y_1
#define y2 y_2
#define find Find

struct node {
    double x1, y1, x2, y2;
    double x, y;
    bool operator < (const node &a) const {
        return x < a.x;
    }
}a[maxn], b[maxn];
int n, m, ans[maxn];
double x1, y1, x2, y2;

void find (int pos) {
    double h = b[pos].y;
    for (int i = 0; i < n; i++) {
        double rate = (h-a[i].y1)/(a[i].y2-a[i].y1);
        a[i].x = a[i].x1 + rate*(a[i].x2-a[i].x1);
    }
    int p = upper_bound (a, a+n, b[pos]) - a-1;
    ans[p]++;
}

int main () {
    //freopen ("in", "r", stdin);
    while (scanf ("%d", &n) == 1 && n) {
        scanf ("%d%lf%lf%lf%lf", &m, &x1, &y1, &x2, &y2);
        a[0].x1 = x1, a[0].y1 = y2, a[0].x2 = x1, a[0].y2 = y1;
        double u, v;
        for (int i = 1; i <= n; i++) {
            scanf ("%lf%lf", &u, &v);
            a[i].x1 = v, a[i].x2 = u;
            a[i].y1 = y2, a[i].y2 = y1;
        }
        n++;
        a[n].x1 = x2, a[n].y1 = y2;
        a[n].x2 = x2, a[n].y2 = y1;
        n++;
        memset (ans, 0 ,sizeof ans);
        for (int i = 1; i <= m; i++) {
            scanf ("%lf%lf", &u, &v);
            b[i].x = u, b[i].y = v;
            find (i);
        }
        for (int i = 0; i < n-1; i++) {
            printf ("%d: %d\n", i, ans[i]);
        }
        printf ("\n");
    }
    return 0;
}


你可能感兴趣的:(POJ2318(计算几何))