概念:
什么是半平面?半平面实际上就是一条有方向的直线,不过它还包括这条直线一侧(具体哪侧自行设置)的全部区域。那半平面交的概念也呼之欲出了,就是这些半平面覆盖区域的交集。
作用:
1. 求多边形的核。(能看到所有点的位置)
2. 求凸多边形最大内切圆。(二分+边推进)
3. 解决线性规划问题。(矩形切割面积)
注意事项:
1. 该模板中半平面的直线方向逆时针区域为半平面覆盖区域。
2. 半平面交的区域一定是个凸多边形。
3. 在求半平面交的凸多边形前要调用halfplaneinsert函数判断半平面交是否存在。
4. halfplaneinsert返回false还可能是因为半平面交不封闭。
附模板:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
const double eps = 1e-8;
const double inf = 1e20;
const double pi = acos(-1.0);
const int maxp = 1000;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct Point
{
double x, y;
Point(){}
Point(double _x,double _y){x = _x, y = _y;}
void input(){scanf("%lf%lf",&x,&y);}
void output(){printf("%.2f %.2f\n",x,y);}
bool operator == (Point b)const{return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;}
bool operator < (Point b)const{return sgn(x-b.x)== 0?sgn(y-b.y)<0:x<b.x;}
Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}
//叉积
double operator ^(const Point &b)const{return x*b.y - y*b.x;}
//点积
double operator *(const Point &b)const{return x*b.x + y*b.y;}
//返回长度
double len(){return hypot(x,y);/*库函数*/}
//返回长度的平方
double len2(){return x*x + y*y;}
//返回两点的距离
double distance(Point p){return hypot(x-p.x,y-p.y);}
Point operator +(const Point &b)const{return Point(x+b.x,y+b.y);}
Point operator *(const double &k)const{return Point(x*k,y*k);}
Point operator /(const double &k)const{return Point(x/k,y/k);}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e){s = _s, e = _e;}
bool operator ==(Line v){return (s == v.s)&&(e == v.e);}
//`根据一个点和倾斜角angle确定直线,0<=angle<pi`
Line(Point p,double angle)
{
s = p;
if(sgn(angle-pi/2) == 0){e = (s + Point(0,1));}
else{e = (s + Point(1,tan(angle)));}
}
void input()
{
s.input();
e.input();
}
bool parallel(Line v){return sgn((e-s)^(v.e-v.s)) == 0;/*两向量叉积为0*/ }
Point crosspoint(Line v)
{
double a1 = (v.e-v.s)^(s-v.s);
double a2 = (v.e-v.s)^(e-v.s);
return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));
}
};
struct halfplane:public Line
{
double angle;
halfplane(){}
//`表示向量s->e逆时针(左侧)的半平面`
halfplane(Point _s,Point _e)
{
s = _s;
e = _e;
}
halfplane(Line v)
{
s = v.s;
e = v.e;
}
void calcangle(){angle = atan2(e.y-s.y,e.x-s.x);}
bool operator <(const halfplane &b)const{return angle < b.angle;}
};
struct polygon
{
int n;
Point p[maxp];
Line l[maxp];
double getarea()
{
double sum = 0;
for(int i = 0;i < n;i++){
sum += (p[i]^p[(i+1)%n]);
}
return fabs(sum)/2;
}
};
struct halfplanes
{
int n;//需要输入
halfplane hp[maxp];//需要输入,且封闭区域都在向量逆时针方向
Point p[maxp];
int que[maxp];
int st,ed;//队列的头尾指针,且下标从0开始,指向元素就是头和尾
void push(halfplane tmp){hp[n++] = tmp;}
//去重
void unique()
{
int m = 1;
for(int i = 1;i < n;i++)
{
if(sgn(hp[i].angle-hp[i-1].angle) != 0)
hp[m++] = hp[i];
//去除极角相同的情况下,位置在右边(沿向量方向)的边
else if(sgn( (hp[m-1].e-hp[m-1].s)^(hp[i].s-hp[m-1].s) ) > 0)
hp[m-1] = hp[i];
}
n = m;
}
bool halfplaneinsert()//判断半平面交是否存在
{
for(int i = 0;i < n;i++)hp[i].calcangle();//计算每条边的倾斜角
sort(hp,hp+n);//先对倾斜角排序
unique();
que[st=0] = 0;
que[ed=1] = 1;
p[1] = hp[0].crosspoint(hp[1]);
for(int i = 2;i < n;i++){
while(st<ed && sgn((hp[i].e-hp[i].s)^(p[ed]-hp[i].s))<0)ed--;
while(st<ed && sgn((hp[i].e-hp[i].s)^(p[st+1]-hp[i].s))<0)st++;
que[++ed] = i;
if(hp[i].parallel(hp[que[ed-1]]))return false;
p[ed]=hp[i].crosspoint(hp[que[ed-1]]);
}
while(st<ed && sgn((hp[que[st]].e-hp[que[st]].s)^(p[ed]-hp[que[st]].s))<0)ed--;
while(st<ed && sgn((hp[que[ed]].e-hp[que[ed]].s)^(p[st+1]-hp[que[ed]].s))<0)st++;
if(st+1>=ed)return false;//最后剩下小于三条直线,表明半平面交不存在
return true;
}
void getconvex(polygon &con)
{
p[st] = hp[que[st]].crosspoint(hp[que[ed]]);
con.n = ed-st+1;
for(int j = st,i = 0;j <= ed;i++,j++)
con.p[i] = p[j];
}
};