c++凸包算法

#include
#include
#include
#include

struct Point {
int x, y;
bool operator<(const Point& p) const {
return x < p.x || (x == p.x && y < p.y);
}
};

// 计算两个点之间的斜率
int orientation(const Point &p, const Point &q, const Point &r) {
int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // 共线
return (val > 0) ? 1 : 2; // 逆时针或顺时针
}

// 按照极角排序,如果极角相同,则按照距离排序
bool compare(const Point &p1, const Point &p2) {
int o = orientation(p0, p1, p2);
if (o == 0) {
return (p0.x - p1.x) * (p0.x - p1.x) + (p0.y - p1.y) * (p0.y - p1.y) <
(p0.x - p2.x) * (p0.x - p2.x) + (p0.y - p2.y) * (p0.y - p2.y);
}
return (o == 2);
}

std::vector convexHull(std::vector &points) {
// 找到最左下方的点
Point p0 = *std::min_element(points.begin(), points.end());

// 根据极角排序
std::sort(points.begin(), points.end(), compare);

// 创建一个栈来存储凸包的顶点
std::stack<Point> hull;
hull.push(points[0]);
hull.push(points[1]);

for (int i = 2; i < points.size(); i++) {
    while (hull.size() > 1 && orientation(hull.top(), hull.second_from_top(), points[i]) != 2) {
        hull.pop();
    }
    hull.push(points[i]);
}

// 将栈转换为向量
std::vector<Point> convexHull;
while (!hull.empty()) {
    convexHull.push_back(hull.top());
    hull.pop();
}

return convexHull;

}

int main() {
std::vector points = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 0}};
std::vector hull = convexHull(points);

std::cout << "The points in the convex hull are:" << std::endl;
for (const auto& point : hull) {
    std::cout << "(" << point.x << ", " << point.y << ")" << std::endl;
}

return 0;

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++凸包算法实现代码,使用的是Graham Scan算法: ```c++ #include <iostream> #include <stack> #include <algorithm> using namespace std; struct Point { int x, y; }; // 求出两点之间的距离平方 int dist_square(Point p1, Point p2) { int x = p1.x - p2.x; int y = p1.y - p2.y; return x * x + y * y; } // 求出叉积 int cross_product(Point a, Point b, Point c) { int x1 = b.x - a.x; int y1 = b.y - a.y; int x2 = c.x - b.x; int y2 = c.y - b.y; return x1 * y2 - x2 * y1; } // 比较两点的极角大小 bool cmp(Point a, Point b) { int cp = cross_product(p0, a, b); if (cp > 0) return true; if (cp == 0 && dist_square(p0, a) < dist_square(p0, b)) return true; return false; } // Graham Scan算法 void convex_hull(Point points[], int n) { // 找出y坐标最小的点 int ymin = points[0].y, min_idx = 0; for (int i = 1; i < n; i++) { int y = points[i].y; if ((y < ymin) || (ymin == y && points[i].x < points[min_idx].x)) { ymin = y; min_idx = i; } } // 将y坐标最小的点放到首位 swap(points[0], points[min_idx]); p0 = points[0]; // 按极角从小到大排序 sort(points + 1, points + n, cmp); // 构建凸包 stack<Point> hull; hull.push(points[0]); hull.push(points[1]); for (int i = 2; i < n; i++) { Point top = hull.top(); hull.pop(); while (hull.size() && cross_product(hull.top(), top, points[i]) <= 0) { top = hull.top(); hull.pop(); } hull.push(top); hull.push(points[i]); } // 输出凸包顶点 while (!hull.empty()) { Point p = hull.top(); cout << "(" << p.x << ", " << p.y << ")" << endl; hull.pop(); } } int main() { Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}}; int n = sizeof(points) / sizeof(points[0]); convex_hull(points, n); return 0; } ``` 其中,`Point`结构体表示二维平面上的一个点,`dist_square`函数用于求出两点之间的距离平方,`cross_product`函数用于求出三点构成的向量的叉积,`cmp`函数用于排序,`convex_hull`函数实现了Graham Scan算法,`main`函数用于测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值