我是靠谱客的博主 花痴小蘑菇,最近开发中收集的这篇文章主要介绍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);

结果

最后

以上就是花痴小蘑菇为你收集整理的C++解决凸包问题凸包类(参考网上方法)测试结果的全部内容,希望文章能够帮你解决C++解决凸包问题凸包类(参考网上方法)测试结果所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部