Exemple #1
0
        private MaxPoint Calculate(ClassesData classes)
        {
            double tempVar     = 0;
            double maxDistance = 0;
            Point  numPoint    = new Point(1, 1);

            foreach (PointData pd in classes.ObjectClass)
            {
                if (pd == classes.ObjectClass.First())
                {
                    tempVar     = Math.Sqrt(Math.Pow(pd.point.X - classes.center.point.X, 2) + Math.Pow(pd.point.Y - classes.center.point.Y, 2));
                    maxDistance = tempVar;
                }
                else
                {
                    tempVar = Math.Sqrt(Math.Pow(pd.point.X - classes.center.point.X, 2) + Math.Pow(pd.point.Y - classes.center.point.Y, 2));
                    if (maxDistance < tempVar)
                    {
                        maxDistance = tempVar;
                        numPoint    = pd.point;
                    }
                }
            }
            MaxPoint mp = new MaxPoint(numPoint, maxDistance);

            return(mp);
        }
Exemple #2
0
        private void ResultButton_Click(object sender, EventArgs e)
        {
            PaintPanel.Refresh();
            classes = new ClassesData[0];
            Random r = new Random();
            Point  p = pointlist.pointcollection[r.Next(0, pointlist.pointcollection.Length)].point;

            Array.Resize(ref classes, classes.Length + 1);
            classes[classes.Length - 1] = (new ClassesData(p.X, p.Y, Color.Black));
            p = pointlist.pointcollection[Calculate(pointlist, classes.First().center.point)].point;
            Array.Resize(ref classes, classes.Length + 1);
            classes[classes.Length - 1] = (new ClassesData(p.X, p.Y, Color.Black));
            for (int i = 0; i < pointlist.pointcollection.Length; i++)
            {
                int numCluster = Calculate(classes, pointlist.pointcollection[i].point);
                pointlist.pointcollection[i].color = colors[numCluster];
                classes[numCluster].ObjectClass.Add(pointlist.pointcollection[i]);
            }

            MaxPoint mp = NewCenter(classes);

            while (IsValidCenter(mp))
            {
                Array.Resize(ref classes, classes.Length + 1);
                classes[classes.Length - 1] = (new ClassesData(mp.point.X, mp.point.Y, Color.Black));
                for (int i = 0; i < pointlist.pointcollection.Length; i++)
                {
                    int numCluster = Calculate(classes, pointlist.pointcollection[i].point);
                    pointlist.pointcollection[i].color = colors[numCluster];
                    classes[numCluster].ObjectClass.Add(pointlist.pointcollection[i]);
                }
                mp = NewCenter(classes);
            }
            foreach (PointData pd in pointlist.pointcollection)
            {
                graph.DrawRectangle(new Pen(pd.color),
                                    pd.point.X, pd.point.Y, 1, 1);
            }
            for (int i = 0; i < classes.Length; i++)
            {
                Rectangle rect = new Rectangle(classes[i].center.point.X,
                                               classes[i].center.point.Y, 10, 10);

                graph.DrawRectangle(new Pen(classes[i].center.color),
                                    rect);
                graph.FillRectangle(Brushes.Black, rect);
            }
        }
Exemple #3
0
        private bool IsValidCenter(MaxPoint NewPoint)
        {
            double l = 0;

            double[] ll = new double[classes.Length];
            for (int i = 0; i < classes.Length; i++)
            {
                for (int j = 0; j < classes.Length - 1; j++)
                {
                    ll[i] += Math.Sqrt(Math.Pow(classes[j].center.point.X - classes[j + 1].center.point.X, 2) +
                                       Math.Pow(classes[j].center.point.Y - classes[j + 1].center.point.Y, 2));
                }
                l += ll[i];
            }
            if (NewPoint.length > l / (classes.Length * 2))
            {
                return(true);
            }
            return(false);
        }
Exemple #4
0
        private MaxPoint NewCenter(ClassesData[] classes)
        {
            MaxPoint[] points = new MaxPoint[classes.Length];
            MaxPoint   p      = new MaxPoint(new Point(0, 0), 0);

            for (int i = 0; i < classes.Length; i++)
            {
                points[i] = Calculate(classes[i]);
            }
            for (int i = 0; i < points.Length - 1; i++)
            {
                if (points[i].length > points[i].length)
                {
                    p = points[i];
                }
                else
                {
                    p = points[i + 1];
                }
            }
            return(p);
        }