joj1131

 1131: Intersection

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 712 174 Standard

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 (xstartystart) is the start and (xendyend) the end point of the line and (xleft,ytop) the top left and (xrightybottom) 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

This problem is used for contest: 117  184 



这是我做的第一个计算几何的问题在这里纪念一下,详细情况请看我博客里面转载的判断线段的相交。。。

#include<iostream>
using namespace std;
struct POINT
{
    double  x;
    double  y;
};
struct LINE
{
    POINT head;
    POINT tail;
};
double crossmul(POINT point1,POINT point2,POINT point3)//计算两个向量的叉乘积
{
    double ax=point1.x-point2.x;
    double ay=point1.y-point2.y;
    double bx=point1.x-point3.x;
    double by=point1.y-point3.y;
    return (ax*by-ay*bx);
}
bool linecross(LINE line1,LINE line2)//判断两个线段是否相交
{
    if((crossmul(line1.head,line1.tail,line2.head)*crossmul(line1.head,line1.tail,line2.tail)<=0)//注意等号
       &&(crossmul(line2.head,line2.tail,line1.head)*crossmul(line2.head,line2.tail,line1.tail)<=0))
    {
        return true;
    }
    else return false ;
}
int main()
{
    int n;
    cin>>n;
    for(int t=1;t<=n;t++)
    {
        double xstart,ystart,xend,yend;
        double xleft,ytop,xright,ybottom;
        cin>>xstart>>ystart>>xend>>yend;
        cin>>xleft>>ytop>>xright>>ybottom;
        if((xstart>=xleft&&xstart<=xright&&ystart>=ybottom&&ystart<=ytop)||
           (xend>=xleft&&xend<=xright&&yend>=ybottom&&yend<=ytop))//判断线段的端点是否有一个在矩形内部
        {
            cout<<"T"<<endl;
        }
        else
        {
            LINE a;
            LINE cross1,cross2;
            a.head.x=xstart;a.head.y=ystart;
            a.tail.x=xend;a.tail.y=yend;
            cross1.head.x=xleft;cross1.head.y=ybottom;
            cross1.tail.x=xright;cross1.tail.y=ytop;
            cross2.head.x=xleft;cross2.head.y=ytop;
            cross2.tail.x=xright;cross2.tail.y=ybottom;
            if(linecross(cross1,a)||linecross(cross2,a))//判断线段与矩形的两个对角线相交
            {
                cout<<"T"<<endl;
            }
            else
            {
                cout<<"F"<<endl;
            }
        }
    }
    return 0;
}

你可能感兴趣的:(struct,File,Integer,input,each,Numbers)