poj1279

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

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

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

//板子题到此为止了

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <deque> using namespace std;
typedef double db;
const db eps=1e-;
const db pi=acos(-);
int sign(db k){
if (k>eps) return ; else if (k<-eps) return -; return ;
}
int cmp(db k1,db k2){return sign(k1-k2);}
struct point{
db x,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};}
db getP()const { return sign(y)==||(sign(y)==&&sign(x)==-);}
};
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;}
db rad(point k1,point k2){ return atan2(cross(k1,k2),dot(k1,k2));}
int compareangle(point k1,point k2){
return k1.getP()<k2.getP()||(k1.getP()==k2.getP()&&sign(cross(k1,k2))>);
}
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);
}
struct line{
point p[];
line(point k1,point k2){p[]=k1;p[]=k2;}
point &operator[](int k){ return p[k];}
int include(point k){ return sign(cross(p[]-p[],k-p[])>);}
point dir(){ return p[]-p[];}
};
point getLL(line k1,line k2){
return getLL(k1[],k1[],k2[],k2[]);
}
int parallel(line k1,line k2){ return sign(cross(k1.dir(),k2.dir()))==;}
int sameDir(line k1,line k2){
return parallel(k1,k2)&&sign(dot(k1.dir(),k2.dir()))==;
}
int operator <(line k1,line k2){
if(sameDir(k1,k2))return k2.include(k1[]);
return compareangle(k1.dir(),k2.dir());
}
int checkpos(line k1,line k2,line k3){ return k3.include(getLL(k1,k2));}
vector<line> getHL(vector<line> &L){
sort(L.begin(),L.end());deque<line> q;
for(int i=;i<L.size();i++){
if(i&&sameDir(L[i],L[i-]))continue;
while (q.size()>&&!checkpos(q[q.size()-],q[q.size()-],L[i]))q.pop_back();
while (q.size()>&&!checkpos(q[],q[],L[i]))q.pop_front();
q.push_back(L[i]);
}
while (q.size()>&&!checkpos(q[q.size()-],q[q.size()-],q[]))q.pop_back();
while (q.size()>&&!checkpos(q[],q[],q[q.size()-]))q.pop_front();
vector<line> ans;for(int i=;i<q.size();i++)ans.push_back(q[i]);
return ans;
}
int t,n;
point p[];
bool cw(){//时针
db s=;
for(int i=;i<n-;i++){
s+=cross(p[i]-p[],p[i+]-p[]);
}
return s>;
}
vector<line>l;
int main(){
scanf("%d",&t);
while (t--){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
if(!cw())reverse(p,p+n);
for(int i=;i<n;i++){
l.push_back(line(p[i],p[(i+)%n]));
}
l=getHL(l);
if(l.size()<){
printf("0.00\n");
} else{
vector<point> a;
for(int i=;i<l.size();i++){
a.push_back(getLL(l[i],l[(i+)%l.size()]));
}
db ans = ;
for(int i=;i<a.size()-;i++)
ans+=cross(a[i]-a[],a[i+]-a[]);
ans/=;
printf("%.2f\n",ans);
}
l.clear();
}
}
上一篇:MyBatis - 6.Spring整合MyBatis


下一篇:认识CSS中字体图标