版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liufengwei1/article/details/87562094
我真的佛了,竟然还有0<=n<=1e5的情况,导致我一直RE,因为处理斜率的时候要特殊处理w[n-1],所以n=0时,w[n-1]会数组越界导致RE。。。。计算几何竟然还有给0个点的情况,真是有毒,dicuss里只有人提醒n=1的情况。。。。
这里需要改变一下凸包的点的先后顺序,优先y坐标小的在前,这样就可以根据atan2的值二分了,由于是逆时针的凸包,第一个点一定是y坐标最小的中间x坐标最小的,那么1号点到2号点的atan2一定大于0,>0边构成右凸包,<0的构成左凸包,由于atan2的取值范围为(-pi,pi],从右凸包到左凸包的atan2值有一个梯度下滑,所以只要加上一个2*pi,就可以是逆时针递增的顺序了。
inline bool cmp(const point &a,const point &b)
{
if(sgn(a.y-b.y)==0)
return sgn(a.x-b.x)<0;
return sgn(a.y-b.y)<0;
}
然后对于s->e的线,二分找到第一个比他大的atan2值,根据下标得出那个店,e->s同样得出一个点,如果有与凸包相交的话,这两个点一定在这两条线的两边。
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#define maxl 100010
#define eps 1e-8
const double pi=acos(-1.0);
using namespace std;
inline int sgn(double x)
{
if(x>-eps && x<eps) return 0;
if(x>0) return 1;
else return -1;
}
struct point
{
double x,y;
point(double a=0,double b=0)
{
x=a;y=b;
}
point operator + (const point &b)const
{
return point(x+b.x,y+b.y);
}
point operator - (const point &b)const
{
return point(x-b.x,y-b.y);
}
inline bool operator == (const point &b)const
{
return sgn(b.x-x)==0 && sgn(b.y-y)==0;
}
friend point operator *(const double t,const point &p)
{
return point(t*p.x,t*p.y);
}
inline double norm()
{
return sqrt(x*x+y*y);
}
};
inline double dot(const point &a,const point &b)
{
return a.x*b.x+a.y*b.y;
}
inline double det(const point &a,const point &b)
{
return a.x*b.y-a.y*b.x;
}
struct polygon_convex
{
vector<point> P;
polygon_convex(int size=0)
{
P.resize(size);
}
};
inline bool cmp(const point &a,const point &b)
{
if(sgn(a.y-b.y)==0)
return sgn(a.x-b.x)<0;
return sgn(a.y-b.y)<0;
}
polygon_convex convex_hull(vector<point> a)
{
polygon_convex res(2*a.size()+5);
sort(a.begin(),a.end(),cmp);
a.erase(unique(a.begin(),a.end()),a.end());
int m=0,l=a.size();
for(int i=0;i<l;i++)
{
while(m>1 && sgn(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)
--m;
res.P[m++]=a[i];
}
int k=m;//l-1号点一定在其中,从l-2开始
for(int i=l-2;i>=0;i--)
{
while(m>k && sgn(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)
--m;
res.P[m++]=a[i];
}
res.P.resize(m);
if(a.size()>1)
res.P.resize(m-1);
return res;
}
int n;
vector<point> a;
polygon_convex hull;
double w[maxl];
inline void prework()
{
scanf("%d",&n);
a.resize(n);
for(int i=0;i<n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
}
inline double getA(const point &a)
{
double res=atan2(a.y,a.x);
if(res<0)
res+=2*pi;
return res;
}
inline int find(double x)
{
if(x<w[0]+eps || x>w[n-1]-eps) return 0;
return (upper_bound(w,w+n,x)-w);
}
inline void mainwork()
{
hull=convex_hull(a);
n=hull.P.size();
for(int i=0;i<n-1;i++)
w[i]=getA(hull.P[i+1]-hull.P[i]);
if(n>0)
w[n-1]=getA(hull.P[0]-hull.P[n-1]);
point s,e;
while(~scanf("%lf%lf%lf%lf",&s.x,&s.y,&e.x,&e.y))
{
if(n==0)
{
puts("GOOD");
continue;
}
int i=find(getA(e-s));
int j=find(getA(s-e));
if(sgn(det(e-s,hull.P[i]-s)*det(e-s,hull.P[j]-s))<0)
puts("BAD");
else
puts("GOOD");
}
}
inline void print(){}
int main()
{
prework();
mainwork();
print();
return 0;
}