NEUQ-2021自主学习训练-4

该博客介绍了一个用于判断两个三维空间中的三角形是否相交、包含或不相交的简单问题。给定两个三角形的坐标,通过计算它们的边长和海伦公式来确定它们的关系。代码实现中,定义了节点结构体,计算边长和海伦面积,并定义了一个局部判断函数来检查一个点是否在三角形内。通过遍历所有可能的点与三角形组合,最终得出相交、包含或不相交的结论。
摘要由CSDN通过智能技术生成
B - Triangles
This is a simple problem. Given two triangles A and B, you should determine they are intersect, contain or disjoint. (Public edge or point are treated as intersect.)

Input
First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.

For each test case: X1 Y1 X2 Y2 X3 Y3 X4 Y4 X5 Y5 X6 Y6. All the coordinate are integer. (X1,Y1) , (X2,Y2), (X3,Y3) forms triangles A ; (X4,Y4) , (X5,Y5), (X6,Y6) forms triangles B.

-10000<=All the coordinate <=10000

Output
For each test case, output “intersect”, “contain” or “disjoint”.

Sample Input
2
0 0 0 1 1 0 10 10 9 9 9 10
0 0 1 1 1 0 0 0 1 1 0 1
Sample Output
disjoint 
intersect 
#include<bits/stdc++.h>

using namespace std;

struct node{
	double x,y;
}p[4],q[4];

double length(double x1,double y1,double x2,double y2){
	return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
}

double helen(double l1,double l2,double l3){
	double p=(l1+l2+l3)/2;
	return sqrt(p*(p-l1)*(p-l2)*(p-l3));
}

bool local(node p,node a,node b,node c){
	double l1=length(a.x,a.y,b.x,b.y);
	double l2=length(a.x,a.y,c.x,c.y);
	double l3=length(b.x,b.y,c.x,c.y);
	double ll1=length(p.x,p.y,a.x,a.y);
	double ll2=length(p.x,p.y,b.x,b.y);
	double ll3=length(p.x,p.y,c.x,c.y);
	double s1=helen(ll1,ll2,l1);
	double s2=helen(ll1,ll3,l2);
	double s3=helen(ll2,ll3,l3);
	double s=helen(l1,l2,l3);
	if(fabs(s-s1-s2-s3)<0.0000001) return true;
	return false;
}

int main()
{
	int t;
	cin>>t;
	while(t--){
		for(int i=1;i<=3;++i) cin>>p[i].x>>p[i].y;
		for(int i=1;i<=3;++i) cin>>q[i].x>>q[i].y;
		bool flag1=local(p[1],q[1],q[2],q[3]);
		bool flag2=local(p[2],q[1],q[2],q[3]);
		bool flag3=local(p[3],q[1],q[2],q[3]);
		bool flag4=local(q[1],p[1],p[2],p[3]);
		bool flag5=local(q[2],p[1],p[2],p[3]);
		bool flag6=local(q[3],p[1],p[2],p[3]);
		if(!flag1&&!flag2&&!flag3&&!flag4&&!flag5&&!flag6) puts("disjoint");
		else{
			if((flag1&&flag2&&flag3)||(flag4&&flag5&&flag6)) puts("contain");
			else puts("intersect");
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值