当前位置: 首页 > news >正文

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

主题链接:点击打开链接

求给定的3维坐标的凸包的表面积

三维凸包裸题。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
#define PR 1e-8
#define N 510
struct TPoint{
	double x, y, z;
	TPoint(){}
	TPoint(double _x, double _y, double _z):x(_x), y(_y), z(_z){}
	TPoint operator-(const TPoint p){return TPoint(x-p.x, y-p.y, z-p.z);}
	TPoint operator*(const TPoint p){return TPoint(y*p.z-z*p.y, z*p.x-x*p.z, x*p.y-y*p.x);}
	double operator^(const TPoint p){return x*p.x+y*p.y+z*p.z;}
};
struct fac{
	int a, b, c;
	bool ok;
};
struct T3dhull{
	int n;
	TPoint ply[N];
	int trianglecnt;
	fac tri[N];
	int vis[N][N];
	double dist(TPoint a){return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);}
	double area(TPoint a, TPoint b, TPoint c)
	{ return dist((b-a)*(c-a));}
	double volume(TPoint a, TPoint b, TPoint c, TPoint d)
	{ return (b-a)*(c-a)^(d-a);}
	double ptoplane(TPoint &p, fac &f)
	{
		TPoint m = ply[f.b] - ply[f.a], n = ply[f.c]-ply[f.a], t = p-ply[f.a];
		return (m*n)^t;
	}
	void deal(int p, int a, int b){
		int f = vis[a][b];
		fac add;
		if(tri[f].ok)
		{
			if((ptoplane(ply[p], tri[f])) > PR)
				dfs(p, f);
			else 
			{
				add.a = b, add.b = a, add.c = p, add.ok = 1;
				vis[p][b] = vis[a][p] = vis[b][a] = trianglecnt;
				tri[trianglecnt++] = add;
			}
		}
	}
	void dfs(int p, int cnt) {
		tri[cnt].ok = 0;
		deal(p, tri[cnt].b, tri[cnt].a);
		deal(p, tri[cnt].c, tri[cnt].b);
		deal(p, tri[cnt].a, tri[cnt].c);
	}
	bool same(int s, int e) {
		TPoint a = ply[tri[s].a], b = ply[tri[s].b], c = ply[tri[s].c];
		return fabs(volume(a,b,c,ply[tri[e].a])) < PR
			&& fabs(volume(a,b,c,ply[tri[e].b])) < PR
			&& fabs(volume(a,b,c,ply[tri[e].c])) < PR;
	}
	void construct()
	{
		int i, j;
		trianglecnt = 0;
		if(n<4) return ;
		bool tmp = true;
		for(i = 1; i < n; i++)
		{
			if((dist(ply[0]-ply[i])) > PR)
			{
				swap(ply[1], ply[i]);
				tmp = false;
				break;
			}
		}
		if(tmp)return ;
		tmp = true;
		for(i = 2; i < n; i++)
		{
			if((dist((ply[0]-ply[1])*(ply[1]-ply[i]))) > PR)
			{
				swap(ply[2], ply[i]);
				tmp = false;
				break;
			}
		}
		if(tmp) return ;
		tmp = true;
		for(i = 3; i < n; i++)
		{
			if(fabs((ply[0]-ply[1])*(ply[1]-ply[2])^(ply[0]-ply[i]))>PR)
			{
				swap(ply[3], ply[i]);
				tmp =false;
				break;
			}
		}
		if(tmp)return ;
		fac add;
		for(i = 0; i < 4; i++)
		{
			add.a = (i+1)%4, add.b = (i+2)%4, add.c = (i+3)%4, add.ok = 1;
			if((ptoplane(ply[i], add))>0)
				swap(add.b, add.c);
			vis[add.a][add.b] = vis[add.b][add.c] = vis[add.c][add.a] = trianglecnt;
			tri[trianglecnt++] = add;
		}
		for(i = 4; i < n; i++)
		{
			for(j = 0; j < trianglecnt; j++)
			{
				if(tri[j].ok && (ptoplane(ply[i], tri[j])) > PR)
				{
					dfs(i, j); break;
				}
			}
		}
		int cnt = trianglecnt;
		trianglecnt = 0;
		for(i = 0; i < cnt; i++)
		{
			if(tri[i].ok)
				tri[trianglecnt++] = tri[i];
		}
	}
	double area()
	{
		double ret = 0;
		for(int i = 0; i < trianglecnt; i++)
			ret += area(ply[tri[i].a], ply[tri[i].b], ply[tri[i].c]);
		return ret/2.0;
	}
	double volume()
	{
		TPoint p(0,0,0);
		double ret = 0;
		for(int i = 0; i < trianglecnt; i++)
			ret += volume(p, ply[tri[i].a], ply[tri[i].b], ply[tri[i].c]);
		return fabs(ret/6);
	}
}hull;

int main(){
	int Cas = 1;
	while(scanf("%d",&hull.n), hull.n){
		int i ;
		for(i = 0; i < hull.n; i++)
			scanf("%lf %lf %lf",&hull.ply[i].x, &hull.ply[i].y, &hull.ply[i].z);
		hull.construct();
		printf("Case %d: %.2lf\n", Cas++, hull.area());
	}
	return 0;
}


版权声明:本文博主原创文章,博客,未经同意不得转载。

相关文章:

  • html: Table合并行和列
  • win10升级提示图标的四种关闭方法
  • window平台下的MySQL快速安装。(不好意思,未完成待续,请飘过)
  • 教您如何检查oracle死锁,决解死锁
  • openlayers限制地图拖动区域
  • 测试人员的职业修养
  • 批生产数据库
  • 彩色图像--色彩空间 HSI(HSL)、HSV(HSB)
  • java中Map,List与Set的区别
  • 利用print2flashsetup.exe文档转swf
  • poj 3254 Corn Fields 国家压缩dp
  • [实战]MVC5+EF6+MySql企业网盘实战(5)——登录界面,头像等比例压缩
  • [转]Java输入输出流的使用详细介绍
  • 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数005·graphics-obj,基本绘图单元,包括线段、矩形、椭圆、圆形...
  • iOS app无launch screen.xib 对各个版本进行适配
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • 【面试系列】之二:关于js原型
  • Apache的基本使用
  • Electron入门介绍
  • Fastjson的基本使用方法大全
  • HTML中设置input等文本框为不可操作
  • js
  • JS 面试题总结
  • markdown编辑器简评
  • MySQL的数据类型
  • PHP CLI应用的调试原理
  • react-native 安卓真机环境搭建
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • text-decoration与color属性
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 对JS继承的一点思考
  • 给初学者:JavaScript 中数组操作注意点
  • 你不可错过的前端面试题(一)
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 再次简单明了总结flex布局,一看就懂...
  • 在weex里面使用chart图表
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • 如何正确理解,内页权重高于首页?
  • ​马来语翻译中文去哪比较好?
  • # 数据结构
  • #Linux(帮助手册)
  • (arch)linux 转换文件编码格式
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (floyd+补集) poj 3275
  • (javascript)再说document.body.scrollTop的使用问题
  • (libusb) usb口自动刷新
  • (待修改)PyG安装步骤
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (四) 虚拟摄像头vivi体验
  • (转)自己动手搭建Nginx+memcache+xdebug+php运行环境绿色版 For windows版