poj 1474 Video Surveillance - 求多边形有没有核

/*
poj 1474 Video Surveillance - 求多边形有没有核



*/
#include <stdio.h>
#include<math.h>
const double eps=1e-8;
const int N=103;
struct point
{
    double x,y;
}dian[N];
inline bool mo_ee(double x,double y)
{
	double ret=x-y;
	if(ret<0) ret=-ret;
	if(ret<eps) return 1;
	return 0;
}
inline bool mo_gg(double x,double y)  {   return x > y + eps;} // x > y
inline bool mo_ll(double x,double y)  {   return x < y - eps;} // x < y
inline bool mo_ge(double x,double y) {   return x > y - eps;} // x >= y
inline bool mo_le(double x,double y) {   return x < y + eps;} 	// x <= y
inline double mo_xmult(point p2,point p0,point p1)//p1在p2左返回负,在右边返回正
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

point mo_intersection(point u1,point u2,point v1,point v2)
{
    point ret=u1;
    double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
		/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
    ret.x+=(u2.x-u1.x)*t;
    ret.y+=(u2.y-u1.y)*t;
    return ret;
}
/////////////////////////

//切割法求半平面交
point mo_banjiao_jiao[N*2];
point mo_banjiao_jiao_temp[N*2];
void mo_banjiao_cut(point *ans,point qian,point hou,int &nofdian)
{
	int i,k;
	for(i=k=0;i<nofdian;++i)
	{
		double a,b;
		a=mo_xmult(hou,ans[i],qian);
		b=mo_xmult(hou,ans[(i+1)%nofdian],qian);
		if(mo_le(a,0))//顺时针就是<=0
		{
			mo_banjiao_jiao_temp[k++]=ans[i];
		}if(mo_ll(a*b,0))
		{
			mo_banjiao_jiao_temp[k++]=mo_intersection(qian,hou,ans[i],ans[(i+1)%nofdian]);
		}
	}
	for(i=0;i<k;++i)
	{
		ans[i]=mo_banjiao_jiao_temp[i];
	}
	nofdian=k;
}
int mo_banjiao(point *dian,int n)
{
	int i,nofdian;
	nofdian=n;
	for(i=0;i<n;++i)
	{
		mo_banjiao_jiao[i]=dian[i];
	}
	for(i=0;i<n;++i)//i从0开始
	{
		mo_banjiao_cut(mo_banjiao_jiao,dian[i],dian[(i+1)%n],nofdian);
		if(nofdian==0)
		{
			return nofdian;
		}
	}
	return nofdian;
}
/////////////////////////
int main()
{
    int t,i,n,iofcase=1;
    while(scanf("%d",&n),n)
    {
        
        for(i=0;i<n;++i)
        {
            scanf("%lf%lf",&dian[i].x,&dian[i].y);
        }
        int ret=mo_banjiao(dian,n);
        if(ret==0)
        {
			printf("Floor #%d\n",iofcase++);
            printf("Surveillance is impossible.\n\n");
        }else
        {
            printf("Floor #%d\n",iofcase++);
            printf("Surveillance is possible.\n\n");
        }
    }
    return 0;
}


posted @ 2013-08-09 23:42  jlins  阅读(223)  评论(0编辑  收藏  举报