poj3335 判断半平面交是否有解集

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liufengwei1/article/details/87477112

跟poj1474一个意思

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#define maxl 20010
#define eps 1e-8
const double inf=1e18;
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);
	}
	friend point operator * (const double &t,const point &a)
	{
		return point(t*a.x,t*a.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 halfplane
{// s->e on the left
	point s,e;
	double k;
	halfplane(point a=point(),point b=point())
	{
		s=a;e=b;
		k=atan2(e.y-s.y,e.x-s.x);
	}
	point operator &(const halfplane &b)const
    {
        double t=det(b.s-s,b.e-b.s);
		t=t/det(e-s,b.e-b.s);
		return s+t*(e-s);
    }
};
inline bool satisfy(point a,const halfplane L)
{//不允许在线上 
	return sgn(det(a-L.s,L.e-L.s))<=0;
}
inline bool HPIcmp(const halfplane &a,const halfplane &b)
{//如果平行且同向,内侧的在前 
	int res=sgn(a.k-b.k);
	return res==0 ? satisfy(a.s,b) : a.k<b.k;
}
halfplane Q[maxl];
void HPI(halfplane line[],int n,point res[],int &resn)
{
	int tot=n;
	sort(line,line+n,HPIcmp);
	tot=1;
	for(int i=1;i<n;i++)
	if(sgn(line[i].k-line[i-1].k)!=0)
		line[tot++]=line[i]; 
	int head=0,tail=1;
	Q[0]=line[0];
	Q[1]=line[1];
	resn=0;
	for(int i=2;i<tot;i++)
	{
		if(sgn(det(Q[tail].e-Q[tail].s,Q[tail-1].e-Q[tail-1].s))==0||
		   sgn(det(Q[head].e-Q[head].s,Q[head+1].e-Q[head+1].s))==0)
		   	return; 
		while(head<tail && !satisfy(Q[tail]&Q[tail-1],line[i]))
			tail--;
		while(head<tail && !satisfy(Q[head]&Q[head+1],line[i]))
			head++;
		Q[++tail]=line[i];
	}
	while(head<tail && !satisfy(Q[tail]&Q[tail-1],Q[head]))
		tail--;
	while(head<tail && !satisfy(Q[head]&Q[head+1],Q[tail]))
		head++;
	if(tail<=head+1) return;
	for(int i=head;i<tail;i++)
		res[resn++]=Q[i]&Q[i+1];
	if(head<tail-1)
		res[resn++]=Q[head]&Q[tail];
}

int n,cas,resn;
halfplane line[maxl];
point p[maxl],res[maxl];
double ans;


inline void prework()
{
	for(int i=0;i<n;i++)
	{
		scanf("%lf%lf",&p[i].x,&p[i].y);
		if(i)
			line[i]=halfplane(p[i],p[i-1]);
	}
	line[0]=halfplane(p[0],p[n-1]);
	line[n++]=(halfplane(point(-inf,-inf),point(inf,-inf)));
	line[n++]=(halfplane(point(inf,-inf),point(inf,inf)));
	line[n++]=(halfplane(point(inf,inf),point(-inf,inf)));
	line[n++]=(halfplane(point(-inf,inf),point(-inf,-inf)));
}

inline void mainwork()
{
	HPI(line,n,res,resn);
}

inline void print()
{
	++cas;
//	printf("Floor #%d\n",cas);
	if(resn==0) 
		puts("NO");
	else
		puts("YES");
}

int main()
{
	int t;
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		scanf("%d",&n);
		prework();
		mainwork();
		print();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/liufengwei1/article/details/87477112