题解:P13338 三角形面积并 加强版
yanjin9540 · · 题解
P13338 三角形面积并(加强版)题解
思路分析
本题题意是计算
稍微分析即可转化为求多个不规则多边形面积,我们采用扫描图形边界,对边界积分解决。大概意思就是这样,下面为复杂的数学推导:
接下来引入关键数学工具:
Step 0. Green 定理
其表述如下:
设平面闭区域
证明
光有这个公式可不行,怎么用它来求并集面积呢?为了方便计算,我们取
化简左侧积分:
所以平面区域
现在,我们只需找到三角形并集
以下操作是对边界进行的处理推导:
Step 1.参数化边
为进行曲线积分,需对边参数化。
设当前处理的边为线段
边向量
边的长度
单位方向向量
为计算曲线积分,用参数
当
Step 2. 覆盖区间推导
找当前边
线段
两点式直线方程:
整理得:
令
设直线
由克莱姆法则,当系数行列式
用点积判断交点是否同时落在两条线段上,对线段
交点
由表达式 (2),
化简后,最终公式为:
因此,该外露段的面积贡献为
Step 4. 实现细节
- 对顶点坐标加极小随机扰动,避免浮点精度问题——这点不加会导致部分WA。
-
将所有三角形顶点调整为逆时针顺序,保证积分方向一致。
最终我们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;
}
这个题解写了好久,被打回了好多次,求通过。