题目大意:
给出n个点,m条直线,对于每条直线,问所有点是否都在该直线同一侧。
解题思路:
即是问n个点的凸包是否在直线同一侧。
若直线与凸包有交点,那直线与凸包的两个切点一定在直线两侧。
所以我们可以将凸包上每条直线的倾斜角求出,对于每条直线二分找到两个切点,判断两个切点是否在直线两侧即可。
具体细节见代码注释。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=100005;
struct point
{
double x,y;
point(){}
point(double _x,double _y):x(_x),y(_y){}
inline friend point operator - (const point &a,const point &b)
{return point(a.x-b.x,a.y-b.y);}
inline friend double operator * (const point &a,const point &b)
{return a.x*b.y-a.y*b.x;}
inline double dis(){return x*x+y*y;}
}p[N],st,ed;
int n,top;
struct node
{
double ang;point a;
inline friend bool operator < (const node &a,const node &b)
{return a.ang<b.ang;}
}q[N];
inline bool cmp(const point &a,const point &b)
{
double det=(a-p[1])*(b-p[1]);
if(det)return det>0;
return (a-p[1]).dis()<(b-p[1]).dis();
}
void graham()
{
int id=1;
for(int i=2;i<=n;i++)
if(p[i].x<p[id].x||p[i].x==p[id].x&&p[i].y<p[id].y)id=i;
swap(p[id],p[1]);
sort(p+2,p+n+1,cmp);
for(int i=1;i<=n;i++)
{
while(top>=2&&(p[top]-p[top-1])*(p[i]-p[top-1])<0)top--;
p[++top]=p[i];
}
n=top;p[n+1]=p[1];//求出凸包
for(int i=1;i<=n;i++)
q[i].ang=atan2((p[i+1]-p[i]).y,(p[i+1]-p[i]).x),q[i].a=p[i];
sort(q+1,q+n+1);//计算倾斜角并排序。
q[n+1].ang=q[1].ang+2*acos(-1.0),q[n+1].a=q[1].a;
//由于atan2(0,-1)=3.14,而不是-3.14,第一条直线本就是满足大于它的,所以将q[1]加360度再放入。
}
bool dif(const point &a,const point &b)
{
return ((st-a)*(ed-a))*((st-b)*(ed-b))<0;
}
bool check()
{
if(n<=1)return true;
node t;
t.ang=atan2((ed-st).y,(ed-st).x);//第一个切点
point a=q[lower_bound(q+1,q+n+1,t)-q].a;
t.ang=atan2((st-ed).y,(st-ed).x);//第二个切点
point b=q[lower_bound(q+1,q+n+1,t)-q].a;
return !dif(a,b);//判断是否在不同侧
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
if(n>1)graham();
while(scanf("%lf%lf%lf%lf",&st.x,&st.y,&ed.x,&ed.y)!=EOF)
check()?puts("GOOD"):puts("BAD");
return 0;
}