[POJ 1410]Intersection

Intersection
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15055   Accepted: 3937

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

题目大意:

给你一个实心矩形左上角和右下角和一条线段,问你这条线段是否和矩形相交。

题解:

首先判断这个线段是否在矩形内,然后判断线段是否和矩形的四条边相交。

注意以下几点:

1.矩形左上角和右下角如果反过来输入的话要swap一下。

2.先用快速排斥实验再用跨立实验。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long lol;
double n,x1,x2,yy,y2;
struct Vector
{
    double x,y;
    Vector() {}
    Vector(double xx,double yy){x=xx;y=yy;}
    Vector operator+(Vector b)
    {
        Vector ans;
        ans.x=x+b.x;ans.y=y+b.y;
        return ans;
    }
    Vector operator-(Vector b)
    {
        Vector ans;
        ans.x=x-b.x;ans.y=y-b.y;
        return ans;
    }
    double operator*(Vector b)
    {
        return x*b.y-b.x*y;
    }
    double operator^(Vector b)
    {
        return x*b.x+y*b.y;
    }
};
struct node
{
    Vector a,b;
}x,a,b,c,d;
lol gi()
{
    lol ans=0,f=1;
    char i=getchar();
    while(i<'0'||i>'9'){if(i=='-')f=-1;i=getchar();}
    while(i>='0'&&i<='9'){ans=ans*10+i-'0';i=getchar();}
    return ans*f;
}
bool judge(node a,node b)
{
    if(min(a.a.x,a.b.x)<=max(b.a.x,b.b.x)&&min(b.a.x,b.b.x)<=max(a.a.x,a.b.x)&&min(a.a.y,a.b.y)<=max(b.a.y,b.b.y)&&min(b.a.y,b.b.y)<=max(a.a.y,a.b.y))
    {
        if(((a.b-a.a)*(b.a-a.a))*((a.b-a.a)*(b.b-a.a))<=0&&((b.b-b.a)*(a.b-b.a))*((b.b-b.a)*(a.a-b.b))<=0)return true;//与四条边相交
    }
    return false;
}
int main()
{
    int i,j;
    n=gi();
    while(n--)
    {
        scanf("%lf%lf%lf%lf",&x.a.x,&x.a.y,&x.b.x,&x.b.y);
        scanf("%lf%lf%lf%lf",&x1,&yy,&x2,&y2);
        if(x1>x2)swap(x1,x2);if(yy<y2)swap(yy,y2);
        a.a.x=x1;a.a.y=yy;a.b.x=x2;a.b.y=yy;
        b.a.x=x2;b.a.y=yy;b.b.x=x2;b.b.y=y2;
        c.a.x=x1;c.a.y=yy;c.b.x=x1;c.b.y=y2;
        d.a.x=x1;d.a.y=y2;d.b.x=x2;d.b.y=y2;
        //完全包含在矩形内
        if((x.a.x>=x1&&x.a.x<=x2&&x.a.y>=y2&&x.a.y<=yy)||(x.b.x>=x1&&x.b.x<=x2&&x.b.y>=y2&&x.b.y<=yy))
        {
            printf("T\n");
            continue;
        }
        if(judge(x,a)||judge(x,b)||judge(x,c)||judge(x,d))
        {
            printf("T\n");
            continue;
        }
        printf("F\n");
    }
    return 0;
}

 

posted @ 2017-08-03 09:40  kakakakakaka  阅读(161)  评论(0编辑  收藏  举报

Never forget why you start

//鼠标爆炸特效