凸包:在一个实数向量空间V中,对于给定集合X,所有包含X的凸集的交集S被称为X的凸包。X的凸包可以用X内所有点(X1,…Xn)的凸组合来构造.
简单来说就是选几个点构成一个凸多边形,这个凸多边形能把所有点包含,这个凸多边形就是凸包。
那么如何求凸包呢?
我们可以考虑将所有点排序(按水平序排序,即以x坐标为第一关键字,y坐标为第二关键字),这样最左边的点一定在凸包上,这样我们从右到左遍历所有点维护上凸壳,在从左到右维护下凸壳,即可求得凸包,这是Andrew算法。
此外还有Craham扫描法,但是这种方法需要用到三角函数,精度损失大。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <utility>
#include <climits>
#include <cstring>
#include <vector>
#include <algorithm>
#define eps 1e-8
#define N_MAX 30005
using std::pair;
using std::vector;
int n;
int sign(double x){ //符号
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
return 1;
}
struct Point{
double x,y;
Point(double x = 0,double y = 0):x(x),y(y){}
Point operator+(Point a){
return Point(x + a.x,y + a.y);
}
Point operator-(Point a){
return Point(x - a.x,y - a.y);
}
Point operator*(double t){
return Point(x * t,y * t);
}
Point operator/(double t){
return Point(x / t,y / t);
}
bool operator==(Point a){
return x == a.x && y == a.y;
}
double lenth(){ //向量的模
return sqrt(x * x + y * y);
}
}point[N_MAX];
vector<Point> up,down;
bool cmp(Point p1,Point p2){
if(sign(p1.x - p2.x) == 0)return p1.y < p2.y;
return p1.x < p2.x;
}
typedef Point Vector;
struct Line{
Point a,b;
Line(){}
Line(Point a,Point b){
this->a = a;
this->b = b;
}
}line[N_MAX];
double cross(Vector A,Vector B){ // 向量外积
return A.x * B.y - B.x * A.y;
}
double dot(Vector A,Vector B){ //向量内积
return B.x * A.x + B.y * A.y;
}
bool isclock(Vector v1,Vector v2){ // 判断v2是否在v1的顺时针方向
if(cross(v1,v2) < 0)return true;
return false;
}
double GetAngle(Vector v1,Vector v2){ // 算两向量夹角
return acos(dot(v1,v2) / v1.lenth() / v2.lenth());
}
Vector rotate(Vector v,double angle){
return Vector(v.x * cos(angle) + v.y * sin(angle),-v.x * sin(angle) + v.y * cos(angle));
}
Point GetPointForLine(Line X,Line Y){ // 算直线交点
Vector v = X.a - X.b;
Vector w = Y.a - Y.b;
Vector u = X.a - Y.a;
double base = (cross(w,u)) / (cross(v,w));
return X.a + v * base;
}
double DistanceForLine(Point P,Line X){ // 算点到直线的距离
Vector vp = X.b - X.a,vq = P - X.a;
return fabs(cross(vp,vq))/vp.lenth();
}
double DistanceForSegment(Point P,Line X){ //点到线段的距离
if(X.a == X.b)return (P - X.a).lenth();
Vector v1 = X.b - X.a,v2 = P - X.a,v3 = P - X.b;
if(sign(dot(v1,v2)) < 0) return v2.lenth();
if(sign(dot(v1,v3)) > 0) return v3.lenth();
return DistanceForLine(P,X);
}
Point GetProjectionInLine(Point P,Line X){ //点到直线上的投影
Vector v = X.b - X.a;
return X.a + v * (dot(v,P - X.a) / dot(v,v));
}
bool PointOnSegment(Point P,Line X){ // 点是否在线段上
return sign(cross(P - X.a,P - X.b)) == 0 && sign(dot(P - X.a,P - X.b)) <= 0;
}
bool SegmentIntersection(Line X,Line Y){ //两线段是否相交
double c1 = cross(X.b - X.a,Y.a - Y.b),c2 = cross(X.b - X.a,Y.b - X.a);
double c3 = cross(Y.b - Y.a,X.b - Y.a),c4 = cross(Y.b - Y.a,X.a - Y.a);
return sign(c1) * sign(c2) <= 0 && sign(c3) * sign(c4) <= 0;
}
pair<Point,Point> PointWithCircleAndLine(Point o,Line X,double r){ // 求圆与直线的交点
Point o2 = o;
o2.x += X.a.y - X.b.y;
o2.y += X.b.x - X.a.x;
o2 = GetPointForLine(Line(o,o2),X);
double base = sqrt(r * r - (o2 - o).lenth() * (o2 - o).lenth());
Vector e = (X.b - X.a) / (X.b - X.a).lenth();
return std::make_pair(o2 - e * base,o2 + e * base);
}
int main(int argc,char *argv[]){
scanf("%d",&n);
for(int i = 1;i <= n;i++){
double x,y;
scanf("%lf %lf",&x,&y);
point[i] = Point(x,y);
}
//凸包 Andrew算法
std::sort(point + 1,point + n + 1,cmp);
up.push_back(point[1]);
up.push_back(point[2]);
for(int i = 3;i <= n;i++){
for(int j = up.size();j >= 2 && !isclock(up[j - 1] - up[j - 2],point[i] - up[j - 2]);j--){
up.pop_back();
}
up.push_back(point[i]);
}
down.push_back(point[n]);
down.push_back(point[n - 1]);
for(int i = n - 2;i >= 1;i--){
for(int j = down.size();j >= 2 && !isclock(down[j - 1] - down[j - 2],point[i] - down[j - 2]);j--){
down.pop_back();
}
down.push_back(point[i]);
}
for(int i = 1;i < up.size() - 1;i++)down.push_back(up[i]);
return 0;
}