http://acm.hdu.edu.cn/showproblem.php?pid=3662

求给定空间中的点的三维凸包上有多少个面。

用增量法,不断加入点,把新加的点能看到的面都删掉,不能看到的面与能看到的面的棱与新点相连构成一个新的三角形面。

这样的面全都是三角形,注意最后统计答案时要把重合的面算成一个。

时间复杂度\(O(n^2)\)。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 303;
const double eps = 1e-6; struct Point {
double x, y, z;
Point(double _x = 0, double _y = 0, double _z = 0) : x(_x), y(_y), z(_z) {}
Point operator + (const Point &A) const {
return Point(x + A.x, y + A.y, z + A.z);
}
Point operator - (const Point &A) const {
return Point(x - A.x, y - A.y, z - A.z);
}
double operator * (const Point &A) const {
return x * A.x + y * A.y + z * A.z;
}
Point operator ^ (const Point &A) const {
return Point(y * A.z - z * A.y, z * A.x - x * A.z, x * A.y - y * A.x);
}
double sqrlen() {
return x * x + y * y + z * z;
}
} P[N]; struct Face {
int a, b, c; bool ex;
Face(int _a = 0, int _b = 0, int _c = 0, bool _ex = false) : a(_a), b(_b), c(_c), ex(_ex) {}
} F[N * N]; int n, ftot, LeftFace[N][N]; void insFace(int a, int b, int c, int n1, int n2, int n3) {
F[++ftot] = (Face) {a, b, c, true};
LeftFace[a][b] = LeftFace[b][c] = LeftFace[c][a] = ftot;
LeftFace[b][a] = n1;
LeftFace[c][b] = n2;
LeftFace[a][c] = n3;
} bool visible(int f, int p) {
Point a = P[F[f].b] - P[F[f].a], b = P[F[f].c] - P[F[f].a];
return (P[p] - P[F[f].a]) * (a ^ b) > eps;
} int st, to[N], ps[N], pt[N], ptot = 0, pf[N]; void dfs(int x, int s, int t, int p) {
if (F[x].ex == false) return; if (visible(x, p))
F[x].ex = false;
else {
to[st = s] = t;
return;
} dfs(LeftFace[F[x].b][F[x].a], F[x].a, F[x].b, p);
dfs(LeftFace[F[x].c][F[x].b], F[x].b, F[x].c, p);
dfs(LeftFace[F[x].a][F[x].c], F[x].c, F[x].a, p);
} Point ff;
void dfs2(int x) {
if (F[x].ex == false) return; Point now = (P[F[x].b] - P[F[x].a]) ^ (P[F[x].c] - P[F[x].a]);
if (fabs(now * ff - sqrt(now.sqrlen() * ff.sqrlen())) < 1e-6)
F[x].ex = false;
else
return; dfs2(LeftFace[F[x].b][F[x].a]);
dfs2(LeftFace[F[x].c][F[x].b]);
dfs2(LeftFace[F[x].a][F[x].c]);
} int main() {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; ++i) scanf("%lf%lf%lf", &P[i].x, &P[i].y, &P[i].z);
ftot = 0;
Point a, b, c, d, e;
a = P[2] - P[1];
int tmp, id1, id2;
for (tmp = 3; tmp <= n; ++tmp) {
b = P[tmp] - P[1];
d = a ^ b;
if (d.sqrlen() < eps) continue;
id1 = tmp; break;
}
for (++tmp; tmp <= n; ++tmp) {
c = P[tmp] - P[1];
if (fabs(d * c) < eps) continue;
id2 = tmp; break;
} if (d * c < 0) swap(id1, id2);
insFace(1, 2, id2, 3, 4, 2);
insFace(1, id2, id1, 1, 4, 3);
insFace(1, id1, 2, 2, 4, 1);
insFace(2, id1, id2, 3, 2, 1); for (int i = 3; i <= n; ++i) {
if (i == id1 || i == id2) continue;
for (int j = 1; j <= ftot; ++j)
if (F[j].ex && visible(j, i)) {
dfs(j, 0, 0, i);
ptot = 0;
int tmps = st, tmpt = to[st], ppff = ftot;
do {
++ptot;
ps[ptot] = tmps; pt[ptot] = tmpt;
pf[ptot] = ++ppff;
tmps = tmpt; tmpt = to[tmpt];
} while (tmps != st); for (int k = 1, pre, suc; k <= ptot; ++k) {
pre = k - 1; suc = k + 1;
if (pre == 0) pre = ptot;
if (suc > ptot) suc = 1;
pre = pf[pre]; suc = pf[suc];
insFace(pt[k], i, ps[k], suc, pre, LeftFace[pt[k]][ps[k]]);
} break;
}
} int ans = 0;
for (int i = 1; i <= ftot; ++i)
if (F[i].ex) {
++ans;
ff = (P[F[i].b] - P[F[i].a]) ^ (P[F[i].c] - P[F[i].a]);
dfs2(i);
}
printf("%d\n", ans);
} return 0;
}

最新文章

  1. Atitit &#160;图像处理底色变红的解决
  2. &lt;实训|第十一天&gt;学习一下linux中的进程,文件查找,文件压缩与IO重定向
  3. hive 常见面试题
  4. dynamic介绍
  5. 最简单的视音频播放示例8:DirectSound播放PCM
  6. ASP.NET中怎样才能使自己的代码运行的效率更高
  7. Linux下Oracle11G RAC报错:在安装oracle软件时报file not found一例
  8. js中 正則表達式
  9. SweetTips: 快意灵动的Android提示库!
  10. epoll的ET和LT模式比较 - 源码分析
  11. java web SSO单点登录
  12. Swift学习第二天--面向对象
  13. Nginx负载均衡(架构之路)
  14. 【Linux基础】判断当前机器是虚拟机还是物理机
  15. Spring 基础知识(三)MVC 架构简介
  16. Matlab:显(隐)式Euler和Richardson外推法变步长求解刚性问题
  17. MySql数据类型范围
  18. Day 10 动态参数&amp;名称空间,局部全部.函数嵌套&amp;global nonlocal关键字.
  19. 第九章 Mysql函数
  20. SELECTORS模块实现并发简单版FTP

热门文章

  1. linux源码安装nginx
  2. Linux服务-配置Nginx反向代理
  3. 查看GCC的内置宏定义
  4. 使用mysql的SUBSTRING_INDEX函数解决项目中编码非重复问题的实现方案!
  5. 个性化你的Git Log的输出格式
  6. .net基础初学Android
  7. Failed to execute &#39;setRequestHeader&#39; on &#39;XMLHttpRequest&#39;: The object&#39;s state must be OPENED.
  8. 【小程序开发总结】微信小程序开发常用技术方法总结
  9. 数据库-mysql触发器
  10. 20165203 2017-2018-2 《Java程序设计》第一周学习总结