POJ 1410 Intersection(判断线段和矩形是否相交)

来源:互联网 发布:质量成本优化方法 编辑:程序博客网 时间:2024/04/30 08:17

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

14 9 11 2 1 5 7 1

Sample Output

F

【思路分析】

   判断线段和矩形是否相交。分为两种情况:第一种情况就是线段和矩形其中某一边相交时,便满足条件,即转化为判断线段与线段是否相交的问题;第二种情况就是线段在矩形之内,即线段的两个端点在矩形之内(可以不判断线段的端点是否在矩形的边上,因为第一种情况已经考虑过)。如果不满足上述两种情况,则线段在矩形外,即不和矩形相交。还有一个注意点就是要对矩形的四个点排个序,因为“The terms top left and bottom right do not imply any ordering of coordinates”。


代码如下:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define eps 1e-8struct Point{    double x,y;    Point()    {    }    Point(double x1,double y1)    {        x = x1;        y = y1;    }    Point operator - (const Point &b) const    {        return Point(x - b.x,y - b.y);    }    double operator ^ (const Point &b) const    {        return x * b.y - y * b.x;    }};struct Line{    Point s,e;    Line()    {    }    Line(Point s1,Point e1)    {        s = s1;        e = e1;    }};int sgn(double x){    if(fabs(x) < eps)//等于0        return 0;    if(x < 0)//小于0        return -1;    return 1;//大于0}bool intersection(Line l1,Line l2)//判断两线段是否相交{    return    max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&    max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&    max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&    max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&    sgn((l2.s - l1.e) ^ (l1.s - l1.e)) * sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0 &&    sgn((l1.s - l2.e) ^ (l2.s - l2.e)) * sgn((l1.e - l2.e) ^ (l2.s - l2.e)) <= 0;}bool onSeg(Point p,Line l)//判断点是否在直线上{    return    sgn((l.s - p) ^ (l.e - p)) == 0 &&    sgn((l.s.x - p.x) * (l.e.x - p.x)) <= 0 &&    sgn((l.s.y - p.y) * (l.e.y - p.y)) <= 0;}int inConvexPoly(Point a,Point p[],int n)//判断点和凸多边形的位置关系(凸多边形的点逆时针排序){    for(int i = 0;i < n;i++)    {        if(sgn((p[i] - a) ^ (p[(i + 1) % n] - a)) < 0)//点在凸多边形外,若点为顺时针排序改为大于号            return -1;        /*        if(onSeg(a,Line(p[i],p[(i + 1) % n])))//点在凸多边形的边上            return 0;        */    }    return 1;//点在凸多边形内}int main(){    int t;    double x1,x2,y1,y2;    scanf("%d",&t);    while(t--)    {        scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);        Line line(Point(x1,y1),Point(x2,y2));        scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);        Point point[4];        if(x1 > x2)        {            double temp = x1;            x1 = x2;            x2 = temp;        }        if(y1 > y2)        {            double temp = y1;            y1 = y2;            y2 = temp;        }        point[0] = Point(x1,y1);        point[1] = Point(x2,y1);        point[2] = Point(x2,y2);        point[3] = Point(x1,y2);//逆时针排序        int n = 4;        bool flag = false;        for(int i = 0;i < n;i++)//判断线段是否和矩形的四条边相交        {            if(intersection(line,Line(point[i],point[(i + 1) % n])))            {                flag = true;                break;            }        }        if(flag)        {            printf("T\n");            continue;        }        if(inConvexPoly(line.s,point,n) > 0 && inConvexPoly(line.e,point,n) > 0)//判断线段是否在矩形内        {            printf("T\n");            continue;        }        printf("F\n");    }    return 0;}



0 0
原创粉丝点击