1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include <bits/stdc++.h> #define STC static #define LL long long #define DB double #define DRG default_random_engine #define URD uniform_real_distribution using namespace std; const int N = 2000 + 5; const DB PI = acos(-1), eps = 1e-12; DRG e{20051221}; URD<DB> u{-0.5, 0.5}; struct Point { DB x, y, z; void shake() { x += u(e) * eps, y += u(e) * eps, z += u(e) * eps; } Point operator - (const Point &t) const { return {x - t.x, y - t.y, z - t.z}; } DB operator & (const Point &t) const { return x * t.x + y * t.y + z * t.z; } Point operator * (const Point &t) const { return {y * t.z - t.y * z, z * t.x - x * t.z, x * t.y - y * t.x}; } DB len() { return sqrt(x * x + y * y + z * z); } } ; struct Plane { Point v[3]; int id[3]; Point norm() { return (v[1] - v[0]) * (v[2] - v[0]); } DB area() { return norm().len() / 2; } bool above(Point x) { return ((x - v[0]) & norm()) >= 0; } } ; void convex_3d(Point x[], int len, Plane p[], int &lp) { STC bool g[N][N]; STC Plane np[N]; p[lp++] = {x[0], x[1], x[2], 0, 1, 2}; p[lp++] = {x[2], x[1], x[0], 2, 1, 0}; for (int i = 3; i < len; ++i) { int cnt = 0; for (int j = 0; j < lp; ++j) { bool t = p[j].above(x[i]); if (!t) np[cnt++] = p[j]; for (int k = 0; k < 3; ++k) g[p[j].id[k]][p[j].id[(k + 1) % 3]] = t; } for (int j = 0; j < lp; ++j) for (int k = 0; k < 3; ++k) { int a = p[j].id[k], b = p[j].id[(k + 1) % 3]; if (g[a][b] && !g[b][a]) np[cnt++] = {x[a], x[b], x[i], a, b, i}; } lp = cnt; for (int j = 0; j < lp; ++j) p[j] = np[j]; } } int main() { int n, m; scanf("%d", &n); STC Point q[N]; STC Plane p[N]; for (int i = 0; i < n; ++i) scanf("%lf %lf %lf", &q[i].x, &q[i].y, &q[i].z), q[i].shake(); convex_3d(q, n, p, m); DB ans = 0; for (int i = 0; i < m; ++i) ans += p[i].area(); printf("%.3lf\n", ans); return 0; }
|