2012-05-31 07:30:02Morris

[UVA][幾何] 191 - Intersection


 Intersection 

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)

  figure27
Figure: 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:

displaymath45

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 tex2html_wrap_inline55 and tex2html_wrap_inline57 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

外積判斷兩線段似否相交, 一個順一個逆對於四個點都是,

判斷四個邊與線段是否有相交, 以及是否存在線段的一點在矩形中


#include <stdio.h>

#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
struct Point {
    double x, y;
};
struct Segment {
    Point s, t;
};
double in(double a, double b, double c) {
    return c >= a && c <= b;
}
int onLine(Segment a, Point c) {
    static double minx, maxx, miny, maxy;
    minx = min(a.s.x, a.t.x);
    maxx = max(a.s.x, a.t.x);
    miny = min(a.s.y, a.t.y);
    maxy = max(a.s.y, a.t.y);
    if(in(minx, maxx, c.x) != 0 && in(miny, maxy, c.y) != 0) {
        if((a.s.x-a.t.x)*(c.y-a.s.y) == (a.s.y-a.t.y)*(c.x-a.s.x)) {
            return 1;
        }
    }
    return 0;
}
double cross(Point &o, Point &a, Point &b) {
    return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
int intersection(Segment a, Segment b) {
    if(onLine(a, b.s) || onLine(a, b.t) || onLine(b, a.s) || onLine(b, a.t))
        return 1;
    if(cross(a.s, a.t, b.s)*cross(a.s, a.t, b.t) < 0 &&
       cross(a.t, a.s, b.s)*cross(a.t, a.s, b.t) < 0 &&
       cross(b.s, b.t, a.s)*cross(b.s, b.t, a.t) < 0 &&
       cross(b.t, b.s, a.s)*cross(b.t, b.s, a.t) < 0
       )
        return 1;
    return 0;
}
int main() {
    int t;
    Segment line, edge;
    double xl, yt, xr, yb, tmp;
    scanf("%d", &t);
    while(t--) {
        scanf("%lf %lf", &line.s.x, &line.s.y);
        scanf("%lf %lf", &line.t.x, &line.t.y);
        scanf("%lf %lf %lf %lf", &xl, &yt, &xr, &yb);
        if(xl > xr)
            tmp = xl, xl = xr, xr = tmp;
        if(yt < yb)
            tmp = yt, yt = yb, yb = tmp;
        //printf("%d %d %d %d", xl, xr, yb, yt);
        if(in(xl, xr, line.s.x) && in(yb, yt, line.s.y))
            puts("T");
        else if(in(xl, xr, line.t.x) && in(yb, yt, line.t.y))
            puts("T");
        else {
            int flag = 0;
            edge.s.x = xl, edge.s.y = yt, edge.t.x = xl, edge.t.y = yb;
            flag |= intersection(edge, line);
            edge.s.x = xr, edge.s.y = yt, edge.t.x = xr, edge.t.y = yb;
            flag |= intersection(edge, line);
            edge.s.x = xl, edge.s.y = yb, edge.t.x = xr, edge.t.y = yb;
            flag |= intersection(edge, line);
            edge.s.x = xl, edge.s.y = yt, edge.t.x = xr, edge.t.y = yt;
            flag |= intersection(edge, line);
            if(flag)
                puts("T");
            else
                puts("F");
        }
    }
    return 0;
}
/*
5
0 -18 -8 -12 -1 -1 -11 -11
*/