点集欧式聚类C++

 欧式聚类点集,获取分类的点集以及最大点集,少量点集环境下计算效率很好

输入:点集+阈值

输出:分类结构+最大分类集

int EuclideanClustering(std::vector srcPts, float threshold, std::vector>& dstPts, std::vector& maxPts)
	{
		if (!srcPts.size())
			return -1;
		dstPts.clear();
		float distance2;
		while (srcPts.size() > 0)//遍历srcPts所有点塞到分类的vector中
		{
			std::vector pts;
			pts.push_back(srcPts[srcPts.size() - 1]);//返回最后一个并删除
			srcPts.pop_back();
			int flag;
			do
			{
				flag = 0;
				for (size_t i = 0; i < srcPts.size(); i++)
				{
					for (size_t j = 0; j < pts.size(); j++)
					{
						distance2 = (srcPts[i] - pts[j]).x*(srcPts[i] - pts[j]).x + (srcPts[i] - pts[j]).y*(srcPts[i] - pts[j]).y;
						if (distance2 < threshold*threshold)//如果距离小于阈值,传递过去
						{
							pts.push_back(srcPts[i]);
							srcPts.erase(srcPts.begin() + i);
							i--;
							flag++;
							break;
						}
					}
				}
			} while (flag > 0);
			dstPts.push_back(pts);
		}
		
		//找出最大的聚类
		int max_num = 0, max_no = 0;
		for (size_t i = 0; i < dstPts.size(); i++)
		{
			if (max_num < dstPts[i].size())
			{
				max_num = dstPts[i].size();
				max_no = i;
			}
		}
		maxPts = dstPts[max_no];
	}

你可能感兴趣的:(c++)