Intersection

来源:互联网 发布:卤素灯 led 对比 知乎 编辑:程序博客网 时间:2024/04/27 14:34
Intersection
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 11593 Accepted: 3017

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

Source


做这个题都快哭了,明明一个简单的题花了我一下午外加一晚上,最后错的原因是矩形时左上角坐标和右下角坐标并不确定。另外线段在矩形内部是也输出T,因为矩形是实心的,可以想到的错我全犯了,人才啊,下午的比赛啥也没干全弄它了。

其实看矩形的四条边和已知线段是否相交,和线段是否在矩形内部,就可以确定答案的输出。所以代码如下:

#include<stdio.h>
#include<string.h>
#include<string>
#include <math.h>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
struct point
{
    double x,y;
};


double xmult(point p1,point p2,point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}


double dmult(point p1,point p2,point p0)
{
    return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}


int dots_inline(point p1,point p2,point p3)
{
    return zero(xmult(p1,p2,p3));
}


int dot_online_in(point p,point l1,point l2)
{
    return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;
}


int same_side(point p1,point p2,point l1,point l2)
{
    return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;
}


int intersect_in(point u1,point u2,point v1,point v2)
{
    if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))
        return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
    return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}
int main()
{
    point x,y;
    point q[10],p,pp;
    int T,i,j,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p.x,&p.y,&pp.x,&pp.y,&q[4].x,&q[4].y,&q[2].x,&q[2].y);
        q[1].x=q[4].x;
        q[1].y=q[2].y;
        q[3].x=q[2].x;
        q[3].y=q[4].y;
        if(((p.x>=q[2].x&&p.x<=q[4].x)||(p.x>=q[4].x&&p.x<=q[2].x)) && ((p.y>=q[2].y&&p.y<=q[4].y)||(p.y>=q[4].y&&p.y<=q[2].y)) )
        {
                printf("T\n");
                continue;


        }
        if(((pp.x>=q[2].x&&pp.x<=q[4].x)||(pp.x>=q[4].x&&pp.x<=q[2].x)) && ((pp.y>=q[2].y&&pp.y<=q[4].y)||(pp.y>=q[4].y&&pp.y<=q[2].y)))
        {
                printf("T\n");
                continue;
        }
        if(intersect_in(p,pp,q[1],q[2])||intersect_in(p,pp,q[3],q[2])||intersect_in(p,pp,q[3],q[4])||intersect_in(p,pp,q[1],q[4]))
        {
            printf("T\n");
        }
        else
        {
            printf("F\n");
        }
    }
    return 0;
}

0 0