凸包

本文详细介绍了凸包的概念及其在计算机图形学中的重要性,重点讲解了Andrew算法和Graham扫描法这两种求解凸包的方法。通过排序和向量运算,实现从一组点中找到最小凸多边形覆盖所有点的过程。此外,还讨论了点与线段、直线的几何关系,以及在实际问题中如何应用这些算法。
摘要由CSDN通过智能技术生成

凸包:在一个实数向量空间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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值