poj 1410 Intersection 线段与矩形的关系


Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13986 Accepted: 3656

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

[Submit]   [Go Back]   [Status]   [Discuss]



题意:如果线段与矩形相交或者完全在矩形内部,输出T,否则F

坑点:

给出的矩形“左上”、 “右下”点不一定满足"左"<“右”、“上”<“下”。

题目没说如果线段完全在矩形内,则输出T。


知识点:线段的不规范相交、点在凸多边形内的判定。

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])
#define mem(a,x)  memset(a,x,sizeof a)
#define ysk(x)  (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;

const double PI=cos(-1.0);
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps)  return 0;
    else return x<0?-1:1;
}
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {};
    bool operator ==(const Point B)const {return dcmp(x-B.x)==0&&dcmp(y-B.y)==0;}

    bool operator<(const Point& b)const
    {
        return dcmp(x-b.x)<0|| dcmp(x-b.x)==0 &&dcmp(y-b.y)<0;
    }
};
typedef Point Vector;
Vector operator -(Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y); }


double Cross(Vector A,Vector B)//叉乘
{
    return A.x*B.y-A.y*B.x;
}

double Dot(Vector A,Vector B)//点乘
{
    return A.x*B.x+A.y*B.y;
}

struct Line
{
    Point p,p2;
    Vector v;
    Line(){}
    Line(Point a,Vector v):p(a),v(v){}//点线式
    void twoPointIntial(Point p,Point p2)//两点式
    {
        this->p=p;
        this->p2=p2;
        v=  p2-p;
    }

};
typedef Line Seg;

bool onSegment(Point p,Point a1,Point a2)
{
    return dcmp(Cross(a1-p,a2-p)  )==0&&dcmp(Dot(a1-p,a2-p)  )<=0;
}

bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
    if(onSegment(a1,b1,b2)||onSegment(a2,b1,b2)||onSegment(b1,a1,a2)||onSegment(b2,a1,a2)) return true;
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
           c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}


bool PointIn(Point p,Seg seg[])
{
    for1(i,4)
    {
//        if(dcmp(Cross(seg[i].p2-p, seg[i].p-seg[i].p2 ) )>=0  )  return false;//均可
          if(dcmp(Cross(seg[i].p2-p, seg[i].p-p ) )>=0  )  return false;
    }
    return true;
}
int main()
{
   std::ios::sync_with_stdio(false);
   int T; cin>>T;
   Line seg[5];
   double x[2],y[2];
   double le,up,ri,down;
   while(T--)
   {
       cin>>x[0]>>y[0]>>x[1]>>y[1];
       seg[0].twoPointIntial(Point(x[0],y[0]),Point(x[1],y[1]) );
       cin>>le>>up>>ri>>down;
       if(le>ri)    swap(le,ri);
       if(up<down)  swap(up,down);

       seg[1].twoPointIntial(Point(le,down),Point(ri,down) );
       seg[2].twoPointIntial(Point(ri,down),Point(ri,up  ) );
       seg[3].twoPointIntial(Point(ri,up),Point(le,up) );
       seg[4].twoPointIntial(Point(le,up),Point(le,down) );

       bool inte=false;
       for1(i,4)
       {
           if(SegmentProperIntersection(seg[0].p,seg[0].p2,seg[i].p,seg[i].p2)) {inte=true;break;}
       }
       if(!inte&& PointIn(Point(x[0],y[0]),seg)&&PointIn(Point(x[1],y[1]),seg) )  inte=true;
       puts(inte?"T":"F");
   }
   return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值