注意:线段完全在矩形内也是相交,注意输入。
解法:分别判断线段是否跟矩形的四条边相交,再判断线段的两个端点是否都在矩形内
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<map>
#include<iostream>
#include<math.h>
#include <queue>
#include<algorithm>
using namespace std;
#define inf 2147483646
#define pi 3.1415926535898
#define N 33
#define LL __int64
struct point
{
double x,y;
};
double xmulti(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool isjiao(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))&&
(xmulti(s1,s2,e1)*xmulti(s1,e1,e2)>=0)&&
(xmulti(s2,s1,e2)*xmulti(s2,e2,e1)>=0);
}
point a,b,c,d,e,f;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int xs, ys, xe, ye, xl, yt, xr, yb;
int xx,yy,x,y;
int ans=0;
scanf("%d%d%d%d%d%d%d%d",&xs,&ys,&xe,&ye,&xl,&yt,&xr,&yb);
x=min(xl,xr);
xx=max(xl,xr);
y=min(yt,yb);
yy=max(yt,yb);
if(xs>=x&&xs<=xx&&ys>=y&&ys<=yy)//线段是否落在矩形内
{
printf("T\n");
continue;
}
if(xe>=x&&xe<=xx&&ye>=y&&ye<=yy)
{
printf("T\n");
continue;
}
a.x =x;a.y=yy;
b.x=x;b.y=y;
c.x=xx;c.y=y;
d.x=xx;d.y=yy;
e.x=xs;e.y=ys;
f.x=xe;f.y=ye;
if(isjiao(a,b,e,f))
ans=1;
if(isjiao(a,d,e,f))
ans=1;
if(isjiao(b,c,e,f))
ans=1;
if(isjiao(c,d,e,f))
ans=1;
if(ans)
printf("T\n");
else
printf("F\n");
}
return 0;
}