计算几何基础 Intersection POJ - 1410

这个题判断线段和矩形的相交情况
注意线段在矩形内部的情况

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#define cl(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=100;
const double eps=1e-8;
typedef long long ll;
struct point
{
    double x,y;
    point(double _X=0,double _Y=0)
    {
        x=_X;y=_Y;
    }
    point operator - (const point &oth)const
    {
        return point(x-oth.x,y-oth.y);
    }
        point operator + (const point &oth)const
    {
        return point(x+oth.x,y+oth.y);
    }
        point operator * (const double oth)const
    {
        return point(x*oth,y*oth);
    }
        point operator / (const double oth)const
    {
        return point(x/oth,y/oth);
    }
    bool operator == (const point &oth)const
    {
        return x==oth.x&&y==oth.y;
    }
};
int dcmp(double x)
{
    if(fabs(x)<eps)return 0;
    else return x<0?-1:1;
}
double cross(point a,point b)
{
    return a.x*b.y-b.x*a.y;
}
bool segment(point a,point b,point c,point d)
{
    double c1 = cross(b-a,c-a),c2 = cross(b-a,d-a);
    double d1 = cross(d-c,a-c),d2 = cross(d-c,b-c);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(d1)*dcmp(d2)<0;
}
double dot(point a,point b)
{
    return a.x*b.x+a.y*b.y;
}
bool onSegment( point p1,point p2,point p ) {
    if( (p1.x-p.x)*(p2.y-p.y)==(p2.x-p.x)*(p1.y-p.y)&&
            p.x >=min(p1.x,p2.x) && p.x <=max(p1.x,p2.x)&&
            p.y >=min(p1.y,p2.y) && p.y <=max(p1.y,p2.y)  )
        return true;
    else
        return false;
}
bool checkdot(point a,point b,point c,point d,point lst,point led)
{
    if(onSegment(lst,led,a))return false;
    if(onSegment(lst,led,b))return false;
    if(onSegment(lst,led,c))return false;
    if(onSegment(lst,led,d))return false;
    return true;
}
bool checkline(point a,point b,point c,point d,point lst,point led)
{
    if(segment(a,b,lst,led))return false;
    if(segment(b,c,lst,led)) return false;
    if(segment(c,d,lst,led)) return false;
    if(segment(d,a,lst,led)) return false;
    return true;
}
double len(point a)
{
    return  sqrt(dot(a,a));
}
double dists(point p,point a,point b)
{
    if(a==b) return len(p-a);
    point v1=b-a,v2=p-a,v3=p-b;
    if(dcmp(dot(v1,v2))<0) return len(v2);
    else if(dcmp(dot(v1,v3))>0)return len(v3);
    return fabs(cross(v1,v2))/len(v1);
}
int pointin(point p,point a[])
{
    int wn = 0,k,d1,d2;
    for(int i=1;i<=4;i++)
    {
        if(dcmp(dists(p,a[i],a[(i+1-1)%4+1]))==0)return -1;
        k = dcmp(cross(a[(i+1-1)%4+1]-a[i],p-a[i]));
        d1 = dcmp(a[i].y - p.y);
        d2 = dcmp(a[(i+1-1)%4+1].y-p.y);
        if(k>0&&d1<=0&&d2>0)wn++;
        if(k<0&&d2<=0&&d1>0)wn--;
    }
    if(wn)return 1;
    else return 0;
}
point reg[5];
int main()
{
    int n;
    scanf("%d",&n);
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        flag=1;
        point lst,led;
        point a,b,c,d;
        scanf("%lf%lf%lf%lf",&lst.x,&lst.y,&led.x,&led.y);
        scanf("%lf%lf%lf%lf",&a.x,&a.y,&c.x,&c.y);
        b.x=a.x;b.y=c.y;
        d.x=c.x;d.y=a.y;
        if(!checkdot(a,b,c,d,lst,led)) flag=0;
        //cout<<flag<<" 1\n";
        if(!checkline(a,b,c,d,lst,led))flag=0;
       // cout<<flag<<" 2\n";
       reg[1]=a;reg[2]=b;reg[3]=c;reg[4]=d;
// for(int i=1;i<=4;i++)
// {
// cout<<reg[i].x<<" "<<reg[i].y<<" "<<endl;
// }
       int q1=pointin(lst,reg);
       int q2=pointin(led,reg);
// cout<<q1<<" "<<q2<<endl;
       if(q1!=0||q2!=0) flag=0;
        if(flag) puts("F");
        else puts("T");
    }
    return 0;
}

全部评论

相关推荐

kyw_:接好运
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务