题解:P13338 三角形面积并 加强版

· · 题解

P13338 三角形面积并(加强版)题解

思路分析

本题题意是计算 n 个三角形的并集面积,由于数据范围 n \le 1000 ,需要 O(V ^2\log V)(V\texttt{是顶点总数})的算法。

稍微分析即可转化为求多个不规则多边形面积,我们采用扫描图形边界,对边界积分解决。大概意思就是这样,下面为复杂的数学推导:

接下来引入关键数学工具:

Step 0. Green 定理

其表述如下:

设平面闭区域 S 由分段光滑的闭曲线 \partial S 围成,\partial S 取逆时针方向,若函数 P(x,y)、Q(x,y) 在 S 上具有一阶连续偏导数,则有:

\iint_S \left( \frac{\partial Q}{\partial x} - \frac{\partial P}{\partial y} \right) dxdy = \oint_{\partial S} Pdx + Qdy

证明

光有这个公式可不行,怎么用它来求并集面积呢?为了方便计算,我们取 P = -y,Q = x,代入定理可得:

\iint_S \left( \frac{\partial x}{\partial x} - \frac{\partial (-y)}{\partial y} \right) dxdy = \oint_{\partial S} (-y)dx + xdy

化简左侧积分:\iint_S (1 + 1)dxdy = 2\iint_S dxdy = 2 \cdot \text{Area}(S)

所以平面区域 S 的面积公式为:

\text{Area}(S) = \frac{1}{2} \oint_{\partial S} (x dy - y dx) \tag{1}

现在,我们只需找到三角形并集 S = \bigcup_{i=1}^n S_i 的边界 \partial S,沿边界积分即可。

以下操作是对边界进行的处理推导:

Step 1.参数化边

为进行曲线积分,需对边参数化。

设当前处理的边为线段 PQ,其中 P(x_p, y_p)、Q(x_q, y_q)。

边向量 \vec{v} = Q - P = (x_q - x_p, y_q - y_p)

边的长度 L = |\vec{v}| = \sqrt{(x_q - x_p)^2 + (y_q - y_p)^2}

单位方向向量 \hat{v} = \frac{\vec{v}}{L} = \left( \frac{x_q - x_p}{L}, \frac{y_q - y_p}{L} \right),满足 |\hat{v}| = 1

为计算曲线积分,用参数 t 表示边上的点,t \in [0, L],则边上一点 X(t) 可表示为:

X(t) = P + t \cdot \hat{v} = \left( x_p + t \cdot \frac{x_q - x_p}{L}, y_p + t \cdot \frac{y_q - y_p}{L} \right) \tag{2}

当 t=0 时,X(0) = P;当 t=L 时,X(L) = Q,恰好覆盖整条线段 PQ 。

Step 2. 覆盖区间推导

找当前边 PQ 覆盖的区间,求解交点并映射到参数 t。

线段 PQ 所在直线的一般式为 ax + by + c = 0,由两点式推导:

两点式直线方程:\frac{y - y_p}{y_q - y_p} = \frac{x - x_p}{x_q - x_p}

整理得:(y_p - y_q)x + (x_q - x_p)y + (x_p y_q - x_q y_p) = 0

令 a = y_p - y_q,b = x_q - x_p,c = x_p y_q - x_q y_p,即直线 PQ: ax + by + c = 0。

设直线 MN : a'x + b'y + c' = 0,联立两直线方程:

\begin{cases} ax + by = -c \\ a'x + b'y = -c' \end{cases}

由克莱姆法则,当系数行列式 D = ab' - a'b \neq 0(两直线不平行)时,交点 (x_{inter}, y_{inter}) 为:

x_{inter} = \frac{-cb' + bc'}{D}, \quad y_{inter} = \frac{-ac' + ca'}{D} \tag{3}

用点积判断交点是否同时落在两条线段上,对线段 MN,交点 inter 满足 \text{dot}(inter - M, inter - N) \leq \varepsilon

交点 inter 映射到当前边 PQ 的参数 t,推导:

代入 $\hat{v}$ 的表达式,化简得: $$t = \frac{(x_{inter} - x_p)(x_q - x_p) + (y_{inter} - y_p)(y_q - y_p)}{L} \tag{4}$$ 对每个其他三角形,其三条边与当前边 $PQ$ 的交点,会映射出若干参数 $t$,两两配对形成区间 $[t_1, t_2]$。 ### Step 3. 提取边界与积分 一、提取边界 将覆盖区间转化为事件:区间左端点 $(t_1, +1)$(进入覆盖)、区间右端点 $(t_2, -1)$(离开覆盖),同时加入边界事件 $(0, 0)$、$(L, 0)$。 将事件点按 $t$ 排序,扫描时维护覆盖计数 $cover$: - 当 $cover = 0$ 时,对应区间 $[t_j, t_{j+1}]$ 未被任何其他三角形覆盖,即为外露边界段。 二、积分 对每个外露边界段 $[t_a, t_b]$,对应边上两点 $A = X(t_a)$、$B = X(t_b)$,代入公式 (1) 计算积分。 由于 $A$、$B$ 之间为直线段,积分可离散化计算,推导如下: 公式 (1) 中,$\oint_{\partial S} (x dy - y dx)$ 对线段 $AB$ 的积分的为 $\int_A^B (x dy - y dx)

