Пример #1
0
        private double Distance(dane centroid, dane punkt)
        {
            double x1 = Math.Pow(centroid.X - punkt.X, 2);
            double x2 = Math.Pow(centroid.Y - punkt.Y, 2);

            return(Math.Sqrt(x1 + x2));
        }
Пример #2
0
 public void NadanieGrupZbiorowi(ref List <dane> zbior, List <dane> centroidy)
 {
     for (int i = 0; i < zbior.Count; i++)
     {
         dane punkt2 = zbior[i];                 //kopiowana jest referencja do zmiennej dlatego to zadziala
         NadanieGrupy(ref punkt2, centroidy);
     }
 }
        public List <dane> czytaj()
        {
            List <dane> wejsciadane = new List <dane>();
            dane        temp        = new dane();

            using (FileStream fs = File.Open(@"dane.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (BufferedStream bs = new BufferedStream(fs))
                {
                    using (StreamReader sr = new StreamReader(bs))
                    {
                        int    j = 0;
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            int i = 0;

                            line += "\0\0";
                            while (line[i] != '\0')
                            {
                                string liczba       = "";
                                double liczba_wej   = 0.0;
                                double liczba_oczek = 0.0;
                                while (line[i] == ' ')
                                {
                                    i++;
                                }
                                while (line[i] != ' ')
                                {
                                    liczba += line[i]; i++;
                                }
                                liczba_wej = Convert.ToDouble(liczba);
                                liczba     = "";

                                while (line[i] == ' ')
                                {
                                    i++;
                                }
                                while (line[i] != '\r' && line[i] != '\0' && line[i] != '\r')
                                {
                                    liczba += line[i]; i++;
                                }
                                liczba_oczek = Convert.ToDouble(liczba);
                                i++;
                                temp.X       = liczba_wej;
                                temp.Y       = liczba_oczek;
                                temp.idGrupy = -1;
                                wejsciadane.Add(temp);
                            }
                            j++;

                            // MessageBox.Show(Convert.ToString((wejsciadane[0, j]))+" "+ Convert.ToString((oczekiwania[0, j])));
                        }
                    }
                }
            return(wejsciadane);
        }
        public void ReadFile(string path, ref List <dane> punkty)
        {
            string s;

            using (StreamReader sr = new StreamReader(path))//wczytanie
            {
                s = sr.ReadToEnd();
            }
            List <string> numb = new List <string>();
            int           it   = 0;
            string        temp = "";

            while (s.Length != 0)
            {
                dane temPkt = new dane();
                temPkt.idGrupy = -1;
                while (s[it] != '\r' && s[it] != '\n' && s[it] != '\0' && s.Length > it + 1)
                {
                    if (s[it] == ',')                   //Jesli przecinek to dodaj x
                    {
                        temPkt.X = Convert.ToDouble(temp);
                        s        = s.Remove(0, it + 1);
                        it       = 0;
                        temp     = "";
                        continue;
                    }
                    temp += s[it];
                    it++;
                }
                if (temp != "")
                {
                    temPkt.Y = Convert.ToDouble(temp);
                    punkty.Add(temPkt);
                }
                if (s.Length != it)
                {
                    s = s.Remove(0, it + 1);
                }
                it   = 0;
                temp = "";
            }
        }
Пример #5
0
        public void NadanieGrupy(ref dane punktDocelowy, List <dane> listaPunktow)
        {
            Dictionary <double, dane> d = new Dictionary <double, dane>();

            foreach (var punkt in listaPunktow)
            {
                double od = Distance(punkt, punktDocelowy);


                while (d.ContainsKey(od))               //w razie gdyby algorytm nie mogl zdecydowac do ktorego centroidu mamy przypisac punkt
                {
                    od = od + 0.000001;
                }
                d.Add(od, punkt);
            }
            var g = d.First(k => k.Key == d.Keys.Min()).Value.idGrupy;

            punktDocelowy.idGrupy = g;
            bladKwantyzacji      += Distance(punktDocelowy, listaPunktow[g]);
        }