Intersection ,poj1410(判断两线段是否相交)

Description

You are to write a program that has to decide whether a given line segment intersects a given rectangle.

An example:
line: start point: (4,9)
end point: (11,2)
rectangle: left-top: (1,5)
right-bottom: (7,1)

在这里插入图片描述

Figure 1: Line segment does not intersect rectangle

The line is said to intersect the rectangle if the line and the rectangle have at least one point in common. The rectangle consists of four straight lines and the area in between. Although all input values are integer numbers, valid intersection points do not have to lay on the integer grid.

Input

The input consists of n test cases. The first line of the input file contains the number n. Each following line contains one test case of the format:
xstart ystart xend yend xleft ytop xright ybottom

where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) the top left and (xright, ybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms top left and bottom right do not imply any ordering of coordinates.

Output

For each test case in the input file, the output file should contain a line consisting either of the letter “T” if the line segment intersects the rectangle or the letter “F” if the line segment does not intersect the rectangle.

Sample Input

1
4 9 11 2 1 5 7 1
Sample Output

F

超详细ac代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
struct point//记录点的坐标 
{
	double x,y;
};
struct line//记录线段的始端点和尾端点 
{
	point st,ed;
};
//判断两条线段是否相交:https://www.cnblogs.com/tuyang1129/p/9390376.html 
//叉乘:ab X ac 
double mul(point a,point b,point c)
{
	return (b.x -a.x )*(c.y -a.y )-(b.y -a.y )*(c.x -a.x ); 
}
bool find(line l1,line l2)
{
	//如果l1(或l2)两端点的横坐标最大值仍比l2(或l1)两端点的横坐标最小值小,那么一定不相交; 
	if(max(l1.st.x ,l1.ed.x )<min(l2.st.x ,l2.ed.x )||max(l2.st.x ,l2.ed.x )<min(l1.st.x ,l1.ed.x ))
		return false;
	//同理,纵坐标 
	if(max(l1.st.y ,l1.ed.y )<min(l2.st.y ,l2.ed.y )||max(l2.st.y ,l2.ed.y )<min(l1.st.y ,l1.ed.y ))
		return false;
	/*
	定理:向量a×向量b(×为向量叉乘),
	若结果小于0,表示向量b在向量a的顺时针方向;
	若结果大于0,表示向量b在向量a的逆时针方向;
	若等于0,表示向量a与向量b平行。
	
	如果线段CD的两个端点C和D,
	与另一条线段的一个端点(A或B,只能是其中一个)连成的向量,
	与向量AB做叉乘,
	若结果异号,表示C和D分别在直线AB的两边,
	若结果同号,则表示CD两点都在AB的一边,则肯定不相交。
	*/ 

	//叉乘,判断l2的两端点是否满足与l1相交的条件 
	if(mul(l1.st,l1.ed,l2.st)*mul(l1.st,l1.ed,l2.ed)>0)
		return false;
	//同理,判断l1的两端点是否满足与l2相交的条件 
	if(mul(l2.st,l2.ed,l1.st)*mul(l2.st,l2.ed,l1.ed)>0)
		return false;
	return true;
}
bool judge(point a,point b,point c,point d,line l1)
{
	line l2;
	//l2依次代表矩形四边,分别判断是否相交 
	l2.st =a;
	l2.ed =b;
	if(find(l1,l2))
		return true;
	
	l2.st =a;
	l2.ed =d;
	if(find(l1,l2))
		return true;
	
	l2.st =c;
	l2.ed =b;
	if(find(l1,l2))
		return true;
		
	l2.st =c;
	l2.ed =d;
	if(find(l1,l2))
		return true;
	return false;
}
void solve()
{
	int t;
	point a,b,c,d;
	line l;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&l.st.x ,&l.st.y ,&l.ed.x ,&l.ed.y ,&a.x ,&a.y ,&c.x ,&c.y );
		//并非按照左上,右下输入,需判断 
		if(a.x >c.x )
			swap(a.x ,c.x );
		if(a.y <c.y )
			swap(a.y ,c.y );
		int flag=0;
		//逆时针记录矩形四点 
		b.x =a.x ,b.y =c.y ;
		d.x =c.x ,d.y =a.y ;
		//判断线段是否与矩形四条边相交 
		if(judge(a,b,c,d,l))
			flag=1;
		//特殊情况:判断线段是否有一端点在矩形内,若果有那么一定相交(可以理解为这是一个实心矩阵) 
		if((l.st.x >=a.x &&l.st.x <=c.x&&l.st.y>=c.y &&l.st.y<=a.y )||(l.ed.x >=a.x &&l.ed.x <=c.x&&l.ed.y>=c.y &&l.ed.y<=a.y ))
			flag=1;
		
		if(flag)
			printf("T\n");
		else
			printf("F\n");
	}
}
int main()
{
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值