判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No。

训练指南上的分析:

1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交。如果相交(不一定是规范相交,有公共点就算相交),则无解

2.任取一个红点,判断是否在蓝凸包内。如果是,则无解。蓝点红凸包同理。

其中任何一个凸包退化成点或者线段时需要特判。

其实只需要把上面两个判断顺序颠倒一下,就可以不需要特判。

先判断点是否在凸包内,因为这个考虑了点在凸包边界上的情况,所以后面判凸包线段是否相交时直接用规范相交判断即可。

此时特殊情况包含在了上两种情况中,因此不需要特判。


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
using namespace std;
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
};
Point operator+(Point A,Point B)
{
    return Point(A.x+B.x,A.y+B.y);
}
Point operator-(Point A,Point B)
{
    return Point(A.x-B.x,A.y-B.y);
}
int dcmp(double x)
{


    if(fabs(x)<1e-10)return 0;
    else return x<0?-1:1;
}
bool operator < (const Point&a,const Point&b)
{
    return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
bool operator == (const Point&a,const Point&b)
{
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
double Cross(Point A,Point B)
{
    return A.x*B.y-A.y*B.x;
}
double Dot(Point A,Point B)
{
    return A.x*B.x+A.y*B.y;
}


bool OnSegment(Point p,Point a1,Point a2)
{
    return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
int isPointInPolygon(Point p,Point *poly,int n)
{
    int wn=0;
    for(int i=0;i<n;i++)
    {
        Point p1=poly[i],p2=poly[(i+1)%n];
        if(p==p1||p==p2||OnSegment(p,p1,p2))return -1;
        int k=dcmp(Cross(p2-p1,p-p1));
        int d1=dcmp(p1.y-p.y);
        int d2=dcmp(p2.y-p.y);
        if(k>0&&d1<=0&&d2>0)wn++;
        if(k<0&&d2<=0&&d1>0)wn--;
    }
    if(wn!=0)return 1;
    return 0;
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1);
    double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
bool checkConvexHullIntersection(Point *a,Point *b,int na,int nb)
{
    for(int i=0;i<na;i++)
        if(isPointInPolygon(a[i],b,nb))
          return true;
    for(int i=0;i<nb;i++)
        if(isPointInPolygon(b[i],a,na))
          return true;
    for(int i=0;i<na;i++)
        for(int j=0;j<nb;j++)
          if(SegmentProperIntersection(a[i],a[(i+1)%na],b[j],b[(j+1)%nb]))
            return true;
     return false;
}


int ConvexHull(Point *p,int n,Point *ch)
{
    sort(p,p+n);
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
        ch[m++]=p[i];
    }
    if(n>1)m--;
    return m;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(n==0&&m==0) break;
        Point red[510],chred[510];
        Point blue[510],chblue[510];
        for(int i=0;i<n;i++)
            cin>>red[i].x>>red[i].y;
        for(int i=0;i<m;i++)
            cin>>blue[i].x>>blue[i].y;
        int Ncnt=ConvexHull(red,n,chred);
        int Mcnt=ConvexHull(blue,m,chblue);
        if(checkConvexHullIntersection(chred,chblue,Ncnt,Mcnt))
            cout<<"No"<<endl;
        else
            cout<<"Yes"<<endl;
    }
    return 0;
}


posted on 2015-05-01 11:00  星斗万千  阅读(140)  评论(0编辑  收藏  举报