python 三维凸包_三维立体空间凸包模板_Hdu 4273 Rescue

该博客介绍了如何使用Python实现三维凸包算法,并通过HDU 4273 Rescue问题为例,展示了算法的应用。文章提供了完整的C++代码,包括点的扰动处理和增量法求解三维凸包。此外,还讨论了凸包的质心计算和求最小距离的方法。
摘要由CSDN通过智能技术生成

三维凸包模板_Hdu 4273 Rescue

使用汝佳的模板:   简单,但是有的题目过不了

acm.hdu.edu.cn/showproblem.php?pid=4273

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

//LOOP

#define FF(i, a, b) for(int i = (a); i < (b); ++i)

#define FE(i, a, b) for(int i = (a); i <= (b); ++i)

#define FED(i, b, a) for(int i = (b); i>= (a); --i)

#define REP(i, N) for(int i = 0; i < (N); ++i)

#define CLR(A,value) memset(A,value,sizeof(A))

#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)

//OTHER

#define SZ(V) (int)V.size()

#define PB push_back

#define MP make_pair

#define all(x) (x).begin(),(x).end()

//INPUT

#define RI(n) scanf("%d", &n)

#define RII(n, m) scanf("%d%d", &n, &m)

#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)

#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)

#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)

#define RS(s) scanf("%s", s)

//OUTPUT

#define WI(n) printf("%d\n", n)

#define WS(n) printf("%s\n", n)

//debug

//#define online_judge

#ifndef online_judge

#define debugt(a) cout << (#a) << "=" << a << " ";

#define debugI(a) debugt(a) cout << endl

#define debugII(a, b) debugt(a) debugt(b) cout << endl

#define debugIII(a, b, c) debugt(a) debugt(b) debugt(c) cout << endl

#define debugIV(a, b, c, d) debugt(a) debugt(b) debugt(c) debugt(d) cout << endl

#else

#define debugI(v)

#define debugII(a, b)

#define debugIII(a, b, c)

#define debugIV(a, b, c, d)

#endif

typedef long long LL;

typedef unsigned long long ULL;

typedef vector VI;

const int INF = 0x3f3f3f3f;

const double eps = 1e-10;

const int MOD = 100000007;

const int MAXN = 400;

const double PI = acos(-1.0);

inline int dcmp(double x)

{

if(fabs(x) < eps) return 0;

else return x < 0 ? -1 : 1;

}

struct Point

{

double x, y, z;

Point(double x=0, double y=0, double z=0):x(x),y(y),z(z) { }

inline void read() { scanf("%lf%lf%lf", &x, &y, &z); }

};

typedef Point Vector;

inline Vector operator + (Vector A, Vector B)

{

return Vector(A.x+B.x, A.y+B.y, A.z+B.z);

}

inline Vector operator - (Point A, Point B)

{

return Vector(A.x-B.x, A.y-B.y, A.z-B.z);

}

inline Vector operator * (Vector A, double p)

{

return Vector(A.x*p, A.y*p, A.z*p);

}

inline Vector operator / (Vector A, double p)

{

return Vector(A.x/p, A.y/p, A.z/p);

}

bool operator == (const Point& a, const Point& b)

{

return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0 && dcmp(a.z-b.z) == 0;

}

inline double Dot(Vector A, Vector B)

{

return A.x*B.x + A.y*B.y + A.z*B.z;

}

inline double Length(Vector A)

{

return sqrt(Dot(A, A));

}

inline double Angle(Vector A, Vector B)

{

return acos(Dot(A, B) / Length(A) / Length(B));

}

inline Vector Cross(Vector A, Vector B)

{

return Vector(A.y*B.z - A.z*B.y, A.z*B.x - A.x*B.z, A.x*B.y - A.y*B.x);

}

inline double Area2(Point A, Point B, Point C)

{

return Length(Cross(B-A, C-A));

}

inline double Volume6(Point A, Point B, Point C, Point D)

{

return Dot(D-A, Cross(B-A, C-A));

}

inline Point Centroid(Point A, Point B, Point C, Point D)

{

return (A + B + C + D)/4.0;

}

inline double rand01()

{

return rand() / (double)RAND_MAX;

}

inline double randeps()

{

return (rand01() - 0.5) * eps;

}

inline Point add_noise(Point p)

{

return Point(p.x + randeps(), p.y + randeps(), p.z + randeps());

}

struct Face

{

int v[3];

Face(int a, int b, int c)

{

v[0] = a;

v[1] = b;

v[2] = c;

}

inline Vector Normal(const vector& P) const

{

return Cross(P[v[1]]-P[v[0]], P[v[2]]-P[v[0]]);

}

// f是否能看见P[i]

inline int CanSee(const vector& P, int i) const

{

return Dot(P[i]-P[v[0]], Normal(P)) > 0;

}

};

// 增量法求三维凸包

// 注意:没有考虑各种特殊情况(如四点共面)。实践中,请在调用前对输入点进行微小扰动

vector CH3D(const vector& P)

{

int n = P.size();

vector > vis(n);

for(int i = 0; i < n; i++) vis[i].resize(n);

vector cur;

cur.push_back(Face(0, 1, 2)); // 由于已经进行扰动,前三个点不共线

cur.push_back(Face(2, 1, 0));

for(int i = 3; i < n; i++)

{

vector next;

// 计算每条边的“左面”的可见性

for(int j = 0; j < (int)cur.size(); j++)

{

Face& f = cur[j];

int res = f.CanSee(P, i);

if(!res) next.push_back(f);

for(int k = 0; k < 3; k++) vis[f.v[k]][f.v[(k+1)%3]] = res;

}

for(int j = 0; j < (int)cur.size(); j++)

for(int k = 0; k < 3; k++)

{

int a = cur[j].v[k], b = cur[j].v[(k+1)%3];

if(vis[a][b] != vis[b][a] && vis[a][b]) // (a,b)是分界线,左边对P[i]可见

next.push_back(Face(a, b, i));

}

cur = next;

}

return cur;

}

struct ConvexPolyhedron

{

int n;

vector P, P2;

vector faces;

bool read()

{

if(scanf("%d", &n) != 1) return false;

P.resize(n);

P2.resize(n);

for(int i = 0; i < n; i++)

{

P[i].read();

P2[i] = add_noise(P[i]);

}

faces = CH3D(P2);

return true;

}

Point centroid()

{

Point C = P[0];

double totv = 0;

Point tot(0,0,0);

for(int i = 0; i < (int)faces.size(); i++)

{

Point p1 = P[faces[i].v[0]], p2 = P[faces[i].v[1]], p3 = P[faces[i].v[2]];

double v = -Volume6(p1, p2, p3, C);

totv += v;

tot = tot + Centroid(p1, p2, p3, C)*v;

}

return tot / totv;

}

double mindist(Point C)

{

double ans = 1e30;

for(int i = 0; i < (int)faces.size(); i++)

{

Point p1 = P[faces[i].v[0]], p2 = P[faces[i].v[1]], p3 = P[faces[i].v[2]];

ans = min(ans, fabs(-Volume6(p1, p2, p3, C) / Area2(p1, p2, p3)));

}

return ans;

}

} P1;

int main()

{

ConvexPolyhedron P1, P2;

while(P1.read())

{

Point C1 = P1.centroid();

double d1 = P1.mindist(C1);

printf("%.3lf\n", d1);

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值