计算几何专项: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; }
相关文章推荐
- 计算几何专项:UVa 10439
- 计算几何专项:UVa 811
- 计算几何专项:UVa 10556
- 计算几何专项:UVa 10585
- 计算几何专项:UVa 10245
- 计算几何专项:UVa 10566
- 计算几何专项:UVa 11072
- 计算几何专项:UVa 10084
- 计算几何专项:UVa 10674
- 计算几何专项:UVa 12301
- 计算几何专项:UVa 11265
- 计算几何专项:UVa 12307
- 计算几何专项:UVa 11437
- 计算几何专项:UVa 11186
- 计算几何专项:UVa 11817
- 计算几何专项:UVa 754
- 计算几何专项:UVa 11524
- 计算几何专项:UVa 10522
- UVA 10652 Board Wrapping 计算几何
- UVa 10387 Billiard (计算几何&反射)