#include <vector>
#include <map>
//空间点的定义
class Node
{
public:
double pos_x;
double pos_y;
double pos_z;
Node()
{
pos_x = 0.0;
pos_y = 0.0;
pos_z = 0.0;
}
Node(double x,double y,double z)
{
pos_x = x;
pos_y = y;
pos_z = z;
}
friend bool operator < (const Node& first,const Node& second)
{
//对x轴的比较
if(first.pos_x < second.pos_x)
{
return true;
}
else if (first.pos_x > second.pos_x)
{
return false;
}
//对y轴的比较
else
{
if(first.pos_y < second.pos_y)
{
return true;
}
else if (first.pos_y > second.pos_y)
{
return false;
}
//对z轴的比较
else
{
if(first.pos_z < second.pos_z)
{
return true;
}
else if (first.pos_z >= second.pos_z)
{
return false;
}
}
}
}
friend bool operator == (const Node& first,const Node& second)
{
if(first.pos_x == second.pos_x && first.pos_y == second.pos_y && first.pos_z == second.pos_z)
{
return true;
}
else
{
return false;
}
}
};
class KMean
{
private:
int cluster_num;//生成的簇的数量。
std:: vector<Node> mean_nodes;//均值点
std:: vector<Node> data;//所有的数据点
std:: map <int , std:: vector<Node> > cluster;//簇,key为簇的下标,value为该簇中所有点
void Init();//初始化函数(首先随即生成代表点)
void ClusterProcess();//聚类过程,将空间中的点分到不同的簇中
Node GetMean(int cluster_index);//生成均值
void NewCluster();//确定新的簇过程,该函数会调用ClusterProcess函数。
double Kdistance(Node active,Node other);//判断两个点之间的距离
public:
KMean(int c_num,std:: vector<Node> node_vector);
void Star();//启动k均值算法
};
#endif // KMEAN_HEAD
</div>
#include <vector>
#include <map>
//空间点的定义
class Node
{
public:
double pos_x;
double pos_y;
double pos_z;
Node()
{
pos_x = 0.0;
pos_y = 0.0;
pos_z = 0.0;
}
Node(double x,double y,double z)
{
pos_x = x;
pos_y = y;
pos_z = z;
}
friend bool operator < (const Node& first,const Node& second)
{
//对x轴的比较
if(first.pos_x < second.pos_x)
{
&