1. 程式人生 > >poj 1410 Intersection(判斷線段是否與實心矩形相交)

poj 1410 Intersection(判斷線段是否與實心矩形相交)

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
大致題意:告訴你線段的兩個端點座標和矩形的兩個端點座標,問線段是否和矩形相交,矩形是實心的。

思路:判斷一下線段是否和矩形的四條邊相交,如果沒有的話再判斷下是否在矩形內部。
注意:題目給你的矩形的那兩個座標不一定就是左上角和右下角,也可能是左下角和右上角,而且順序也不一定,所以要先自己判斷下。(這裡被坑哭了。。。。)

程式碼如下

#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const double eps=1e-8;
const double INF=0x3f3f3f3f;

int dcmp(double x) 
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}
struct Point 
{
    double x,y;
    Point() {}
    Point(double  _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator-(const Point &b) const {
        return Point(x-b.x,y-b.y);
    }
    double operator *(const Point &b)const {
        return x*b.x + y*b.y;
    }
    double operator ^(const Point &b)const {
        return x*b.y - y*b.x;
    }
    int operator ==(const Point &b)const {
        if(dcmp(x-b.x)==0&&dcmp(y-b.y)==0)//兩點相同 
        return 0;
        else return 1;
    }
};
struct Line 
{
    Point a,b;
    Line() {}
    Line(Point _a,Point _b) 
    {
        a=_a;
        b=_b;
    }
};

double xmult(Point p0,Point p1,Point p2) 
{
    return (p1-p0)^(p2-p0);
}
bool inter(Line L1,Line L2)//判斷兩條線段是否相交,相交返回1 
{
    return
        max(L1.a.x,L1.b.x) >= min(L2.a.x,L2.b.x) &&
        max(L2.a.x,L2.b.x) >= min(L1.a.x,L1.b.x) &&
        max(L1.a.y,L1.b.y) >= min(L2.a.y,L2.b.y) &&
        max(L2.a.y,L2.b.y) >= min(L1.a.y,L1.b.y) &&
        dcmp((L2.a-L1.a)^(L1.b-L1.a))*dcmp((L2.b-L1.a)^(L1.b-L1.a)) <= 0 &&
        dcmp((L1.a-L2.a)^(L2.b-L2.a))*dcmp((L1.b-L2.a)^(L2.b-L2.a)) <= 0; 
}

int main() 
{

    int n;
    double x1,y1,x2,y2,x3,y3,x4,y4;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        Line L=Line(Point(x1,y1),Point(x2,y2));
        scanf("%lf%lf%lf%lf",&x3,&y3,&x4,&y4);

        if(x3>x4)//先自己找出矩形的左上角和右下角座標
        {
            swap(x3,x4);
        }
        if(y3<y4)
        {
            swap(y3,y4);
        }

        Point p1=Point(x3,y3);
        Point p2=Point(x4,y3);
        Point p3=Point(x3,y4);
        Point p4=Point(x4,y4);
        Line L1=Line(p1,p2);
        Line L2=Line(p1,p3);
        Line L3=Line(p2,p4);
        Line L4=Line(p3,p4);
        if(x1>=x3&&x2>=x3&&x1<=x4&&x2<=x4&&y1<=y3&&y2<=y3&&y1>=y4&&y2>=y4)
        {
            printf("T\n");
            continue;
        }
        if(inter(L,L1)||inter(L,L2)||inter(L,L3)||inter(L,L4))          
            printf("T\n");  
        else    
            printf("F\n");
    }
    return 0;
}