Exemplo n.º 1
0
        public static List <Figure>[] Clustarization(List <Figure> list, int count)
        {
            //Normalization(list);
            Double maxP = 0;
            Double maxE = 0;
            Double minE = Double.MaxValue;
            Double minP = Double.MaxValue;
            Double maxD = 0;

            foreach (var item in list)
            {
                if (maxP < item.Perimetr)
                {
                    maxP = item.Perimetr;
                }
                if (maxE < item.Elongation)
                {
                    maxE = item.Elongation;
                }
                if (maxD < item.Density)
                {
                    maxD = item.Density;
                }
                if (minP > item.Perimetr)
                {
                    minP = item.Perimetr;
                }
                if (minE > item.Elongation)
                {
                    minE = item.Elongation;
                }
            }
            Double step  = (maxE - minE) / (count + 1);
            Double step1 = (maxP - minP) / (count + 1);

            Random rand = new Random();

            MyPoint[]       clustCenter = new MyPoint[count];
            List <Figure>[] clust       = new List <Figure> [count];
            MyPoint[]       check       = new MyPoint[count];

            for (int i = 0; i < count; i++)
            {
                int z = rand.Next(list.Count);
                clustCenter[i] = new MyPoint(list[z].Perimetr, list[z].Elongation/*Convert.ToDouble(rand.Next(Convert.ToInt32(maxP + 1))), Convert.ToDouble(rand.Next(Convert.ToInt32(maxE + 1)))*/
                                             /*  Convert.ToDouble(rand.Next(Convert.ToInt32(maxD + 1)))*/);
                clust[i] = new List <Figure>();
            }

            //MyPoint clust2Center = new MyPoint(Convert.ToDouble(rand.Next(Convert.ToInt32(maxP + 1))), rand.NextDouble());
            bool flag = true;

            while (flag)
            {
                for (int j = 0; j < clustCenter.Count(); j++)
                {
                    check[j] = new MyPoint(clustCenter[j]);
                }
                foreach (var clustItem in clust)
                {
                    clustItem.Clear();
                }
                foreach (var figure in list)
                {
                    List <Double> dist = new List <Double>();
                    foreach (var center in clustCenter)
                    {
                        dist.Add(center.GetDistance(figure.Perimetr, figure.Elongation /*, figure.Density-*/));
                    }
                    clust[dist.IndexOf(dist.Min())].Add(figure);
                }
                int i = 0;
                for (int j = 0; j < count; j++)
                {
                    clustCenter[j].NewCenter(clust[j]);
                    i++;
                }
                i    = 0;
                flag = false;
                foreach (var center in clustCenter)
                {
                    if (!center.Equals(check[i]))
                    {
                        flag = true;
                        break;
                    }
                    i++;
                }
            }
            foreach (var center in clustCenter)
            {
                Console.WriteLine(center.x.ToString() + "   " + center.y.ToString());
            }
            return(clust);
        }