http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28235#problem/A

题目大意:给出三维空间两个三角形三个顶点,判断二者是否有公共点,三角形顶点、边、内部算三角形的一部分。
解题思路:见模板
//*******************************************************************************
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdio.h>
using namespace std;
#define eps 1e-8 int dcmp(double x){
if(fabs(x)<eps)return ;
else return x< ? -:;
} struct Point3{
double x,y,z;
Point3(double x=,double y=,double z=):x(x),y(y),z(z){}
};
bool operator==(const Point3& a,const Point3& b){
return dcmp(a.x-b.x)== && dcmp(a.y-b.y)== && dcmp(a.z-b.z)== ;
}
typedef Point3 Vector3;
Vector3 operator+(Vector3 A,Vector3 B){
return Vector3(A.x+B.x,A.y+B.y,A.z+B.z);
}
Vector3 operator-(Vector3 A,Vector3 B){
return Vector3(A.x-B.x,A.y-B.y,A.z-B.z);
}
Vector3 operator*(Vector3 A,double p){
return Vector3(A.x*p,A.y*p,A.z*p);
}
Vector3 operator/(Vector3 A,double p){
return Vector3(A.x/p,A.y/p,A.z/p);
} double Dot(Vector3 A,Vector3 B){return A.x*B.x+A.y*B.y+A.z*B.z;}
double Length(Vector3 A){return sqrt(Dot(A,A));}
double Angle(Vector3 A,Vector3 B){return acos(Dot(A,B)/Length(A)/Length(B));} /*p到平面p0-n的距离
double DistanceToPlane(Point3 p,Point3 p0,Vector3 n){
return fabs(Dot(p-p0,n))/Length(n);
}
//p到平面p0-n的投影
Point3 GetPlaneProjection(Point3 p,Point3 p0,Vector3 n){
double d=Dot(p-p0,n)/Length(n);
return p+n*d;
}
//直线p1-p2到平面p0-n的交点
Point3 LinePlaneIntersection(Point3 p1,Point3 p2,Point3 p0,Vector3 n){
Vector3 v=p2-p1;
double t=(Dot(n,p0-p1)/Dot(n,p2-p1));//判断分母是否为0
return p1+v*t;//如果是线段,判断t是不是在0-1之间
}*/ //叉积
Vector3 Cross(Vector3 A,Vector3 B){
return Vector3(A.y*B.z-A.z*B.y,A.z*B.x-A.x*B.z,A.x*B.y-A.y*B.x);
}
double Area2(Point3 A,Point3 B,Point3 C){return Length(Cross(B-A,C-A));}
//点p在三角形p0p1p2中(利用面积法算点是否在三角形内,假定所有的点共面)
bool PointInTri(Point3 p,Point3 p0,Point3 p1,Point3 p2){
double area1=Area2(p,p0,p1);
double area2=Area2(p,p1,p2);
double area3=Area2(p,p2,p0);
return dcmp(area1+area2+area3-Area2(p0,p1,p2))==;
}
//三角形p0p1p2是否和线段AB相交(此函数会把线段在平面上的情况视为不相交)
bool TriSegIntersection(Point3 p0,Point3 p1,Point3 p2,Point3 A,Point3 B,Point3& p){
Vector3 n=Cross(p1-p0,p2-p0);
if(dcmp(Dot(n,B-A))==)return false;//平行或共面
else{ //直线AB和平面P0P1P2有唯一交点
double t=Dot(n,p0-A)/Dot(n,B-A);
if(dcmp(t)< || dcmp(t-)>)return false;//交点不在线段AB上
p=A+(B-A)*t; //计算交点
return PointInTri(p,p0,p1,p2); //判断交点是否在三角形内
}
}
/*到直线的距离
double DistanceToLine(Point3 p,Point3 A,Point3 B){
Vector3 v1=B-A,v2=p-A;
return Length(Cross(v1,v2))/Length(v1);
}
//点p到线段AB的距离
double DistanceToSegment(Point3 p,Point3 A,Point3 B){
if(A==B)return Length(p-A);
Vector3 v1=B-A,v2=p-A,v3=p-B;
if(dcmp(Dot(v1,v2))<0)return Length(v2);
else if(dcmp(Dot(v1,v3))>0)return Length(v3);
else return Length(Cross(v1,v2))/Length(v1);
}
//返回,,的混合积,他等于四面体邮箱面积的6倍
double Volume6(Point3 A,Point3 B,Point3 C,Point3 D){
return Dot(D-A,Cross(B-A,C-A));
}*/
//判断两个三角形是否有公共点
bool TriTriIntersection(Point3* T1,Point3* T2){
Point3 p;
for(int i=;i<;i++){
if(TriSegIntersection(T1[],T1[],T1[],T2[i],T2[(i+)%],p))return true;
if(TriSegIntersection(T2[],T2[],T2[],T1[i],T1[(i+)%],p))return true;
}
return false;
}
//*******************************************************************************
int main(){
int T;cin>>T;
while(T--){
Point3 T1[],T2[];
for(int i=;i<;i++)cin>>T1[i].x>>T1[i].y>>T1[i].z;
for(int i=;i<;i++)cin>>T2[i].x>>T2[i].y>>T2[i].z;
cout<<(TriTriIntersection(T1,T2) ? "1\n":"0\n");
}return ;
}
//*******************************************************************************

