1. /*给出三维空间中的n个顶点,求解由这n个顶点构成的凸包表面的多边形个数.
  2. 增量法求解:首先任选4个点形成的一个四面体,然后每次新加一个点,分两种情况:
  3. 1> 在凸包内,则可以跳过
  4. 2> 在凸包外,找到从这个点可以"看见"的面,删除这些面,
  5. 然后对于一边没有面的线段,和新加的这个点新建一个面,至于这个点可以看见的面,
  6. 就是求出这个面的方程(可以直接求法向量).
  7. */
  8. #include<iostream>
  9. #include<cmath>
  10. #include<cstring>
  11. #include<cstdlib>
  12. #include<algorithm>
  13. using namespace std;
  14. const int MAXN=;
  15. const double EPS=1e-;
  16. struct Point
  17. {
  18. double x,y,z;
  19. Point(){}
  20. Point(double xx,double yy,double zz):x(xx),y(yy),z(zz){}
  21.  
  22. Point operator -(const Point p1) //两向量之差
  23. {
  24. return Point(x-p1.x,y-p1.y,z-p1.z);
  25. }
  26.  
  27. Point operator *(Point p) //叉乘
  28. {
  29. return Point(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);
  30. }
  31.  
  32. double operator ^(Point p) //点乘
  33. {
  34. return (x*p.x+y*p.y+z*p.z);
  35. }
  36. void read()
  37. {
  38. scanf("%lf%lf%lf",&x,&y,&z);
  39. }
  40. };
  41. struct CH3D
  42. {
  43. struct face
  44. {
  45. int a,b,c; //表示凸包一个面上三个点的编号
  46. bool ok; //表示该面是否属于最终凸包中的面
  47. };
  48.  
  49. int n; //初始顶点数
  50. Point P[MAXN]; //初始顶点
  51.  
  52. int num; //凸包表面的三角形数
  53. face F[*MAXN];
  54.  
  55. int g[MAXN][MAXN]; //凸包表面的三角形
  56.  
  57. double vlen(Point a) //向量长度
  58. {
  59. return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
  60. }
  61.  
  62. Point cross(const Point &a, const Point &b, const Point &c) //叉乘
  63. {
  64. return Point((b.y-a.y)*(c.z-a.z)-(b.z-a.z)*(c.y-a.y),-((b.x-a.x)*(c.z-a.z)
  65. -(b.z-a.z)*(c.x-a.x)),(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x));
  66. }
  67. double area(Point a,Point b,Point c) //三角形面积*2
  68. {
  69. return vlen((b-a)*(c-a));
  70. }
  71.  
  72. double volume(Point a,Point b,Point c,Point d) //四面体有向体积*6
  73. {
  74. return (b-a)*(c-a)^(d-a);
  75. }
  76.  
  77. double dblcmp(Point &p,face &f) //正:点在面同向
  78. {
  79. Point m=P[f.b]-P[f.a];
  80. Point n=P[f.c]-P[f.a];
  81. Point t=p-P[f.a];
  82. return (m*n)^t;
  83. }
  84.  
  85. void deal(int p,int a,int b)
  86. {
  87. int f=g[a][b];
  88. face add;
  89. if(F[f].ok)
  90. {
  91. if(dblcmp(P[p],F[f])>EPS)
  92. dfs(p,f);
  93. else
  94. {
  95. add.a=b;
  96. add.b=a;
  97. add.c=p;
  98. add.ok=;
  99. g[p][b]=g[a][p]=g[b][a]=num;
  100. F[num++]=add;
  101. }
  102. }
  103. }
  104.  
  105. void dfs(int p,int now)
  106. {
  107. F[now].ok=;
  108. deal(p,F[now].b,F[now].a);
  109. deal(p,F[now].c,F[now].b);
  110. deal(p,F[now].a,F[now].c);
  111. }
  112.  
  113. bool same(int s,int t)
  114. {
  115. Point &a=P[F[s].a];
  116. Point &b=P[F[s].b];
  117. Point &c=P[F[s].c];
  118. return fabs(volume(a,b,c,P[F[t].a]))<EPS && fabs(volume(a,b,c,P[F[t].b]))<EPS
  119. && fabs(volume(a,b,c,P[F[t].c]))<EPS;
  120. }
  121.  
  122. void solve() //构建三维凸包
  123. {
  124. int i,j,tmp;
  125. face add;
  126. bool flag=true;
  127. num=;
  128. if(n<)
  129. return;
  130. for(i=;i<n;i++) //此段是为了保证前四个点不共面,若以保证,则可去掉
  131. {
  132. if(vlen(P[]-P[i])>EPS)
  133. {
  134. swap(P[],P[i]);
  135. flag=false;
  136. break;
  137. }
  138. }
  139. if(flag)
  140. return;
  141. flag=true;
  142. for(i=;i<n;i++) //使前三点不共线
  143. {
  144. if(vlen((P[]-P[])*(P[]-P[i]))>EPS)
  145. {
  146. swap(P[],P[i]);
  147. flag=false;
  148. break;
  149. }
  150. }
  151. if(flag)
  152. return;
  153. flag=true;
  154. for(i=;i<n;i++) //使前四点不共面
  155. {
  156. if(fabs((P[]-P[])*(P[]-P[])^(P[]-P[i]))>EPS)
  157. {
  158. swap(P[],P[i]);
  159. flag=false;
  160. break;
  161. }
  162. }
  163. if(flag)
  164. return;
  165. for(i=;i<;i++)
  166. {
  167. add.a=(i+)%;
  168. add.b=(i+)%;
  169. add.c=(i+)%;
  170. add.ok=true;
  171. if(dblcmp(P[i],add)>)
  172. swap(add.b,add.c);
  173. g[add.a][add.b]=g[add.b][add.c]=g[add.c][add.a]=num;
  174. F[num++]=add;
  175. }
  176. for(i=;i<n;i++)
  177. {
  178. for(j=;j<num;j++)
  179. {
  180. if(F[j].ok && dblcmp(P[i],F[j])>EPS)
  181. {
  182. dfs(i,j);
  183. break;
  184. }
  185. }
  186. }
  187. tmp=num;
  188. for(i=num=;i<tmp;i++)
  189. if(F[i].ok)
  190. {
  191. F[num++]=F[i];
  192. }
  193. }
  194.  
  195. double area() //表面积
  196. {
  197. double res=0.0;
  198. if(n==)
  199. {
  200. Point p=cross(P[],P[],P[]);
  201. res=vlen(p)/2.0;
  202. return res;
  203. }
  204. for(int i=;i<num;i++)
  205. res+=area(P[F[i].a],P[F[i].b],P[F[i].c]);
  206. return res/2.0;
  207. }
  208.  
  209. double volume() //体积
  210. {
  211. double res=0.0;
  212. Point tmp(,,);
  213. for(int i=;i<num;i++)
  214. res+=volume(tmp,P[F[i].a],P[F[i].b],P[F[i].c]);
  215. return fabs(res/6.0);
  216. }
  217.  
  218. int triangle() //表面三角形个数
  219. {
  220. return num;
  221. }
  222.  
  223. int polygon() //表面多边形个数
  224. {
  225. int i,j,res,flag;
  226. for(i=res=;i<num;i++)
  227. {
  228. flag=;
  229. for(j=;j<i;j++)
  230. if(same(i,j))
  231. {
  232. flag=;
  233. break;
  234. }
  235. res+=flag;
  236. }
  237. return res;
  238. }
  239. Point getcent()//求凸包质心
  240. {
  241. Point ans(,,),temp=P[F[].a];
  242. double v = 0.0,t2;
  243. for(int i=;i<num;i++){
  244. if(F[i].ok == true){
  245. Point p1=P[F[i].a],p2=P[F[i].b],p3=P[F[i].c];
  246. t2 = volume(temp,p1,p2,p3)/6.0;//体积大于0,也就是说,点 temp 不在这个面上
  247. if(t2>){
  248. ans.x += (p1.x+p2.x+p3.x+temp.x)*t2;
  249. ans.y += (p1.y+p2.y+p3.y+temp.y)*t2;
  250. ans.z += (p1.z+p2.z+p3.z+temp.z)*t2;
  251. v += t2;
  252. }
  253. }
  254. }
  255. ans.x /= (*v); ans.y /= (*v); ans.z /= (*v);
  256. return ans;
  257. }
  258. double function(Point fuck){//点到凸包上的最近距离(枚举每个面到这个点的距离)
  259. double min=;
  260. for(int i=;i<num;i++){
  261. if(F[i].ok==true){
  262. Point p1=P[F[i].a] , p2=P[F[i].b] , p3=P[F[i].c];
  263. double a = ( (p2.y-p1.y)*(p3.z-p1.z)-(p2.z-p1.z)*(p3.y-p1.y) );
  264. double b = ( (p2.z-p1.z)*(p3.x-p1.x)-(p2.x-p1.x)*(p3.z-p1.z) );
  265. double c = ( (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x) );
  266. double d = ( -(a*p1.x+b*p1.y+c*p1.z) );
  267. double temp = fabs(a*fuck.x+b*fuck.y+c*fuck.z+d)/sqrt(a*a+b*b+c*c);
  268. if(temp<min)min = temp;
  269. }
  270. }
  271. return min;
  272. }
  273.  
  274. };
  275.  
  276. int main()
  277. {
  278. int n;
  279. while(scanf("%d",&n) && n)
  280. {
  281. CH3D hull;
  282. hull.n=n;
  283. for(int i=;i<n;i++)
  284. {
  285. hull.P[i].read();
  286. }
  287. hull.solve();
  288. int q;
  289. scanf("%d",&q);
  290. for(int i=;i<q;i++)
  291. {
  292. Point tp;
  293. tp.read();
  294. double ans=1e9;
  295. ans = min(ans, hull.function(tp) );
  296. printf("%.4lf\n",ans);
  297. }
  298. }
  299. return ;
  300. }

