public static void AllMoveToCentroid(Kmean graph) { foreach (var claster in graph.clasters) { claster.MoveToCentroid(); claster.edges.Clear(); } }
private void button1_Click(object sender, EventArgs e) { //timer1.Start(); Graphics graphics = CreateGraphics(); Kmean.Partitioning(graph); foreach (var claster in graph.clasters) { foreach (var edge in claster.edges) { graphics.DrawLine(Pens.Black, claster.Location.ToPoint(), edge.Item1.ToPoint()); } } Kmean.FindCentroid(graph); Kmean.AllMoveToCentroid(graph); }
public static void FindCentroid(Kmean graph) { foreach (Claster claster in graph.clasters) { CPoint result = new CPoint(0, 0); int divider = 0; foreach (var edge in claster.edges) { result += edge.Item1; divider++; } if (divider != 0) { result /= divider; } claster.Centroid = result; } }
public static void Partitioning(Kmean graph) { Claster nearestClaster = null; int tempDistance; int curDistance; foreach (CPoint point in graph.points) { tempDistance = -1; foreach (Claster claster in graph.clasters) { curDistance = (int)Math.Sqrt(Math.Pow(Math.Abs(point.X - claster.Location.X), 2) + Math.Pow(Math.Abs(point.Y - claster.Location.Y), 2)); if (curDistance < tempDistance || tempDistance == -1) { nearestClaster = claster; tempDistance = curDistance; } } nearestClaster.AddEdge(point, tempDistance); } }