zsbenn

My Links

Blog Stats

【模板】多边形的核(非严格的半平面角)——poj3335

这个模板用来判断多边形是否存在核(一点也行)

有个地方需要修改的 struct line{}里面的一个函数

   int include(point k){return sign(cross(p[1]-p[0],k-p[0]))>=0;}//k在l左端 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
#define N 205

typedef double db;
const db eps=1e-6;
const db pi=acos(-1.0);
int sign(db k){
    if (k>eps) return 1; else if (k<-eps) return -1; return 0;
}
int cmp(db k1,db k2){return sign(k1-k2);}
int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内 
struct point{
    db x,y;
    point(){}
    point(db x,db y):x(x),y(y){}
    point operator + (const point &k1) const{return point(k1.x+x,k1.y+y);}
    point operator - (const point &k1) const{return point(x-k1.x,y-k1.y);}
    point operator * (db k1) const{return point(x*k1,y*k1);}
    point operator / (db k1) const{return point(x/k1,y/k1);}
    int getP() const{return sign(y)==1||(sign(y)==0&&sign(x)>=0);}
};
int inmid(point k1,point k2,point k3){return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);}
db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;}
db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;}
int comp(point k1,point k2){
    if(k1.getP()==k2.getP())return sign(cross(k1,k2))>0;
    return k1.getP()<k2.getP();
}

struct line{
    point p[2];
    line(point k1,point k2){p[0]=k1; p[1]=k2;}
    point& operator [] (int k){return p[k];}
    int include(point k){return sign(cross(p[1]-p[0],k-p[0]))>=0;}//k在l左端 
    point dir(){return p[1]-p[0];}
};

//输入的点是顺时针:ans<0,逆时针:ans>0 
bool judge(vector<point> v){
    double ans=0;
    for(int i=1;i<v.size()-1;i++)
        ans+=cross(v[i]-v[0],v[i+1]-v[0]);
    return ans>0;
}
point getLL(point k1,point k2,point k3,point k4){
    db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3); return (k1*w2+k2*w1)/(w1+w2);
}
point getLL(line k1,line k2){return getLL(k1[0],k1[1],k2[0],k2[1]);}
int parallel(line k1,line k2){return sign(cross(k1.dir(),k2.dir()))==0;}
int sameDir(line k1,line k2){return parallel(k1,k2)&&sign(dot(k1.dir(),k2.dir()))==1;}
int checkpos(line k1,line k2,line k3){return k3.include(getLL(k1,k2));}//k1,k2交点在 k3 左端 
int operator<(line k1,line k2){//按极角排序,角度相同的从左到右排 
    if(sameDir(k1,k2))return k2.include(k1[0]);
    return comp(k1.dir(),k2.dir());
} 
int getHL(vector<line> &L){ // 求半平面交 , 半平面是逆时针方向 , 输出按照逆时针
    sort(L.begin(),L.end()); deque<line> q;
    for (int i=0;i<(int)L.size();i++){
        //cout<<L[i][0].x<<" "<<L[i][0].y<<" "<<L[i][1].x<<" "<<L[i][1].y<<'\n';
        if (i&&sameDir(L[i],L[i-1])) continue;
        while (q.size()>1&&!checkpos(q[q.size()-2],q[q.size()-1],L[i])) q.pop_back();
        while (q.size()>1&&!checkpos(q[1],q[0],L[i])) q.pop_front();
        q.push_back(L[i]);
    }
    while (q.size()>2&&!checkpos(q[q.size()-2],q[q.size()-1],q[0])) q.pop_back();
    while (q.size()>2&&!checkpos(q[1],q[0],q[q.size()-1])) q.pop_front();
    vector<line>ans; for (int i=0;i<q.size();i++) ans.push_back(q[i]);
    if(q.size()<3)return 0;
    return 1;
}
vector<point>v;
vector<line>L;
int n;

int main(){
    int t;cin>>t;
    while(t--){
        v.clear();L.clear();
        
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            point t;
            scanf("%lf%lf",&t.x,&t.y);
            v.push_back(t);
        }
    
        if(judge(v)){//点逆时针 
            for(int i=0;i<n;i++)
                L.push_back(line(v[i],v[(i+1)%n]));
        }else {//点顺时针 
            for(int i=0;i<n;i++)
                L.push_back(line(v[(i+1)%n],v[i]));
        }
        
        int res = getHL(L);
        if(!res)puts("NO");
        else puts("YES"); 
    }
}

 

posted on 2020-02-27 20:48  zsben  阅读(...)  评论(...编辑  收藏