public tDot Update(tDot center) { color = center.color; type = center.type; _paint(); return(this); }
private void run_kMeans() { var rd = new Random(); bool isEnd = false; int sW = panel1.Location.X; int eW = panel1.Width + sW; int sH = panel1.Location.Y; int eH = panel1.Height + sH; int k = (int)numK.Value; int asd = rd.Next(); foreach (var item in lstCenter) { item.Clear(true); } lstCenter = new List <tDot>(); for (int i = 0; i < k; i++) { tDot ob = new tDot(new Point() { X = rd.Next(sW, eW), Y = rd.Next(sH, eH) }); ob.type = (eType)Enum.GetValues(typeof(eType)).GetValue(1 + k % 3); //KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor)); //KnownColor randomColorName = names[rd.Next(names.Length)]; //Color randomColor = Color.FromKnownColor(randomColorName); Color[] c = new Color[] { Color.Green, Color.Red, Color.Blue, Color.Yellow }; asd = (asd + 1) % c.Length; ob.color = c[asd]; ob._paint(true); _log(string.Format("Center {0}: {1}", i, ob.Location.ToString())); lstCenter.Add(ob); } int count = 0; List <int> lstIndex = new List <int>(); for (int i = 0; i < lstDot.Count; i++) { lstIndex.Add(-1); } while (!isEnd) { isEnd = true; count++; _log("K-Means: " + count); for (int i = 0; i < lstDot.Count; i++) { int id = lstDot[i].SelectCenter(lstCenter); if (lstIndex[i] != id) { lstIndex[i] = id; isEnd = false; } } for (int i = 0; i < lstCenter.Count; i++) { int sx = 0, sy = 0, cc = 0; for (int j = 0; j < lstIndex.Count; j++) { if (lstIndex[j] == i) { sx += lstDot[j].Location.X; sy += lstDot[j].Location.Y; cc++; } } if (cc <= 0) { cc = 1; } lstCenter[i].Clear(true); lstCenter[i].Location = new Point(sx / cc, sy / cc); lstCenter[i]._paint(true); _log(string.Format("Center_{0}: [{3} == {1} - {4}=={2}]", i, lstCenter[i].Location.X, lstCenter[i].Location.Y, sx / cc, sy / cc)); } Thread.Sleep(1000); } _log("Done"); }