poj 1410 Intersection 【判断线段 与矩形面是否相交】

Intersection
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13094   Accepted: 3409

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


题意:给你一个线段和一个矩形,问你线段是否与矩形面相交。


思路:先判断线段的两个端点是否在矩形里面,然后判断线段与矩形4条边是否相交。


坑死啊,康总模板。。。少个if  WA到死!


AC代码:


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define eps 1e-6
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define debug printf("\n");
#define MAXN 1000000+1
#define MAXM 100000
#define LL long long
using namespace std;
struct Point{
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y){}
};
Point operator - (Point A, Point B){
    return Point(A.x-B.x, A.y-B.y);
}
double Cross(Point A, Point B){
    return A.x * B.y - A.y * B.x;
}
int dcmp(double x){
    if(fabs(x) < eps)
        return 0;
    else
        return x < 0 ? -1 : 1;
}
bool judge(Point a1, Point a2, Point b1, Point b2)
{
    if(min(a1.x, a2.x) > max(b1.x, b2.x) || min(a1.y, a2.y) > max(b1.y, b2.y) || min(b1.x, b2.x) > max(a1.x, a2.x) || min(b1.y, b2.y) > max(a1.y, a2.y))
        return false;
    double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);
    double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);
    return dcmp(c1) * dcmp(c2) < eps && dcmp(c3) * dcmp(c4) < eps;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        Point l1, l2;
        double x1, y1, x2, y2;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &l1.x, &l1.y, &l2.x, &l2.y, &x1, &y1, &x2, &y2);
        if(x1 > x2)
            swap(x1, x2);
        if(y1 < y2)
            swap(y1, y2);
        if((l1.x > x1 && l1.x < x2 && l1.y < y1 && l1.y > y2) || (l2.x > x1 && l2.x < x2 && l2.y < y1 && l2.y > y2))
        {
            printf("T\n");
            continue;
        }
        Point b, c, d, e;
        b.x = x1, b.y = y1;
        c.x = x2, c.y = y1;
        d.x = x1, d.y = y2;
        e.x = x2, e.y = y2;
        if(judge(l1, l2, b, c) || judge(l1, l2, b, d) || judge(l1, l2, d, e) || judge(l1, l2, c, e))
            printf("T\n");
        else
            printf("F\n");
    }
    return 0;
}


你可能感兴趣的:(几何)