计算几何专项:UVa 588

一道半平面交,用朴素的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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值