IntersectionTime Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
SubmitStatus

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
struct Point{
    int x;
    int y;
};
 
Point beg, end, lu, ru, ll, rl;
 
double direction(Point p1, Point p2, Point p3){//p3p1->p2
    return (p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y);
}
 
bool online(Point p1, Point p2, Point p3){
    return (p3.x >= min(p1.x, p2.x) && p3.x <= max(p1.x, p2.x) && p3.y >= min(p1.y, p2.y) && p3.y <= max(p1.y, p2.y));
}
 
bool intersect(Point p1, Point p2, Point p3, Point p4){//p1-p2  p3-p4
    double d1 = direction(p3, p4, p1);
    double d2 = direction(p3, p4, p2);
    double d3 = direction(p1, p2, p3);
    double d4 = direction(p1, p2, p4);
    if (d1 * d2 < 0 && d3 * d4 < 0) return true;
    else if (d1 == 0 && online(p3, p4, p1)) return true;
    else if (d2 == 0 && online(p3, p4, p2)) return true;
    else if (d3 == 0 && online(p1, p2, p3)) return true;
    else if (d4 == 0 && online(p1, p2, p4)) return true;
    return false;
}
 
int main(){
    int n, t;
    cin >> t;
    int tmp;
    int x1, y1, x2, y2;
    while(t --){
        scanf("%d %d %d %d", &beg.x, &beg.y, &end.x, &end.y);
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        if(x1 > x2){
            tmp = x1;
            x1 = x2;
            x2 = tmp;
        }
        if(y1 < y2){
            tmp = y1;
            y1 = y2;
            y2 = tmp;
        }
        lu.x = x1;
        lu.y = y1;
        ru.x = x2;
        ru.y = y1;
        ll.x = x1;
        ll.y = y2;
        rl.x = x2;
        rl.y = y2;
        bool flag = false;
        if(intersect(lu, ru, beg, end))
            flag = true;
        else if(intersect(ll, rl, beg, end))
            flag = true;
        else if(intersect(lu, ll, beg, end))
            flag = true;
        else if(intersect(ru, rl, beg, end))
            flag = true;
        else if(beg.x >= x1 && beg.x <= x2 && beg.y <= y1 && beg.y >= y2 && end.x >= x1 && end.x <= x2 && end.y <= y1 && end.y >= y2)
            flag = true;
        if(flag)
            printf("T\n");
        else
            printf("F\n");
    }
    return 0;
}