线段相交:Intersection

来源:互联网 发布:淘宝改版中国质造 编辑:程序博客网 时间:2024/04/30 14:18
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

14 9 11 2 1 5 7 1

Sample Output

F
#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){//p3对于p1->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;}



原创粉丝点击