点集从上到下,从左到右进行Z字型排序(C++与python实现,自写)

C++实现:

void PointDisgus(vector<Point>& Points) {
    Point t;
    int n = Points.size();
    int i, j;
    vector<Point> OutPoints;
    vector<Point> Points_ = Points;
    std::sort(Points_.begin(), Points_.end(), compareValue_y);
    for (vector<Point>::iterator iter = Points_.begin(); iter != Points_.end(); iter++)
    {
        vector<Point> PNews;
        int index = iter - Points_.begin();
        Point P = *iter;
        for (int i = index; i < n; i++)
        {
            if (abs(P.y - Points_[i].y) < 12)    // 两个点在图中并不一定水平,设置一定偏差
            {
                PNews.push_back(Points_[i]);
                iter++;
            }
        }
        std::sort(PNews.begin(), PNews.end(), compareValue_x);
        for (auto pp : PNews) {
            OutPoints.push_back(pp);
        }
        iter = iter - 1;
        PNews.clear();
    }
    Points = OutPoints;
}

python实现:

def PointDisgus(Points):
    n = len(Points)
    Points_ = Points
    Points_.sort(key=lambda elem: elem[1])
    OutPoints = []
    a = 0
    for i, P in enumerate(Points_):
        PNews = []
        if i < a:
            continue
        for j in range(i, n):
            if abs(P[1] - Points_[j][1]) < 30:  # 设置y方向上的偏差
                PNews.append(Points_[j])
                a += 1
        PNews.sort(key=lambda elem: elem[0])
        for pp in PNews:
            OutPoints.append(pp)
        PNews.clear()
    return OutPoints

 

posted @ 2023-03-17 10:42  cn_gzb  阅读(112)  评论(0编辑  收藏  举报