您的位置:首页 > 其它

计算几何专项:UVa 588

2013-06-02 12:36 507 查看
一道半平面交,用朴素的O(n^2)方法就可以过,需要注意平面退化成点或者线段的情况,在这样的情况下,仍然是possible的。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std;
const double eps=1e-6;
const double inf=1e20;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
struct point
{
double x,y;
point(double x=0,double y=0):x(x),y(y){}
};
point operator-(point a,point b){return point(a.x-b.x,a.y-b.y);}
point operator+(point a,point b){return point(a.x+b.x,a.y+b.y);}
point operator*(point a,double p){return point(a.x*p,a.y*p);}
double cross(point a,point b){return a.x*b.y-a.y*b.x;}
double dot(point a,point b){return a.x*b.x+a.y*b.y;}
bool onseg(point p,point a1,point a2)
{
return dcmp(cross(a1-p,a2-p))==0&&dcmp(dot(a1-p,a2-p))<0;
}
point getinter(point p,point v,point q,point w)
{
point u=p-q;
double t=cross(w,u)/cross(v,w);
return p+v*t;
}
vector<point> cut(vector<point> poly,point A,point B)
{
vector<point> newpoly;
int n=poly.size();
for(int i=0;i<n;i++)
{
point C=poly[i];
point D=poly[(i+1)%n];
if(dcmp(cross(B-A,C-A))>=0) newpoly.push_back(C);
if(dcmp(cross(B-A,C-D))!=0)
{
point ip=getinter(A,B-A,C,D-C);
if(onseg(ip,C,D)) newpoly.push_back(ip);
}
}
return newpoly;
}
int n;
int main()
{
int kase=1;
while(cin>>n&&n)
{
vector<point> p,t;
for(int i=0;i<n;i++)
{
double x,y;
cin>>x>>y;
p.push_back(point(x,y));
t.push_back(point(x,y));
}
for(int i=0;i<n;i++)
{
p=cut(p,t[(i+1)%n],t[i]);
}
cout<<"Floor #"<<kase++<<endl;
if(p.size()>0) cout<<"Surveillance is possible."<<endl;
else cout<<"Surveillance is impossible."<<endl;
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: