三维凸包求内部一点到表面的最近距离(HDU4266)
http://acm.hdu.edu.cn/showproblem.php?pid=4266
The Worm in the Apple
Given a description of the apple (defined as a convex shape in 3D space), and a list of possible positions in the apple for Willy (defined as 3D points), determine the minimum distance Willy must travel to get to the surface of the apple from each point.
n (4≤n≤1,000), which tells the number of points describing the apple.
On the next n lines will be three integers x,
y and z (-10,000≤x,y,z≤10,000), where each point (x,y,z) is either on the surface of, or within, the apple. The apple is the convex
hull of these points. No four points will be coplanar.
Following the description of the apple, there will be a line with a single integer
q (1≤q≤100,000), which is the number of queries – that is, the number of points where Willy might be inside the apple. Each of the following
q lines will contain three integers x,
y and z (-10,000≤x,y,z≤10,000), representing a point (x,y,z) where Willy might be. All of Willy’s points are guaranteed to be inside
the apple. The input will end with a line with a single 0.
2.12344 rounds to 2.1234, 2.12345 rounds to 2.1235). Output each number on its own line, with no spaces, and do not print any blank lines between answers.
6 0 0 0 100 0 0 0 100 0 0 0 100 20 20 20 30 20 10 4 1 1 1 30 30 35 7 8 9 90 2 2 0
1.0000 2.8868 7.0000 2.0000
- #include"stdio.h"
- #include"string.h"
- #include"iostream"
- #include"map"
- #include"string"
- #include"queue"
- #include"stack"
- #include"vector"
- #include"stdlib.h"
- #include"algorithm"
- #include"math.h"
- #define M 1009
- #define eps 1e-10
- #define inf 0x3f3f3f3f
- #define mod 1070000009
- #define PI acos(-1.0)
- using namespace std;
- struct node
- {
- double x,y,z,dis;
- node(){}
- node(double xx,double yy,double zz):x(xx),y(yy),z(zz){}
- node operator +(const node p)//向量间求和操作
- {
- return node(x+p.x,y+p.y,z+p.z);
- }
- node operator -(const node p)//向量间相减操作
- {
- return node(x-p.x,y-p.y,z-p.z);
- }
- node operator *(const node p)//向量间叉乘操作
- {
- return node(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);
- }
- node operator *(const double p)//向量乘以一个数
- {
- return node(x*p,y*p,z*p);
- }
- node operator /(const double p)//向量除以一个数
- {
- return node(x/p,y/p,z/p);
- }
- double operator ^(const node p)//向量间点乘操作
- {
- return x*p.x+y*p.y+z*p.z;
- }
- };
- struct threeD_convex_hull//三维凸包
- {
- struct face
- {
- int a,b,c;
- int ok;
- };
- int n;//初始点数
- int cnt;//凸包三角形数
- node p[M];//初始点
- face f[M*8];//凸包三角形
- int to[M][M];//点i到j是属于哪个面
- double len(node p)//向量的长度
- {
- return sqrt(p.x*p.x+p.y*p.y+p.z*p.z);
- }
- double area(node a,node b,node c)//三个点的面积*2
- {
- return len((b-a)*(c-a));
- }
- double volume(node a,node b,node c,node d)//四面体面积*6
- {
- return (b-a)*(c-a)^(d-a);
- }
- double ptof(node q,face f)//点与面同向
- {
- node m=p[f.b]-p[f.a];
- node n=p[f.c]-p[f.a];
- node t=q-p[f.a];
- return m*n^t;
- }
- void dfs(int q,int cur)//维护凸包,若点q在凸包外则更新凸包
- {
- f[cur].ok=0;//删除当前面,因为此时它在更大的凸包内部
- deal(q,f[cur].b,f[cur].a);
- deal(q,f[cur].c,f[cur].b);
- deal(q,f[cur].a,f[cur].c);
- }
- //因为每个三角形的的三边是按照逆时针记录的,所以把边反过来后对应的就是与ab边共线的另一个面
- void deal(int q,int a,int b)
- {
- int fa=to[a][b];//与当前面cnt共边的另一个面
- face add;
- if(f[fa].ok)//若fa面目前是凸包的表面则继续
- {
- if(ptof(p[q],f[fa])>eps)//若点q能看到fa面继续深搜fa的三条边,更新新的凸包面
- dfs(q,fa);
- else//当q点可以看到cnt面的同时看不到a,b共边的fa面,则p和a,b点组成一个新的表面三角形
- {
- add.a=b;
- add.b=a;
- add.c=q;
- add.ok=1;
- to[b][a]=to[a][q]=to[q][b]=cnt;
- f[cnt++]=add;
- }
- }
- }
- int same(int s,int t)//判断两个三角形是否共面
- {
- node a=p[f[s].a];
- node b=p[f[s].b];
- node c=p[f[s].c];
- if(fabs(volume(a,b,c,p[f[t].a]))<eps
- &&fabs(volume(a,b,c,p[f[t].b]))<eps
- &&fabs(volume(a,b,c,p[f[t].c]))<eps)
- return 1;
- return 0;
- }
- void make()//构建3D凸包
- {
- cnt=0;
- if(n<4)
- return;
- int sb=1;
- for(int i=1;i<n;i++)//保证前两个点不共点
- {
- if(len(p[0]-p[i])>eps)
- {
- swap(p[1],p[i]);
- sb=0;
- break;
- }
- }
- if(sb)return;
- sb=1;
- for(int i=2;i<n;i++)//保证前三个点不共线
- {
- if(len((p[1]-p[0])*(p[i]-p[0]))>eps)
- {
- swap(p[2],p[i]);
- sb=0;
- break;
- }
- }
- if(sb)return;
- sb=1;
- for(int i=3;i<n;i++)//保证前四个点不共面
- {
- if(fabs(volume(p[0],p[1],p[2],p[i]))>eps)
- {
- swap(p[3],p[i]);
- sb=0;
- break;
- }
- }
- if(sb)return;
- face add;
- for(int i=0;i<4;i++)//构建初始四面体
- {
- add.a=(i+1)%4;
- add.b=(i+2)%4;
- add.c=(i+3)%4;
- add.ok=1;
- if(ptof(p[i],add)>eps)
- swap(add.c,add.b);
- to[add.a][add.b]=to[add.b][add.c]=to[add.c][add.a]=cnt;
- f[cnt++]=add;
- }
- for(int i=4;i<n;i++)//倍增法更新凸包
- {
- for(int j=0;j<cnt;j++)//判断每个点是在当前凸包的内部或者外部
- {
- if(f[j].ok&&ptof(p[i],f[j])>eps)//若在外部且看到j面继续
- {
- dfs(i,j);
- break;
- }
- }
- }
- int tmp=cnt;//把不是凸包上的面删除即ok=0;
- cnt=0;
- for(int i=0;i<tmp;i++)
- if(f[i].ok)
- f[cnt++]=f[i];
- }
- double Area()//表面积
- {
- double S=0;
- if(n==3)
- {
- S=area(p[0],p[1],p[2])/2.0;
- return S;
- }
- for(int i=0;i<cnt;i++)
- S+=area(p[f[i].a],p[f[i].b],p[f[i].c]);
- return S/2.0;
- }
- double Volume()//体积
- {
- double V=0;
- node mid(0,0,0);
- for(int i=0;i<cnt;i++)
- V+=volume(p[f[i].a],p[f[i].b],p[f[i].c],mid);
- V=fabs(V)/6.0;
- return V;
- }
- int tringleCnt()//凸包表面三角形数目
- {
- return cnt;
- }
- int faceCnt()//凸包表面多边形数目
- {
- int num=0;
- for(int i=0;i<cnt;i++)
- {
- int flag=1;
- for(int j=0;j<i;j++)
- {
- if(same(i,j))
- {
- flag=0;
- break;
- }
- }
- num+=flag;
- }
- return num;
- }
- double pf_dis(face f,node q)//点到面的距离
- {
- double V=volume(p[f.a],p[f.b],p[f.c],q);
- double S=area(p[f.a],p[f.b],p[f.c]);
- return fabs(V/S);
- }
- double min_dis(node q)//暴力搜索内部的点q到面的最短距离即体积/面积
- {
- double mini=inf;
- for(int i=0;i<cnt;i++)
- {
- double h=pf_dis(f[i],q);
- if(mini>h)
- mini=h;
- }
- return mini;
- }
- node barycenter()//凸包的重心
- {
- node ret(0,0,0),mid(0,0,0);
- double sum=0;
- for(int i=0;i<cnt;i++)
- {
- double V=volume(p[f[i].a],p[f[i].b],p[f[i].c],mid);
- ret=ret+(mid+p[f[i].a]+p[f[i].b]+p[f[i].c])/4.0*V;
- sum+=V;
- }
- ret=ret/sum;
- return ret;
- }
- }hull;
- int main()
- {
- while(scanf("%d",&hull.n),hull.n)
- {
- for(int i=0;i<hull.n;i++)
- scanf("%lf%lf%lf",&hull.p[i].x,&hull.p[i].y,&hull.p[i].z);
- hull.make();
- int m;
- scanf("%d",&m);
- while(m--)
- {
- node q;
- scanf("%lf%lf%lf",&q.x,&q.y,&q.z);
- printf("%.4lf\n",hull.min_dis(q));
- }
- }
- return 0;
- }
三维凸包求内部一点到表面的最近距离(HDU4266)的更多相关文章
- 三维凸包求凸包表面的个数(HDU3662)
3D Convex Hull Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 三维凸包求其表面积(POJ3528)
Ultimate Weapon Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 2074 Accepted: 989 D ...
- 三维凸包求重心到面的最短距离(HDU4273)
http://acm.hdu.edu.cn/showproblem.php?pid=4273 Rescue Time Limit: 2000/1000 MS (Java/Others) Memo ...
- 2019CCPC-江西省赛 -A Cotree (树形DP,求树上一点到其他点的距离之和)
我是傻逼我是傻逼 #include<bits/stdc++.h> using namespace std; const int maxn=4e5+50; typedef long long ...
- POJ 2225 / ZOJ 1438 / UVA 1438 Asteroids --三维凸包,求多面体重心
题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...
- POJ 3528 求三维凸包表面积
也是用模板直接套的题目诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include < ...
- hdu4449Building Design(三维凸包+平面旋转)
链接 看了几小时也没看懂代码表示的何意..无奈下来问问考研舍友. 还是考研舍友比较靠谱,分分钟解决了我的疑问. 可能三维的东西在纸面上真的不好表示,网上没有形象的题解,只有简单"明了&quo ...
- hdu 4273 2012长春赛区网络赛 三维凸包中心到最近面距离 ***
新模板 /* HDU 4273 Rescue 给一个三维凸包,求重心到表面的最短距离 模板题:三维凸包+多边形重心+点面距离 */ #include<stdio.h> #include&l ...
- hdu4266(三维凸包模板题)
/*给出三维空间中的n个顶点,求解由这n个顶点构成的凸包表面的多边形个数. 增量法求解:首先任选4个点形成的一个四面体,然后每次新加一个点,分两种情况: 1> 在凸包内,则可以跳过 2> ...
随机推荐
- select文本框架(change事件)改变另外一个select的值
$('select[name=adults]').bind('change', function() { var $value = $(this).val(); if ($value >= 1) ...
- Charles安装包及破解包下载地址
Charles安装包及破解包下载地址 http://xclient.info/s/charles.html?_=baf317d2a9932afca9b32c327f8a34c9
- 帝国cms的list.var中使用php函数
$r[title] = esub($r[title],8,'...'); //截取前8个字符,多出部分用...代替 $r[title] = str_replace("lhj",&q ...
- 简单日历插件jquery.date_input.pack
html: <link rel="stylesheet" type="text/css" href="css/jquery.date_input ...
- Js注释和对象
1.注释 单行: //注释内容 console.log('加油~');//在控制台输出一条信息: 多行: /*注释内容*/: 2.对象 1)对象是一个复合值,是根据某种引用类型创建出来的实例. 2)常 ...
- OpenCV学习:播放avi视频文件
#if 0 //播放avi视频文件(IplImage) #include <opencv2/opencv.hpp> using namespace std; #pragma comment ...
- Visual Basic的未来之路
Green首先列出了当时使用VB进行开发的四个基础指导原则: 1.VB和C#共享的通用IDE和平台构建块. 2.共享的“多范式.面向对象.命令式.强类型等”语言 ...
- Navicat for Mysql 如何备份数据库
Navicat for Mysql 如何备份数据库 打开界面如下 打开自己的的数据库 点击需要备份的数据库名 未完!!! 文章来自:http://jingyan.baidu.com/article/f ...
- [spring] 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 26 in XML document from ...
- [转]ASP.NET MVC 5 - 将数据从控制器传递给视图
在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图.控制器类将响应请求来的URL.控制器类是给您写代码来处理传入请求的地方,并从数据库中检索数据,并最终决定什么类型的返回结果 ...