bzoj 2618: [Cqoi2006]凸多边形 半平面交
题目大意:
逆时针给出n个凸多边形的顶点坐标,求它们交的面积
题解:
对于所有的边我们都当作只有这条边所在的直线的左侧的区域才有可能选中
然后我们求所有的边所在的直线的左半平面的交
所以直接做裸半平面交即可
第一次做半平面交,抄的模板.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 512;
const double eps = 1e-9;
inline int dcmp(const double &x){
if(x < eps && x > -eps) return 0;
return x > 0 ? 1 : -1;
}
struct Point{
double x,y;
Point(const double &a=0,const double &b=0){
x = a;y = b;
}
};
typedef Point Vector;
inline Vector operator + (const Vector &a,const Vector &b){
return Vector(a.x+b.x,a.y+b.y);
}
inline Vector operator - (const Vector &a,const Vector &b){
return Vector(a.x-b.x,a.y-b.y);
}
inline double cross(const Vector &a,const Vector &b){
return a.x*b.y - a.y*b.x;
}
inline Vector operator * (const Vector &a,const double &b){
return Vector(a.x*b,a.y*b);
}
struct line{
Point p;
Vector v;
double alpha;
line(){}
line(const Point &p1,const Point &p2){
p = p1;v = p2 - p1;
alpha = atan2(v.y,v.x);
}
};
inline bool cmp(const line &a,const line &b){
return a.alpha < b.alpha;
}
inline Point lineInterion(const line &P,const line &Q){
Vector u = P.p - Q.p;
double t = cross(Q.v,u)/cross(P.v,Q.v);
return P.p + P.v*t;
}
inline bool onLeft(const Point &p,const line &l){
return dcmp(cross(p-l.p,l.v)) >= 0;
}
int cnt = 0,n = 0;
Point p[maxn];line lines[maxn];
line q[maxn];int l,r;
inline void halfInterion(){
sort(lines+1,lines+n+1,cmp);l = 0;r = -1;
for(int i=1;i<=n;++i){
while(r-l >= 1 && !onLeft(lineInterion(q[r],q[r-1]),lines[i])) --r;
if(r-l >= 0 && dcmp(cross(q[r].v,lines[i].v)) == 0)
q[r] = onLeft(q[r].p,lines[i]) ? q[r] : lines[i];
else q[++r] = lines[i];
}
while(1){
if(r-l >= 1 && !onLeft(lineInterion(q[l],q[l+1]),q[r])) ++ l;
else if(r-l >= 1 && !onLeft(lineInterion(q[r],q[r-1]),q[l])) -- r;
else break;
}
}
int main(){
read(n);
for(int i=1,m;i<=n;++i){
read(m);
for(int j=1;j<=m;++j){
scanf("%lf%lf",&p[j].x,&p[j].y);
if(j != 1) lines[++cnt] = line(p[j],p[j-1]);
}lines[++cnt] = line(p[1],p[m]);
}
swap(n,cnt);halfInterion();
if(r-l <= 1) return puts("0.000");
cnt = 0;
for(int i=l+1;i<=r;++i){
p[++cnt] = lineInterion(q[i],q[i-1]);
}p[++cnt] = lineInterion(q[r],q[l]);
double ans = .0;
for(int i=2;i<=cnt;++i) ans += cross(p[i],p[i-1]);
ans += cross(p[1],p[cnt]);
printf("%.3lf\n",abs(ans)/2.0);
getchar();getchar();
return 0;
}
人就像命运下的蝼蚁,谁也无法操控自己的人生.