poj 1410 Intersection

题目地址:poj1410

判断线段和矩形区域是否有公共点。

1 和线段系相交

2 线段在矩形内部


其中有公共点也算是相交 不一定是规范相交。

非规范相交的模板先写错了。并不是直接把两个<0 改成<=0 就可以了

应该在两线段共线的时候判断是不是其中一个端点在另一个线段上。

3  线段在矩形内,额,没有用dcmp也行。

4  给出的点并不一定是左上和右下呀。 只是至少是矩形的一对对角线如此而已。


代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
const double eps=1e-8;


using namespace std;


struct Point{
    double x;
    double y;
    Point(double x=0,double y=0):x(x),y(y){}
    void operator<<(Point &A) {cout<<A.x<<' '<<A.y<<endl;}
};

int dcmp(double x)  {return (x>eps)-(x<-eps); }

typedef  Point  Vector;

Vector  operator +(Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y);}

Vector  operator -(Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); }

Vector  operator *(Vector A,double p) { return Vector(A.x*p,A.y*p);  }

Vector  operator /(Vector A,double p) {return Vector(A.x/p,A.y/p);}

// ps  cout

ostream &operator<<(ostream & out,Point & P) { out<<P.x<<' '<<P.y<<endl; return out;}

bool  operator< (const Point &A,const Point &B) { return A.x<B.x||(A.x==B.x&&A.y<B.y); }

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


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

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

double  Length(Vector A)  { return sqrt(Dot(A, A));}


double  Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));}

double  Area2(Point A,Point B,Point C ) {return Cross(B-A, C-A);}

Vector Rotate(Vector A,double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}
Vector Normal(Vector A) {double L=Length(A);return Vector(-A.y/L,A.x/L);}

Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
    Vector u=P-Q;
    double t=Cross(w, u)/Cross(v,w);
    return P+v*t;
    
}

double DistanceToLine(Point P,Point A,Point B)
{
    Vector v1=P-A; Vector v2=B-A;
    return fabs(Cross(v1,v2))/Length(v2);
    
}

double DistanceToSegment(Point P,Point A,Point B)
{
    if(A==B)  return Length(P-A);
    
    Vector v1=B-A;
    Vector v2=P-A;
    Vector v3=P-B;
    
    if(dcmp(Dot(v1,v2))==-1)    return  Length(v2);
    else if(Dot(v1,v3)>0)    return Length(v3);
    
    else return DistanceToLine(P, A, B);
    
}

Point GetLineProjection(Point P,Point A,Point B)
{
    Vector v=B-A;
    Vector v1=P-A;
    double t=Dot(v,v1)/Dot(v,v);
    
    return  A+v*t;
}

bool  OnSegment(Point P,Point A,Point B)
{
    return dcmp(Cross(P-A, P-B))==0&&dcmp(Dot(P-A,P-B))<=0;
}
bool  SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
   
    double c1=Cross(b1-a1, a2-a1);
    double c2=Cross(b2-a1, a2-a1);
    double c3=Cross(a1-b1, b2-b1);
    double c4=Cross(a2-b1, b2-b1);
    
    if(c1==0&&c2==0&&c3==0&&c4==0)
    {
        
        if(OnSegment(a1, b1, b2))  return 1;
        if(OnSegment(a2, b1, b2))  return 1;
        return 0;
    }
    
    return dcmp(c1)*dcmp(c2)<=0&&dcmp(c3)*dcmp(c4)<=0 ;
    
}



double PolygonArea(Point *p,int n)
{
    double area=0;
    
    for(int i=1;i<n-1;i++)
    {
        area+=Cross(p[i]-p[0], p[i+1]-p[0]);
        
    }
    return area/2;
    
}

Point  read_point()
{
    Point P;
    scanf("%lf%lf",&P.x,&P.y);
    return  P;
}




int main()
{
    Point p[5];
    Point a[2];
    int n;
    cin>>n;
    
    while(n--)
    {
        a[0]=read_point();
        a[1]=read_point();
        p[0]=read_point();
        p[2]=read_point();
        
        p[1]=Point(p[2].x,p[0].y);
        p[3]=Point(p[0].x,p[2].y);
        p[4]=p[0];
        
        bool ok=1;
        
        for(int i=0;i<4;i++)
            if(SegmentProperIntersection(a[0], a[1], p[i], p[i+1]))
            {
                ok=0;
                break;
            }
        
        if(max(a[0].x,a[1].x)<=max(p[0].x,p[2].x)&&min(a[0].x,a[1].x)>=min(p[0].x,p[2].x)&&max(a[0].y,a[1].y)<=max(p[0].y,p[2].y)&&min(a[0].y,a[1].y)>=min(p[0].y,p[2].y))
            ok=0;
        
        
        if(ok) cout<<"F"<<endl;
        else  cout<<"T"<<endl;
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值