bzoj2618 半平面交

求平面交

例:bzoj2618

傳送門:https://www.lydsy.com/JudgeOnline/problem.php?id=2618

題目大意:求凸多邊形交的核面積

orzorz

就是兩個凸多邊形的交集

上代碼:

 
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstring>
using namespace std;
const double eps = 1e-8;
const int N = 1100;
int read() {
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
double sgn(double x) {return x > eps ? 1 : (x < -eps ? -1 : 0);}
struct P {
    double x, y;
    P(double a = 0, double b = 0) : x(a), y(b) {}
    P operator - (P a) const {return P(x - a.x, y - a.y);}
    P operator + (P a) const {return P(x + a.x, y + a.y);}
    P operator * (double a) {return P(x * a, y * a);}
    P operator / (double a) {return P(x / a, y / a);}
    double operator * (P a) const {return x * a.y - y * a.x;}
}ret[N], p[N];
double xmult(P a, P b, P c) {return (b - a) * (c - a);}
double angle(P a) {return atan2(a.y, a.x);}
  
struct line {
    P a, b; double ang;
    line(P c = P(0, 0), P d = P(0, 0), double e = 0) : a(c), b(d), ang(e) {}
    P operator + (line c) {
        double w1 = xmult(c.a, a, b), w2 = xmult(c.b, b, a);
        return (c.a * w2 + c.b * w1) / (w1 + w2);
    }
}l[N];
int top, id[N], q[N], cnt;
double ans;
bool cmp(int a, int b) {
    int t = sgn(l[a].ang - l[b].ang);
    if(!t) return sgn(xmult(l[a].a, l[b].a, l[b].b)) > 0;
    return t < 0;
}
  
void HPI() {
    int L = 1, R = 2, tot = 1;
    for(int i = 1;i <= top; ++i) id[i] = i;
    sort(id + 1, id + top + 1, cmp);
    for(int i = 2;i <= top; ++i)
        if(sgn(l[id[i]].ang - l[id[i - 1]].ang) > 0)
            id[++tot] = id[i];
    q[1] = id[1]; q[2] = id[2];
    for(int i = 3;i <= tot; ++i) {
        while(L < R && sgn(xmult(l[q[R - 1]] + l[q[R]], l[id[i]].a, l[id[i]].b)) < 0) --R;
        while(L < R && sgn(xmult(l[q[L + 1]] + l[q[L]], l[id[i]].a, l[id[i]].b)) < 0) ++L;
        q[++R] = id[i];
    }
    while(L < R && sgn(xmult(l[q[R - 1]] + l[q[R]], l[q[L]].a, l[q[L]].b)) < 0) --R;
    while(L < R && sgn(xmult(l[q[L + 1]] + l[q[L]], l[q[R]].a, l[q[R]].b)) < 0) ++L;
    q[R + 1] = q[L];
    for(int i = L;i <= R; ++i) ret[++cnt] = l[q[i]] + l[q[i + 1]];
}
  
int main()
{
    int n = read();
    for(int i = 1;i <= n; ++i) {
        int x = read();
        for(int j = 1; j <= x; ++j) p[j].x = read(), p[j].y = read(); 
        p[x + 1] = p[1];
        for(int j = 1;j <= x; ++j) l[++top] = line(p[j], p[j + 1], angle(p[j] - p[j + 1]));
    }
    HPI();
    if(cnt >= 3) {
        ret[cnt+1] = ret[1];
        for(int i = 1; i <= cnt; ++i) ans += ret[i] * ret[i + 1];
    }
    ans = fabs(ans) / 2;
    printf("%.3lf\n", ans);
    return 0;
}

瑟瑟發抖,真EX碼農。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章