24h購物| | PChome| 登入
2012-05-31 07:30:02| 人氣2,705| 回應0 | 上一篇 | 下一篇

[UVA][幾何] 191 - Intersection

推薦 0 收藏 0 轉貼0 訂閱站台


 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
*/



台長: Morris
人氣(2,705) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][幾何] 11343 - Isolated Segments
此分類上一篇:[UVA][模擬] 118 - Mutant Flatworld Explorers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文