poj 3130 How I Mathematician Wonder What You Are! 【半平面交】

求多边形的核,直接把所有边求半平面交判断有无即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=205;
const double eps=1e-6;
int n;
struct dian
{
	double x,y;
	dian(double X=0,double Y=0)
	{
		x=X,y=Y;
	}
	dian operator + (const dian &a)
	{
		return dian(x+a.x,y+a.y);
	}
	dian operator - (const dian &a)
	{
		return dian(x-a.x,y-a.y);
	}
	dian operator * (const double &a) const
	{
		return dian(x*a,y*a);
	}
	dian operator / (const double &a) const
	{
		return dian(x/a,y/a);
	}
}p[N];
struct bian
{
	dian s,v;
	bian(dian S=dian(),dian V=dian())
	{
		s=S,v=V;
	}
}l[N],s[N];
int read()
{
	int r=0,f=1;
	char p=getchar();
	while(p>'9'||p<'0')
	{
		if(p=='-')
			f=-1;
		p=getchar();
	}
	while(p>='0'&&p<='9')
	{
		r=r*10+p-48;
		p=getchar();
	}
	return r*f;
}
double cj(dian a,dian b)
{
	return a.x*b.y-a.y*b.x;
}
double mj(dian a,dian b,dian c)
{
	return cj(b-a,c-a)/2;
}
dian jd(bian x,bian y)
{
	return x.s+x.v*(cj(x.s-y.s,y.v)/cj(y.v,x.v));
}
bool px(bian a,bian b)
{
	return cj(a.v,b.v)==0;
}
bool bn(bian a,bian b)
{
	int ar=cj(a.v,b.v);
	return ar>0||(ar==0&&cj(a.v,b.s-a.s)>0);
}
bool dn(dian x,bian y)
{
	return cj(y.v,x-y.s)<=0;
}
bool cmp(const bian &x,const bian &y)
{
	if(x.v.y==0&&y.v.y==0)
		return x.v.x<y.v.x;
	if((x.v.y<=0)==(y.v.y<=0))
		return bn(x,y);
	return x.v.y<y.v.y;
}
double dis2(bian a)
{
	return sqrt(a.v.x*a.v.x+a.v.y*a.v.y);
}
bian yi(bian a,double r)
{
	return bian(dian(a.s.x-a.v.y*r/dis2(a),a.s.y+a.v.x*r/dis2(a)),a.v);
}
int main()
{
	while(scanf("%d",&n)&&n)
	{
		for(int i=1;i<=n;i++)
			p[i].x=read(),p[i].y=read();
		p[n+1]=p[1];
		for(int i=1;i<=n;i++)
			l[i]=bian(p[i],p[i+1]-p[i]);sort(l+1,l+1+n,cmp);
		int top=0;
		for(int i=1;i<=n;i++)
			if(i==1||!px(l[i],l[i-1]))
				l[++top]=l[i];
		n=top;
		int ll=1,rr=2;
		s[1]=l[1],s[2]=l[2];
		for(int i=3;i<=n;i++)
		{
			while(ll<rr&&dn(jd(s[rr],s[rr-1]),l[i]))
				rr--;
			while(ll<rr&&dn(jd(s[ll],s[ll+1]),l[i]))
				ll++;
			s[++rr]=l[i];
		}
		while(ll<rr&&dn(jd(s[rr],s[rr-1]),s[ll]))
			rr--;
		printf("%d\n",(rr-ll>1));
	}
	return 0;
}
posted @ 2018-03-06 22:02  lokiii  阅读(94)  评论(0编辑  收藏  举报