C++ 存储在向量c+中的聚类点+;

C++ 存储在向量c+中的聚类点+;,c++,algorithm,C++,Algorithm,我有下面的向量 { Point(100, 200), Point(101, 202), Point(200, 200), Point(201, 202), Point(203, 204), Point(100, 400), Point(102, 402), Point(200, 400), Point(202, 401), Point(205, 405) }; 该向量包含矩形的顶点和顶点的一些相邻点。我需要从这些点提取矩形顶点,这意味着点(100200)和(101102)我只需要其

我有下面的向量

{ Point(100, 200), Point(101, 202), Point(200, 200), 
  Point(201, 202), Point(203, 204), Point(100, 400), 
  Point(102, 402), Point(200, 400), Point(202, 401), Point(205, 405) };
该向量包含矩形的顶点和顶点的一些相邻点。我需要从这些点提取矩形顶点,这意味着点
(100200)
(101102)
我只需要其中一个。然后对于点
(200200)
(201202)
(203204)
,我只需要一个点(可能是这些相邻点的平均值或中心)等等。它可以是一个分布相似的三角形,也可以是一条有两组的直线,或者是一个有一组的点


请指导我如何才能做到这一点?我应该使用Kmeans吗?如果是,如何使用?如果没有其他clustring算法来解决此问题。

有了一些乐趣,您可以使用以下简单算法。
阈值可以更改,并以某种方式对应于您的10个单位距离

#包括
#包括
#包括
结构点{
点(intxx=0,intyy=0):x(xx),y(yy){
浮动x;
浮动y;

friend std::ostream&Operator属于同一顶点的点可以有多远?让我们说10个单位,但距离应该是参数控制的。这只在点有序的情况下起作用。如果你随机化这些点,你将得到许多不同的种子,这些种子可能靠得很近,也就是说,如果点是随机的,你就无法链接你完全正确:)。我以op为例假设了这一点
#include <vector>
#include <cmath>
#include <iostream>

struct Point {
    Point(int xx=0, int yy=0) : x(xx), y(yy) {}
    float x;
    float y;
    friend std::ostream & operator<<(std::ostream &os, const Point& p) {
        return os << "(" << p.x << ";" << p.y << ")";
    }
};

float distance(Point a, Point b) { return std::sqrt( (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }

Point average(const std::vector<Point>& vec) {
    Point p_avg;
    for (auto const &p : vec) {
        p_avg.x += p.x;
        p_avg.y += p.y;
    }
    p_avg.x /= vec.size();
    p_avg.y /= vec.size();
    return p_avg;
}

int main() {
    // your list of points in vertices
    std::vector<Point> vertices {Point(101, 202), Point(200, 200), Point(201, 202), Point(203, 204), Point(100, 400), Point(102, 402), Point(200, 400), Point(202, 401), Point(205, 405) };
    int threshold = 10; // change the value according to your context
    std::vector<std::vector<Point>> rect_vertices; // here we handle rect_vertices where vertices on one dimension are supposed to be neighbors 
    rect_vertices.push_back(std::vector<Point>{Point(100, 200)}); // we have to give an arbitrary Point here
    for (auto const &a : vertices) {
        std::size_t size = rect_vertices.size();
        bool is_added = false; // boolean to see if point a has a neighbor in rect_vertices
        for (std::size_t i = 0; i < size; ++ i) {
            if (distance(rect_vertices[i][0],a) < threshold) {
                rect_vertices[i].push_back(a); // we add the neighbor a in the same dimension rect_vertices[i]
                is_added = true;
            }
        }
        if (!is_added) { rect_vertices.push_back(std::vector<Point>{a}); } // if no neighbor found then add an other vector<Point> to rect_vertices
    }
    for (auto const &v : rect_vertices) {
        for (auto const &p : v) {
            std::cout << p << " | ";  // print points in a neighborood
        }
        std::cout << " => avg : " << average(v) << std::endl; // print the average for each neighborood
    }
}