求点到三维凸包的最小距离,直接用模板暴力枚举即可。

hdu4266(三维凸包模板题)的更多相关文章

  1. POJ3528 HDU3662 三维凸包模板

    POJ3528 HDU3662 第一道题 给定若干点 求凸包的表面积,第二题 给定若干点就凸包的面数. 简单说一下三维凸包的求法,首先对于4个点假设不共面,确定了唯一四面体,对于一个新的点,若它不在四 ...

  2. POJ 3348 Cows | 凸包模板题

    题目: 给几个点,用绳子圈出最大的面积养牛,输出最大面积/50 题解: Graham凸包算法的模板题 下面给出做法 1.选出x坐标最小(相同情况y最小)的点作为极点(显然他一定在凸包上) 2.其他点进 ...

  3. hdu 1348 Wall(凸包模板题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others)    M ...

  4. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  5. HDU 1392 凸包模板题,求凸包周长

    1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...

  6. POJ 1113 凸包模板题

    上模板. #include <cstdio> #include <cstring> #include <iostream> #include <algorit ...

  7. bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]

    Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在 ...

  8. UVA 11769 All Souls Night 的三维凸包要求的表面面积

    主题链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2869">点击打开链接 求给定的 ...

  9. hdu 1348 凸包模板

    http://acm.hdu.edu.cn/showproblem.php?pid=1348 造城墙问题,求出凸包加上一圈圆的周长即可 凸包模板题 #include <cstdio> #i ...

