C++解决凸包问题

凸包类(参考网上方法)

#ifndef POINT_HPP
#define POINT_HPP


#include<iostream>
#include<cmath>
#include<stack>
using namespace std;


class mpoint{                       //class point(x, y)
public:
    double x;
    double y;
    mpoint(double xx = 0, double yy = 0){
        x = xx;
        y = yy;
    }

};

int get_miny_point_id(mpoint *points, int size){ //get the point with min_y
    int i, min_id = 0;
    double miny = 10000;
    for(i = 0; i < size; i++){
        if(points[i].y < miny){
            miny = points[i].y;
            min_id = i;
        }
    }
    return min_id;
}

void get_cos(mpoint *points, double *mcos, int id, int size){  //get point's cos
    int i;
    double coss;
    for(i = 0; i < size; i++){
        if(i == id){
            mcos[i] = 2;
        }
        else{
            coss = (points[i].x - points[id].x) / sqrt((points[i].x - points[id].x) * (points[i].x - points[id].x) + (points[i].y - points[id].y) * (points[i].y - points[id].y));
            mcos[i] = coss;
        }
    }
}

void sort_points(mpoint *points, double *mcos, int size){   //sort the points
    int i, j;
    double temp_cos;
    mpoint temp_point;
    for(i = 0; i < size; i++){
        for(j = 0; j < size - i - 1; j++){      //bubble sorting
            if(mcos[j] < mcos[j + 1]){
                temp_cos = mcos[j];
                mcos[j] = mcos[j + 1];
                mcos[j + 1] = temp_cos;

                temp_point = points[j];
                points[j] = points[j + 1];
                points[j + 1] = temp_point;
            }
        }
    }
}

int ccw(mpoint a, mpoint b, mpoint c){          //judge if it is couter-colockwise
    double area2 = (b.x-a.x) * (c.y-a.y) - (b.y-a.y) * (c.x-a.x);
    if (area2 < 0){
        return -1;          // clockwise
    }
    else{
        if (area2 > 0) return 1;    // counter-clockwise
        else return 0;              // collinear
    }

}

void get_outpoint(mpoint *points, int size){    //get points in stack
    int i, k;
    vector <mpoint>outpoint;
    outpoint.push_back(points[0]);
    outpoint.push_back(points[1]);
    i = 2;
    while(true){
        if(i == size){
            break;
        }
        if(ccw(outpoint[outpoint.size() - 2], outpoint[outpoint.size() - 1], points[i]) > 0){
            outpoint.push_back(points[i]);
            i = i + 1;
        }
        else{
            outpoint.pop_back();
        }
    }
    cout << "The outpoints are: " << endl;
    for(k = 0; k < outpoint.size(); k++){
        cout << outpoint[k].x << " " << outpoint[k].y << endl;
    }
}
#endif // POINT_HPP

测试

vector<vector<int>>pointss;
fstream file;
string x, y,file_name;
file_name = "point.txt";
file.open(file_name.c_str(), ios::in);//打开文件
if (!file) {//如果打开不成功
    cout << "the file name is incorrect. please confirm the file name!" << endl;
}
else {
    while (!file.eof()) {
        file >> x >> y;
        pointss.push_back({atoi(x.c_str()),atoi(y.c_str())});
    }
}

for(int i = 0;i<pointss.size();i++){
    for(int j = 0;j<pointss[0].size();j++){
        cout<<pointss[i][j]<<"\t";
    }cout<<endl;
}cout<<endl;

int size = pointss.size();
    mpoint *points;
    int miny_point_id;
    double *mcos;
    points = new mpoint[size];
    mcos = new double[size];
    for(int i = 0; i < size; i++){
        points[i].x = (double)pointss[i][0];
        points[i].y = (double)pointss[i][1];
    }
    miny_point_id = get_miny_point_id(points, size);
    get_cos(points, mcos, miny_point_id, size);
    sort_points(points, mcos, size);
    get_outpoint(points, size);