[ACM_几何] F. 3D Triangles (三维三角行相交)的更多相关文章

  1. 老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破

    你见过能动起来的文档吗? 这可不是动图,也不是视频,而是可以直接自由交互3D模型的3D OFD文档! OFD可能有人不熟悉,它其实是国产"PDF",3D OFD则突破了以往文字.图 ...

  2. [ACM_几何] Pipe

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28417#problem/B     本题大意: 给定一个管道上边界的拐点,管道宽为1,求 ...

  3. [ACM_几何] Fishnet

      http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28417#problem/C 本题大意:有一个1X1的矩形,每边按照从小到大的顺序给n ...

  4. [ACM_几何] Metal Cutting(POJ1514)半平面割与全排暴力切割方案

    Description In order to build a ship to travel to Eindhoven, The Netherlands, various sheet metal pa ...

  5. [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]

    Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...

  6. uva 11275 3D Triangles

    题意:三维空间中,给出两个三角形的左边,问是否相交. 面积法判断点在三角形内: #include<cstdio> #include<cmath> #include<cst ...

  7. ACM_求f(n)

    求f(n) Time Limit: 2000/1000ms (Java/Others) Problem Description: 设函数f(n)=1*1*1+2*2*2+3*3*3+...+n*n*n ...

  8. Leetcode883.Projection Area of 3D Shapes三维形体投影面积

    在 N * N 的网格中,我们放置了一些与 x,y,z 三轴对齐的 1 * 1 * 1 立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上. 现在,我们查 ...

  9. [ACM_几何] The Deadly Olympic Returns!!! (空间相对运动之最短距离)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28235#problem/B 题目大意: 有两个同时再空间中匀速运动的导弹,告诉一个时间以 ...

随机推荐

  1. 如何用Tacker将NFV带入OpenStack?

    最初社区里很多人争论过NFV是否属于OpenStack,而后来可以确定的是OpenStack的确占据了NFV会话中的很大一部分,并且形象地反映在了下面的ETSI MANO概念架构图中,OpenStac ...

  2. ngnix 502 bad gateway 的解决办法之空间满了

    网站一直运行都很正常,但某天登录后台却出现502 bad gateway,上网搜索都是一大堆解决办法,没可操作性.网站难道出现安全问题?这个应该很少概率.最后排查发现空间满了. 使用命令:df -hl ...

  3. 检查Chunksum与Chunk Data之间的缓冲区发送到DataNode节点

    我们会看到左边"iOS Apps"下面有四个选项:"Certificates"."Identifiers"."Devices&qu ...

  4. Spring In action chapter1_wiringBeans

    Automatically wiring beans Spring attacks automatic wiring from two angles: Component scanning-Sprin ...

  5. iframe自适应高度,根据src中页面来得到。

    在页面布局时,div+iframe能根据具体的大小进行布局.但,对于iframe的适应大小,仍需js获取内部html文件来改变. function reinitIframe(){ var iframe ...

  6. Java中的继承与静态static等的执行先后顺序

    package extend; public class X { Y y=new Y(); static{  System.out.println("tttt"); } X(){  ...

  7. winfrom调用FastReport模板

    '存放模板的路径 If Dir(Application.StartupPath & "\ReportsFile\HB\01\IN.frx", FileAttribute.N ...

  8. AngularJS是什么

    先标明来源: https://code.angularjs.org/1.3.15/docs/guide/introduction 也就是官网针对1.3.15版的说明 What Is Angular? ...

  9. Unity IOC容器的简单应用(转)

    转自:http://blog.csdn.net/wanzhuan2010/article/details/7763280 Unity是Unity是微软patterns& practices组用 ...

  10. linux下的5个查找命令

    在Linux中,有很多方法可以做到这一点.国外网站LinuxHaxor总结了五条命令,你可以看看自己知道几条.大多数程序员,可能经常使用其中的2到3条,对这5条命令都很熟悉的人应该是不多的. 1. f ...