POJ 1410 Intersection(矩形和线段的交,线段的交)

来源:互联网 发布:强制uefi安装 ubuntu 编辑:程序博客网 时间:2024/05/16 17:45

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15600 Accepted: 4069
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
Source

Southwestern European Regional Contest 1995

题意:判断线段和矩形是否有交,要是有交就输出T反之F
做法:我的天呐好坑
既要判断线段和矩形的边是不是相交也要判断这个线段是不是在这个矩形里面欧我的天呐矩形是实心的没有注意到
然后写到一半还发现一直以为的线段求交写得其实是错了gg,没考虑到共线却不相交的情况由于以前的数据太水就过了 gg
特此纪念

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <map>#include <stack>#include <vector>#define maxn 100010#define maxe 100010typedef long long ll;using namespace std;const double eps=1e-6;const int inf=0x3f3f3f3f3f;vector<int > ans;int sign(double x){    if(fabs(x) < eps)return 0;    if(x < 0) return -1;    return 1;}typedef double T1;struct Point{    T1 x,y;    Point(){};    Point(T1 a,T1 b)    {        x=a,y=b;    }    void input()    {        scanf("%lf%lf",&x,&y);    }    Point operator +(Point a)    {        Point b(x+a.x,y+a.y);        return b;    }    Point operator -(Point a)    {        Point b(x-a.x,y-a.y);        return b;    }    T1 operator *(Point a)    {        return x*a.x+y*a.y;    }    T1 operator ^(Point a)    {        return x*a.y-y*a.x;    }    bool operator <(Point a)    {        return x<a.x;    }};double mul(Point p1,Point p2,Point p3){    return (p2-p1)^(p3-p1);}double xmul(Point a,Point b,Point c){    return (b-a)*(c-a);}struct Line{    Point a,b;    double k,d;    bool flag;    bool top;    void prepare()    {        if(a.x-b.x!=0)        k=(a.y-b.y)*1.0/(a.x-b.x),flag=true,d=a.y-k*a.x;        else flag=false,d=a.x;        top=true;    }    Line(Point st,Point ed)    {        a=st;        b=ed;        prepare();    }    Line(){};    void input()    {        a.input();        b.input();        prepare();    }    int operator *(Line l)    {        return (a-b)*(l.a-l.b);    }    int operator ^( Line l)    {        return (a-b)^(l.a-l.b);    }    Point intersect(Line l)    {        Point r;double x,y;        if(!flag&&l.flag)        {            x=d;            y=l.k*x+l.d;        }        else if(flag&&!l.flag)        {            x=l.d;            y=k*x+d;        }        else        {            x=(l.d-d)/(k-l.k);            y=k*x+d;        }        printf("POINT %.2lf %.2lf\n",x,y);        r.x=x;        r.y=y;        return r;    }    bool onsegment(Point a,Point b,Point c)   //共线时,判断点是否落在线段上    {        T1 minx=min(a.x,b.x);        T1 maxx=max(a.x,b.x);        T1 miny=min(a.y,b.y);        T1 maxy=max(a.y,b.y);        return c.x>=minx&&c.x<=maxx&&c.y>=miny&&c.y<=maxy;    }    bool inter(Line l)    {        Point p1=a,q1=b,p2=l.a,q2=l.b;        T1 d1=mul(l.a,l.b,a);        T1 d2=mul(l.a,l.b,b);        T1 d3=mul(a,b,l.a);        T1 d4=mul(a,b,l.b);        if(d1*d2<0&&d3*d4<0)            return true;        else if(d1==0&&onsegment(l.a,l.b,a))            return true;        else if(d2==0&&onsegment(l.a,l.b,b))            return true;        else if(d3==0&&onsegment(a,b,l.a))            return true;        else if(d4==0&&onsegment(a,b,l.b))            return true;        return false;}};struct Rec{    Line l1,l2,l3,l4;    Rec(){};    Rec(Point p1,Point p2)    {        Point p3(p2.x,p1.y);        Point p4(p1.x,p2.y);        l1=Line(p1,p3);        l2=Line(p1,p4);        l3=Line(p2,p3);        l4=Line(p2,p4);    }    bool inter(Line l)    {        return l1.inter(l)||l2.inter(l)||l3.inter(l)||l4.inter(l);    }};bool judge(double x1,double x2,double y1,double y2,Point a){    bool b1=x1<a.x&&x2>a.x;    bool b2=y1<a.y&&y2>a.y;    return b1&&b2;}int main(){    int t;    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    scanf("%d",&t);    Rec r;    Line l;    Point p1,p2;    double x1,x2;    double y1,y2;    while(t--)    {        l.input();        p1.input();        p2.input();        x1=min(p1.x,p2.x);        x2=max(p1.x,p2.x);        y1=min(p1.y,p2.y);        y2=max(p1.y,p2.y);        r=Rec(p1,p2);        if(r.inter(l)||(judge(x1,x2,y1,y2,l.a)&&judge(x1,x2,y1,y2,l.b)))            puts("T");        else puts("F");    }    return 0;}