由表达式 (2),dx = \frac{x_q - x_p}{L} dt,dy = \frac{y_q - y_p}{L} dt,代入积分:

\int_{t_a}^{t_b} \left( X_x(t) \cdot \frac{y_q - y_p}{L} - X_y(t) \cdot \frac{x_q - x_p}{L} \right) dt

化简后,最终公式为:

\int_A^B (x dy - y dx) = (A_y + B_y) \cdot |B_x - A_x| \tag{5}

因此,该外露段的面积贡献为 \frac{1}{2} \times (5) 式的结果,再根据 PQ 的方向确定符号,累加所有外露段贡献得到总面积。

Step 4. 实现细节

最终我们AC了,请看AC代码

AC Code

#include <bits/stdc++.h>
using namespace std;

typedef long double ld;
const int N = 1005;
const ld eps = 1e-11;

mt19937 rnd(527398);

struct Node {
    ld x, y;
    void perturb() {
        x += rnd() % ((int)1e8) * 1e-17;
        y += rnd() % ((int)1e8) * 1e-17;
    }
};

Node operator+(Node a, Node b) { return {a.x + b.x, a.y + b.y}; }
Node operator-(Node a, Node b) { return {a.x - b.x, a.y - b.y}; }
Node operator*(Node a, ld k) { return {a.x * k, a.y * k}; }
Node operator/(Node a, ld k) { return {a.x / k, a.y / k}; }

ld dot(Node a, Node b) { return a.x * b.x + a.y * b.y; }
ld len(Node a) { return sqrtl(dot(a, a)); }
ld det(Node a, Node b) { return a.x * b.y - a.y * b.x; }

Node normalize(Node a) { return a / len(a); }

struct Line {
    ld a, b, c;
    ld eval(Node p) { return a * p.x + b * p.y + c; }
};

Node intersect(Line p, Line q) {
    ld D = p.a * q.b - p.b * q.a;
    return {
        (-p.c * q.b + p.b * q.c) / D,
        (-p.a * q.c + p.c * q.a) / D
    };
}

Line pointsToLine(Node p, Node q) {
    return {p.y - q.y, q.x - p.x, det(p, q)};
}

Node tri[N][3];
int n;

int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        for (int j : {0, 1, 2}) {
            cin >> tri[i][j].x >> tri[i][j].y;
            tri[i][j].perturb();
        }
        if (det(tri[i][1] - tri[i][0], tri[i][2] - tri[i][0]) < 0)
            swap(tri[i][1], tri[i][2]);
    }

    ld ans = 0;
    for (int i = 1; i <= n; ++i) {
        for (int p : {0, 1, 2}) {
            vector<pair<ld, int>> events;
            Node edge = tri[i][(p + 1) % 3] - tri[i][p];
            ld L = len(edge);
            Node dir = normalize(edge);
            events.emplace_back(0, 0);
            events.emplace_back(L, 0);
            Line base = pointsToLine(tri[i][p], tri[i][(p + 1) % 3]);

            for (int j = 1; j <= n; ++j) {
                if (j == i) continue;
                vector<ld> proj;
                for (int o : {0, 1, 2}) {
                    Line other = pointsToLine(tri[j][o], tri[j][(o + 1) % 3]);
                    Node inter = intersect(base, other);
                    if (dot(inter - tri[j][o], inter - tri[j][(o + 1) % 3]) <= eps)
                        proj.push_back(dot(inter - tri[i][p], dir));
                }
                if (proj.empty()) continue;
                if (proj.size() == 1) proj.push_back(proj[0]);
                if (proj[0] > proj[1]) swap(proj[0], proj[1]);
                events.emplace_back(proj[0], 1);
                events.emplace_back(proj[1], -1);
            }

            sort(events.begin(), events.end());
            int cover = 0;
            ld area = 0;
            for (int j = 0; j + 1 < events.size(); ++j) {
                cover += events[j].second;
                if (cover == 0 && events[j].first >= -eps && events[j + 1].first <= L + eps) {
                    Node A = tri[i][p] + dir * events[j].first;
                    Node B = tri[i][p] + dir * events[j + 1].first;
                    area += 0.5 * (A.y + B.y) * fabsl(B.x - A.x);
                }
            }
            base.b > 0 ? ans -= area : ans += area;
        }
    }
    printf("%.12Lf\n", ans);
    return 0;
}

这个题解写了好久,被打回了好多次,求通过。