POJ 1410 Intersection G++ 判断两条线段是否相交 背

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
//英语     看博友注释    抄博友程序     判断两条线段是否相交     背 
struct point{
	double x;
	double y;
}; 
point rec[4];
point sa,ea;
double cross(point a, point b, point c)
{
	return (a.x-b.x)*(c.y-b.y)-(a.y-b.y)*(c.x-b.x);
} 
bool intersection(point s1,point e1,point s2,point e2)//判断两条线段是否相交
{
	return
	  (max(s1.x,e1.x)>=min(s2.x,e2.x)&&
	   max(s2.x,e2.x)>=min(s1.x,e1.x)&&
	   max(s1.y,e1.y)>=min(s2.y,e2.y)&&
	   max(s2.y,e2.y)>=min(s1.y,e1.y)&&
	   cross(s2,e2,e1)*cross(e2,s2,s1)>=0&&
	   cross(e2,s2,e1)*cross(s2,e2,s1)>=0);
}
bool in(point a)//抄博友程序   需使用min max 
{
	return (a.y>=min(rec[0].y,rec[1].y)&&
		    a.y<=max(rec[0].y,rec[1].y)&&
			a.x>=min(rec[0].x,rec[3].x)&& 
			a.x<=max(rec[0].x,rec[3].x));
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>sa.x>>sa.y>>ea.x>>ea.y;
		cin>>rec[0].x>>rec[0].y>>rec[2].x>>rec[2].y;
		rec[1].x=rec[0].x;
		rec[1].y=rec[2].y;
		rec[3].x=rec[2].x;
		rec[3].y=rec[0].y;
		int flag=0;//不相交 
		if(in(sa)||in(ea))
		{
			//cout<<"hi0"<<endl;
			flag=1;
		}else
		{
			if(intersection(rec[0],rec[1],sa,ea))
			{
				//cout<<"hi1"<<endl;
				flag=1;
			}
			if(intersection(rec[0],rec[3],sa,ea))
			{
				//cout<<"hi2"<<endl;
				flag=1;
			}
			if(intersection(rec[3],rec[2],sa,ea))
			{
				//cout<<"hi3"<<endl;
				flag=1;
			}
			if(intersection(rec[1],rec[2],sa,ea))
			{
				//cout<<"hi4"<<endl;
				flag=1;
			}
		}
		if(flag)
		{
			cout<<"T"<<endl;
		}else
		{
			cout<<"F"<<endl;
		}
	}
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值