joj1806

 1806: Intersection

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 318 108 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: 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:

where (xstartystart) is the start and (xendyend) the end point of the line and (xleftytop) the top left and (xrightybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms  and  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: 74 

Submit / Problem List / Status / Discuss







#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 crossline(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()
{
    double xstart,ystart,xend,yend;
    double xleft,ytop,xright,ybottom;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>xstart>>ystart>>xend>>yend;
        cin>>xleft>>ytop>>xright>>ybottom;
        if((xstart<=xright&&xstart>=xleft&&ystart<=ytop&&ystart>=ybottom)||
           (xend>=xleft&&xend<=xright&&yend>=ybottom&&yend<=ytop))
        {
            cout<<"T"<<endl;
        }
        else
        {
            LINE line1,line2,a;
            a.head.x=xstart;a.head.y=ystart;
            a.tail.x=xend;a.tail.y=yend;
            line1.head.x=xleft;line1.head.y=ytop;
            line1.tail.x=xright;line1.tail.y=ybottom;
            line2.head.x=xleft;line2.head.y=ybottom;
            line2.tail.x=xright;line2.tail.y=ytop;
            if(crossline(line1,a)||crossline(line2,a))
            {
                cout<<"T"<<endl;
            }
            else cout<<"F"<<endl;
        }
    }
    return 0;
}


这个题目跟joj1131是一样的不说了。。。

你可能感兴趣的:(joj1806)