Click here to Skip to main content
15,848,930 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication10
	class Program

		static void Main(string[] args)

		public static List DoKMeans(PointCollection points, int clusterCount)

			List allClusters = new List();

			List> allGroups = ListUtility.SplitList(points, clusterCount);

			foreach (List group in allGroups)

				PointCollection cluster = new PointCollection();




			int movements = 1;

			while (movements > 0)

				movements = 0;

				foreach (PointCollection cluster in allClusters)

					for (int pointIndex = 0; pointIndex < cluster.Count; pointIndex++)

						Point point = cluster[pointIndex];

						int nearestCluster = FindNearestCluster(allClusters, point);

						if (nearestCluster != allClusters.IndexOf(cluster))

						if (cluster.Count > 1)

							Point removedPoint = cluster.RemovePoint(point);


							movements += 1;






		return allClusters;


Updated 4-Jan-16 19:34pm
[no name] 5-Jan-16 0:27am    
Provide some detail of the problem and ask a question. Do you know how to debug?
Sergey Alexandrovich Kryukov 5-Jan-16 2:34am    
Error or exception? In what line?
Tomas Takac 5-Jan-16 3:11am    
An (maybe) unrelated observation: If you cycle through the list (cluster) and also remove items at the same time you will skip some items eventually. To avoid that you need to change the for loop to go backwards, from the last item to the first one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900