//以下为原blog搬迁过来的内容
【题目大意】:给出一条线段和一个矩形,判断线段和矩形是否相交。
【解题思路】:判给出的线段与矩形的每条线段是否相交(包括相交和非规范相交)。
1、内含也算相交(WA一次) 2、看了discuss,发现题目给出矩形的点并不一定是左上角的点和右下角的点(WA2次)
【代码】:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
using namespace std;
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
struct Point
{
double x, y;
Point() {}
Point(double a, double b)
{
x = a, y = b;
}
}point[20];
struct Line
{
Point a, b;
Line() {}
Line(Point x, Point y)
{
a = x, b = y;
}
}line,line1[20];
inline int sig(double k)
{
return k < -eps ? -1 : k > eps;
}
inline double det(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
}
inline double xmult(Point o, Point a, Point b)
{
return det(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);
}
inline double dotdet(double x1, double y1, double x2, double y2)
{
return x1 * x2 + y1 * y2;
}
inline double dot(Point o, Point a, Point b)
{
return dotdet(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);
}
inline int between(Point o, Point a, Point b)
{
return sig(dot(o, a, b));
}
inline int intersect1(Point a, Point b, Point c, Point d) {
double s1, s2, s3, s4;
int d1 = sig(s1 = xmult(a, b, c));
int d2 = sig(s2 = xmult(a, b, d));
int d3 = sig(s3 = xmult(c, d, a));
int d4 = sig(s4 = xmult(c, d, b));
if ((d1^d2) == -2 && (d3^d4) == -2)
{
return 1;
}
if (d1 == 0 && between(c, a, b) <= 0) return 2;
if (d2 == 0 && between(d, a, b) <= 0) return 2;
if (d3 == 0 && between(a, c, d) <= 0) return 2;
if (d4 == 0 && between(b, c, d) <= 0) return 2;
return 0;
}
inline int intersect(Line u, Line v)
{
return intersect1(u.a, u.b, v.a, v.b);
}
int main()
{
int T;
double p,q,m,n,p1,q1,m1,n1;
scanf("%d",&T);
while (T--)
{
scanf("%lf%lf",&p1,&q1);
scanf("%lf%lf",&m1,&n1);
point[1]=Point(m1,n1);
point[0]=Point(p1,q1);
line=Line(point[0],point[1]);
scanf("%lf%lf%lf%lf",&p,&q,&m,&n);
if (p>m && q<n)
{
swap(p,m);
swap(q,n);
}
else
if (p>m && q>n) swap(m,p);
else
if (p<m && q<n) swap(q,n);
if ((p1>=p && p1<=m && q1<=q && q1>=n && m1>=p && m1<=m && n1<=q && n1>=n))
{
printf("T\n");
continue;
}
point[2]=Point(p,q);
point[3]=Point(m,q);
point[4]=Point(p,n);
point[5]=Point(m,n);
line1[0]=Line(point[2],point[3]);
line1[1]=Line(point[2],point[4]);
line1[2]=Line(point[3],point[5]);
line1[3]=Line(point[4],point[5]);
int k1,k2,k3,k4;
k1=intersect(line,line1[0]);
k2=intersect(line,line1[1]);
k3=intersect(line,line1[2]);
k4=intersect(line,line1[3]);
//cout << k1 << " " << k2 << " " << k3 << " " << k4 << endl;
if (k1==0 && k2==0 && k3==0 && k4==0)
{
printf("F\n");
}
else printf("T\n");
}
return 0;
}