UVA 11275 3D Triangles

点击打开链接

大白p292

水题

 

 

#include <vector>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps=1e-10;//精度
const int INF=1<<29;
const double PI=acos(-1);
int dcmp(double x){//判断double等于0或。。。
	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){}
};
typedef Point Vector;
Vector operator+(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y,a.z+b.z);}
Vector operator-(Point a,Point b){return Vector(a.x-b.x,a.y-b.y,a.z-b.z);}
Vector operator*(Vector a,double p){return Vector(a.x*p,a.y*p,a.z*p);}
Vector operator/(Vector a,double p){return Vector(a.x/p,a.y/p,a.z/p);}
bool operator==(Point a,Point b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0&&dcmp(a.z-b.z)==0;}
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y+a.z*b.z;}
double Length(Vector a){return sqrt(Dot(a,a));}
double Angle(Vector a,Vector b){return acos(Dot(a,b)/Length(a)/Length(b));}

double DistanceToPlane(Point p,Point p0,Vector v){//点p到平面p0 n的距离
	return fabs(Dot(p-p0,v))/Length(v);//fabs取无向距离
}
Point GetPlaneProjection(Point p,Point p0,Vector v){//点p在平面p0 v上的投影
	double d=Dot(p-p0,v)/Length(v);
	return p+v*d;
}
Point LinePlaneIntersection(Point p1,Point p2,Point p0,Vector n){//直线p1 p2到平面p0 n的交点 交点必须唯一
	Vector v=p2-p1;
	double t=(Dot(n,p0-p1)/Dot(n,v));//Dot(n,v)==0时 平行或在平面上
	return p1+v*t;
}
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);
}
double Area2(Point a,Point b,Point c){return Length(Cross(b-a,c-a));}
bool PointInTri(Point p,Point p0,Point p1,Point p2){//点p在三角形p0 p1 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))==0;
}
bool TriSegIntersection(Point a,Point b,Point c,Point p,Point q,Point& Intersection){//三角形abc是否和线段p q相交 Intersection为交点
	Vector n=Cross(b-a,c-a);
	if(dcmp(Dot(n,q-p))==0) return false;//三角形和直线平行或共面
	else{//三角形和直线平行或共面
		double t=Dot(n,a-p)/Dot(n,q-p);
		if(dcmp(t)<0||double(t-1)>0) return false;//交点不在线段AB上
		Intersection=p+(q-p)*t;
		return PointInTri(Intersection,a,b,c);
	}
}
double DistanceToLine(Point p,Point a,Point b){//点p到直线ab的距离
	Vector v1=b-a,v2=p-a;
	return Length(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(Point p,Point a,Point b){
	if(a==b) return Length(p-a);
	Vector 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);
}
//......
int main()
{
	int T;
	scanf("%d",&T);
	while(T--){
		bool ok=false;
		Point T1[3],T2[3],temp;
		for(int i=0;i<3;i++) scanf("%lf%lf%lf",&T1[i].x,&T1[i].y,&T1[i].z);
		for(int i=0;i<3;i++) scanf("%lf%lf%lf",&T2[i].x,&T2[i].y,&T2[i].z);
		for(int i=0;i<3;i++)if(TriSegIntersection(T1[0],T1[1],T1[2],T2[i],T2[(i+1)%3],temp))
			ok=true;
		for(int i=0;i<3;i++)if(TriSegIntersection(T2[0],T2[1],T2[2],T1[i],T1[(i+1)%3],temp))
			ok=true;
		if(ok) puts("1");
		else puts("0");
	}
	return 0;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值