码迷,mamicode.com
首页 > 其他好文 > 详细

poj1279

时间:2019-02-28 22:42:29      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:operator   gif   hide   0.00   割边   scan   event   bsp   --   

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

话说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\n");
 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\n",ans);
100         }
101         l.clear();
102     }
103 }
View Code

 

poj1279

标签:operator   gif   hide   0.00   割边   scan   event   bsp   --   

原文地址:https://www.cnblogs.com/MXang/p/10453657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!