结果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用分治算法来解决平面凸包问题,可以按照以下步骤进行: 1. 将给定的点集按照 x 坐标进行排序,如果 x 坐标相同,则按照 y 坐标排序。 2. 将点集分成两个子集,每个子集包含大约一半的点。可以选择一个 x 坐标值作为分割线,将点集分成左右两个子集,或者选择中间的点作为分割线。 3. 对左右两个子集分别进行递归处理,得到左右两个子集的凸包。 4. 合并左右两个子集的凸包,得到整个点集的凸包。 在每一次递归中,可以使用 Graham 扫描算法或者快速凸包算法来求解子集的凸包。 以下是一个用 C++ 实现的示例代码: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point { int x, y; }; // 按照 x 坐标进行排序,如果 x 坐标相同,则按照 y 坐标排序 bool compareX(Point p1, Point p2) { if (p1.x == p2.x) return p1.y < p2.y; return p1.x < p2.x; } // 按照极角排序,极角相同则按照距离原点的距离排序 bool compareAngle(Point p1, Point p2) { int cross = (p1.x - p[0].x) * (p2.y - p[0].y) - (p2.x - p[0].x) * (p1.y - p[0].y); if (cross == 0) return (p1.x - p[0].x) * (p1.x - p[0].x) + (p1.y - p[0].y) * (p1.y - p[0].y) < (p2.x - p[0].x) * (p2.x - p[0].x) + (p2.y - p[0].y) * (p2.y - p[0].y); return cross > 0; } // 计算凸包 vector<Point> convexHull(vector<Point>& points, int left, int right) { if (left == right) { vector<Point> hull; hull.push_back(points[left]); return hull; } int mid = (left + right) / 2; vector<Point> hullLeft = convexHull(points, left, mid); vector<Point> hullRight = convexHull(points, mid + 1, right); int sizeLeft = hullLeft.size(); int sizeRight = hullRight.size(); // 合并左右凸包 int leftmost = 0, rightmost = 0; for (int i = 1; i < sizeLeft; i++) { if (hullLeft[i].x < hullLeft[leftmost].x) leftmost = i; } for (int i = 1; i < sizeRight; i++) { if (hullRight[i].x > hullRight[rightmost].x) rightmost = i; } int lowerLeft = leftmost, lowerRight = rightmost; while (true) { int newLowerLeft = (lowerLeft + sizeLeft - 1) % sizeLeft; int cross = (hullLeft[lowerLeft].x - hullLeft[newLowerLeft].x) * (hullRight[lowerRight].y - hullLeft[newLowerLeft].y) - (hullRight[lowerRight].x - hullLeft[newLowerLeft].x) * (hullLeft[lowerLeft].y - hullLeft[newLowerLeft].y); if (cross < 0) break; lowerLeft = newLowerLeft; } while (true) { int newLowerRight = (lowerRight + 1) % sizeRight; int cross = (hullRight[lowerRight].x - hullLeft[lowerLeft].x) * (hullRight[newLowerRight].y - hullLeft[lowerLeft].y) - (hullRight[newLowerRight].x - hullLeft[lowerLeft].x) * (hullRight[lowerRight].y - hullLeft[lowerLeft].y); if (cross < 0) break; lowerRight = newLowerRight; } vector<Point> hull; int current = lowerLeft; while (current != lowerRight) { hull.push_back(hullLeft[current]); current = (current + 1) % sizeLeft; } hull.push_back(hullLeft[current]); current = lowerRight; while (current != lowerLeft) { hull.push_back(hullRight[current]); current = (current + 1) % sizeRight; } hull.push_back(hullRight[current]); return hull; } vector<Point> convexHull(vector<Point>& points) { int n = points.size(); if (n < 3) { return points; } sort(points.begin(), points.end(), compareX); return convexHull(points, 0, n - 1); } int main() { vector<Point> points = { {0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3} }; vector<Point> hull = convexHull(points); for (int i = 0; i < hull.size(); i++) { cout << "(" << hull[i].x << ", " << hull[i].y << ")" << endl; } return 0; } ``` 这段代码使用分治算法来解决平面凸包问题,其中 `convexHull` 函数实现了分治过程,`compareX` 和 `compareAngle` 函数用于排序,`main` 函数中的示例展示了如何使用该函数来求解平面凸包问题。你可以将自己的点集替换到 `points` 数组中进行测试。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天生_13

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值