第五节 半平面交
  • conv cut(conv &a)
    用此半平面切割凸包,返回新的凸包
  • conv core(poly &a)
    返回多边形 a 的核(站在核中的点,可以看到多边形内的任意一点)
  • conv conv_inter(vector<poly> &a)
    返回 n 个多边形的交(多边形需要逆时针存储)

例题:P4196【模板】半平面交

题目大意:

n 个凸多边形,求其面积交。

namespace Halfplane {
    struct halfplane {
        //ax+by+c<=0
        db a, b, c;
        halfplane(pt p, pt q) {
            a = p.y - q.y;
            b = q.x - p.x;
            c = det(p, q);
        }
        halfplane(db aa, db bb, db cc) {
            a = aa; b = bb; c = cc;
        }
        db calc(pt &t) {
            return t.x * a + t.y * b + c;
        }
        pt intersect(pt &a, pt &b) {
            pt res;
            double t1 = calc(a), t2 = calc(b);
            res.x = (t2 * a.x - t1 * b.x) / (t2 - t1);
            res.y = (t2 * a.y - t1 * b.y) / (t2 - t1);
            return res;
        }
        conv cut(conv &a) {
            int n = (int)a.P.size();
            conv res;
            for (int i = 0; i < n; i++) {
                if (calc(a.P[i]) < -eps) res.P.push_back(a.P[i]);
                else {
                    int j = bef(i);
                    if (calc(a.P[j]) < -eps) res.P.push_back(intersect(a.P[j], a.P[i]));
                    j = nex(i);
                    if (calc(a.P[j]) < -eps) res.P.push_back(intersect(a.P[i], a.P[j]));
                }
            }
            return res;
        }
    };

    conv core(poly &a) {
        conv res;
        res.P.push_back(pt(-INF, -INF));
        res.P.push_back(pt(INF, -INF));
        res.P.push_back(pt(INF, INF));
        res.P.push_back(pt(-INF, INF));
        int n = (int)a.a.size();
        for (int i = 0; i < n; i++) {
            halfplane L(a.a[nex(i)], a.a[i]);
            res = L.cut(res);
        }
        return res;
    }

    conv conv_inter(vector<poly> &a) {
        conv res;
        res.P.push_back(pt(-INF, -INF));
        res.P.push_back(pt(INF, -INF));
        res.P.push_back(pt(INF, INF));
        res.P.push_back(pt(-INF, INF));
        for (int cnt = 0; cnt < a.size(); cnt++) {
            int n = (int)a[cnt].a.size();
            for(int i = 0; i < n; i++) {
                halfplane L(a[cnt].a[nex(i)], a[cnt].a[i]);
                res = L.cut(res);
            }
        }
        return res;
    }
}

int main()
{
    int n, m;
    scanf("%d", &n);
    vector<poly> p(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &m);
        p[i].a.resize(m);
        for(int j = 0; j < m; j++) p[i].a[j].input();
        p[i].fix();
    }
    conv ans = Halfplane::conv_inter(p);
    printf("%.3lf\n", ans.area());
    return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