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;
}