随机推荐

  1. javascript - 实现jquery类似的$调用方法

    var $ = { name: function (name) { return name; }, age: function (age) { return age; }, numCount: fun ...

  2. Android项目-几种常见的应用架构

    android两种Tab分页的方式:TabActivity和ActivityGroup http://www.fengfly.com/plus/view-209429-1.html 1.单个Activ ...

  3. WP8学习笔记:如何在页面显示前自动转向到其他页面

    在本次修练开始之前,我们除了预设的 MainPage页面外,也另外新增了一个 Login页面,如下图示: MainPage.xaml页面长这样 Login.xaml页面长这样 因为我们的需求是要求使用 ...

  4. 同时安装office2016与visio2016的实现过程

    visio 2016安装问题 同时安装office2016与visio2016的实现过程 visio2016  but failed

  5. centos mysql iptables配置

    在CentOS系统中防火墙默认是阻止3306端口的,我们要是想访问mysql数据库,我们需要这个端口,命令如下: iptables -I INPUT -p tcp --dport 3036 -j AC ...

  6. Leetcode--easy系列3

    #26 Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  7. C++ 内存泄漏

    1. 内存泄漏: 在计算机科学中,内存泄漏指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况. 内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前 ...

  8. >/dev/null 2>&1的作用

    1 表示stdout标准输出,系统默认值是1,所以">/dev/null"等同于"1>/dev/null"  2 表示stderr标准错误  &am ...

  9. Unity学习笔记 - UI System(一)

    转载请注明出处: EnigmaJJ http://www.cnblogs.com/twjcnblog/p/5850648.html 术语 Canvas是Unity的原生组件,Unity的渲染系统使用C ...

  10. deepin下安装python的Tkinter库

    在Linux下,如果需要编写界面应用,并且此界面应用对性能的要求不是很高,一般可以使用Python解决.Python中可以使用自带的Tkinter库或者是第三方的Wxpython库,当然Tkinter ...