poj1279

Posted mxang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj1279相关的知识,希望对你有一定的参考价值。

板子题,求多边形内核面积。

话说jls的板子返回的是边,然后我就在冥思苦想怎么根据割边求面积啊。。

然后发现自己果然是个傻逼,求一下交点存起来就好了。。。

//板子题到此为止了

技术图片
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <vector>
  5 #include <cmath>
  6 #include <deque>
  7 
  8 using namespace std;
  9 typedef double db;
 10 const db eps=1e-6;
 11 const db pi=acos(-1);
 12 int sign(db k){
 13     if (k>eps) return 1; else if (k<-eps) return -1; return 0;
 14 }
 15 int cmp(db k1,db k2){return sign(k1-k2);}
 16 struct point{
 17     db x,y;
 18     point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
 19     point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
 20     point operator * (db k1) const{return (point){x*k1,y*k1};}
 21     point operator / (db k1) const{return (point){x/k1,y/k1};}
 22     db getP()const { return sign(y)==1||(sign(y)==0&&sign(x)==-1);}
 23 };
 24 db cross(point k1,point k2){ return k1.x*k2.y-k1.y*k2.x;}
 25 db dot(point k1,point k2){ return k1.x*k2.x+k1.y*k2.y;}
 26 db rad(point k1,point k2){ return atan2(cross(k1,k2),dot(k1,k2));}
 27 int compareangle(point k1,point k2){
 28     return k1.getP()<k2.getP()||(k1.getP()==k2.getP()&&sign(cross(k1,k2))>0);
 29 }
 30 point getLL(point k1,point k2,point k3,point k4){
 31     db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3);
 32     return (k1*w2+k2*w1)/(w1+w2);
 33 }
 34 struct line{
 35     point p[2];
 36     line(point k1,point k2){p[0]=k1;p[1]=k2;}
 37     point &operator[](int k){ return p[k];}
 38     int include(point k){ return sign(cross(p[1]-p[0],k-p[0])>0);}
 39     point dir(){ return p[1]-p[0];}
 40 };
 41 point getLL(line k1,line k2){
 42     return getLL(k1[0],k1[1],k2[0],k2[1]);
 43 }
 44 int parallel(line k1,line k2){ return sign(cross(k1.dir(),k2.dir()))==0;}
 45 int sameDir(line k1,line k2){
 46     return parallel(k1,k2)&&sign(dot(k1.dir(),k2.dir()))==1;
 47 }
 48 int operator <(line k1,line k2){
 49     if(sameDir(k1,k2))return k2.include(k1[0]);
 50     return compareangle(k1.dir(),k2.dir());
 51 }
 52 int checkpos(line k1,line k2,line k3){ return k3.include(getLL(k1,k2));}
 53 vector<line> getHL(vector<line> &L){
 54     sort(L.begin(),L.end());deque<line> q;
 55     for(int i=0;i<L.size();i++){
 56         if(i&&sameDir(L[i],L[i-1]))continue;
 57         while (q.size()>1&&!checkpos(q[q.size()-2],q[q.size()-1],L[i]))q.pop_back();
 58         while (q.size()>1&&!checkpos(q[1],q[0],L[i]))q.pop_front();
 59         q.push_back(L[i]);
 60     }
 61     while (q.size()>2&&!checkpos(q[q.size()-2],q[q.size()-1],q[0]))q.pop_back();
 62     while (q.size()>2&&!checkpos(q[1],q[0],q[q.size()-1]))q.pop_front();
 63     vector<line> ans;for(int i=0;i<q.size();i++)ans.push_back(q[i]);
 64     return ans;
 65 }
 66 int t,n;
 67 point p[1551];
 68 bool cw(){//时针
 69     db s=0;
 70     for(int i=1;i<n-1;i++){
 71         s+=cross(p[i]-p[0],p[i+1]-p[0]);
 72     }
 73     return s>0;
 74 }
 75 vector<line>l;
 76 int main(){
 77     scanf("%d",&t);
 78     while (t--){
 79         scanf("%d",&n);
 80         for(int i=0;i<n;i++){
 81             scanf("%lf%lf",&p[i].x,&p[i].y);
 82         }
 83         if(!cw())reverse(p,p+n);
 84         for(int i=0;i<n;i++){
 85             l.push_back(line(p[i],p[(i+1)%n]));
 86         }
 87         l=getHL(l);
 88         if(l.size()<3){
 89             printf("0.00
");
 90         } else{
 91             vector<point> a;
 92             for(int i=0;i<l.size();i++){
 93                 a.push_back(getLL(l[i],l[(i+1)%l.size()]));
 94             }
 95             db ans = 0;
 96             for(int i=1;i<a.size()-1;i++)
 97                 ans+=cross(a[i]-a[0],a[i+1]-a[0]);
 98             ans/=2;
 99             printf("%.2f
",ans);
100         }
101         l.clear();
102     }
103 }
View Code

 

以上是关于poj1279的主要内容,如果未能解决你的问题,请参考以下文章

poj1279

POJ 1279 Art Gallery [半平面交]

二分+半平面交——poj1279

半平面交求面积——poj1279

POJ 1279 Art Gallery 半平面交 多边形的核

POJ2451 Uyuw's Concert (半平面交)