Advertisement
Adik28

K średnich / A

Jan 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Windows.Forms;
  6. using System.Windows.Forms.DataVisualization.Charting;
  7.  
  8. namespace ksrednich
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         public List<List<double>> punkty;
  13.         public int iloscSrodkow;
  14.         public List<List<double>> srodki = new List<List<double>>();
  15.         public int ziarno = 1000;
  16.         public List<Color> kolory = new List<Color>() { Color.Black, Color.Green, Color.Yellow, Color.Blue, Color.Brown, Color.DarkBlue, Color.DarkOrange, Color.Aqua, Color.Coral, Color.Cyan, Color.Khaki, Color.LightPink };
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.             chart1.Series.Clear();
  22.             iloscSrodkow = Convert.ToInt32(textBox1.Text);
  23.             PobierzDane(@"C:/Users/laptop/Desktop/spiralka.txt", out punkty);
  24.         }
  25.  
  26.         public void PobierzDane(string sciezka, out List<List<double>> punkty)
  27.         {
  28.             punkty = new List<List<double>>();
  29.             textBox2.Text = "";
  30.  
  31.             try
  32.             {
  33.                 using (StreamReader sr = new StreamReader(sciezka))
  34.                 {
  35.                     string linia;
  36.                     var i = 1;
  37.                     while (null != (linia = sr.ReadLine()))
  38.                     {
  39.  
  40.                         linia = linia.Replace('\t', ' ');
  41.                         linia = linia.Trim();
  42.                         while (linia.Contains("  ")) linia = linia.Replace("  ", " ");
  43.                         var liniaTablica = linia.Split(' ');
  44.                         var probka = new List<double>();
  45.                         if (liniaTablica.Length != 2)
  46.                         {
  47.                             textBox2.Text += "Nieprawidłowa linia nr " + i + " ";
  48.                         }
  49.                         else
  50.                         {
  51.                             var j = 1;
  52.                             foreach (var p in liniaTablica)
  53.                             {
  54.                                 var wartosc = p;
  55.                                 if (wartosc.Length > 0)
  56.                                 {
  57.                                     wartosc = wartosc.Replace('.', ',');
  58.                                     try
  59.                                     {
  60.                                         probka.Add(Convert.ToDouble(wartosc));
  61.                                     }
  62.                                     catch (Exception)
  63.                                     {
  64.                                         textBox2.Text += "Nieprawidłowa wartość " + j + " w lini nr " + i;
  65.                                     }
  66.                                 }
  67.                                 j++;
  68.                             }
  69.                             if (probka.Count == 2)
  70.                             {
  71.                                 punkty.Add(probka);
  72.                             }
  73.                         }
  74.                         i++;
  75.                     }
  76.  
  77.                 }
  78.             }
  79.             catch (Exception)
  80.             {
  81.                 textBox2.Text += "Plik nie może zostać wczytany. ";
  82.             }
  83.  
  84.             if (textBox2.Text.Length == 0)
  85.             {
  86.                 textBox2.Text = "Błędy: 0";
  87.             }
  88.  
  89.         }
  90.  
  91.         public void RysujWykres()
  92.         {
  93.             chart1.Series.Clear();
  94.             var seria = new Series();
  95.             foreach (var punkt in punkty)
  96.             {
  97.                 seria.Points.Add(new DataPoint(punkt[0], punkt[1]));
  98.             }
  99.             seria.ChartType = SeriesChartType.Point;
  100.             chart1.Series.Add(seria);
  101.         }
  102.  
  103.         public void LosujSrodki(int ilosc)
  104.         {
  105.             var random = new Random(ziarno);
  106.             while (srodki.Count != ilosc)
  107.             {
  108.                 var randomInt = random.Next(0, punkty.Count);
  109.                 if (!srodki.Contains(punkty[randomInt]))
  110.                 {
  111.                     srodki.Add(punkty[randomInt]);
  112.                 }
  113.             }
  114.         }
  115.  
  116.         public int UstawKolor(Dictionary<int, double> odleglosciPunktuOdSrodkow)
  117.         {
  118.             var colorNumber = -1;
  119.             var min = (double)int.MaxValue;
  120.             for (int i = 0; i < srodki.Count; i++)
  121.             {
  122.                 if (odleglosciPunktuOdSrodkow[i] < min)
  123.                 {
  124.                     min = odleglosciPunktuOdSrodkow[i];
  125.                     colorNumber = i;
  126.                 }
  127.             }
  128.             return colorNumber;
  129.         }
  130.  
  131.         public double LiczOdleglosc(int indexPunktu, int indexSrodka)
  132.         {
  133.             var pointX = punkty[indexPunktu][0];
  134.             var pointY = punkty[indexPunktu][1];
  135.             var srodekX = srodki[indexSrodka][0];
  136.             var srodekY = srodki[indexSrodka][1];
  137.  
  138.             return Math.Sqrt(((srodekX - pointX) * (srodekX - pointX)) + ((srodekY - pointY) * (srodekY - pointY)));
  139.         }
  140.  
  141.         public List<List<int>> StworzGrupy()
  142.         {
  143.             List<List<int>> grupy = new List<List<int>>();
  144.  
  145.             for (int i = 0; i < iloscSrodkow; i++)
  146.             {
  147.                 grupy.Add(new List<int>());
  148.             }
  149.  
  150.             return grupy;
  151.         }
  152.  
  153.         public List<double> GenerujSrodek(List<int> grupa)
  154.         {
  155.             var sumaX = 0.00;
  156.             var sumaY = 0.00;
  157.             foreach (var indexPunktu in grupa)
  158.             {
  159.                 sumaX += punkty[indexPunktu][0];
  160.                 sumaY += punkty[indexPunktu][1];
  161.             }
  162.             return new List<double>() { sumaX / grupa.Count, sumaY / grupa.Count };
  163.         }
  164.  
  165.         public void GenerujNoweSrodki(List<List<int>> grupy)
  166.         {
  167.             srodki.Clear();
  168.             for (int i = 0; i < iloscSrodkow; i++)
  169.             {
  170.                 srodki.Add(GenerujSrodek(grupy[i]));
  171.             }
  172.         }
  173.  
  174.         private void button1_Click(object sender, EventArgs e)
  175.         {
  176.             srodki.Clear();
  177.             RysujWykres();
  178.         }
  179.  
  180.         private void button2_Click(object sender, EventArgs e)
  181.         {
  182.             chart1.Series.Clear();
  183.  
  184.             var iloscSrodkowZPola = Convert.ToInt32(textBox1.Text);
  185.             if (iloscSrodkowZPola != iloscSrodkow)
  186.             {
  187.                 iloscSrodkow = iloscSrodkowZPola;
  188.                 srodki.Clear();
  189.             }
  190.  
  191.             if (srodki.Count == 0)
  192.             {
  193.                 LosujSrodki(iloscSrodkow);
  194.             }
  195.  
  196.             Dictionary<int, Dictionary<int, double>> odleglosci = new Dictionary<int, Dictionary<int, double>>();
  197.             for (int i = 0; i < punkty.Count; i++)
  198.             {
  199.                 if (!odleglosci.ContainsKey(i))
  200.                 {
  201.                     odleglosci.Add(i, new Dictionary<int, double>());
  202.                 }
  203.                 for (int j = 0; j < iloscSrodkow; j++)
  204.                 {
  205.                     var odleglosc = LiczOdleglosc(i, j);
  206.                     odleglosci[i].Add(j, odleglosc);
  207.                 }
  208.             }
  209.  
  210.             var grupy = StworzGrupy();
  211.             var seria = new Series() { ChartType = SeriesChartType.Point };
  212.  
  213.             var k = 0;
  214.             foreach (var punkt in punkty)
  215.             {
  216.                 var indexKolor = UstawKolor(odleglosci[k]); // indexKolor to też numer grupy
  217.                 var p = new DataPoint(punkt[0], punkt[1]) { Color = kolory[indexKolor] };
  218.                 seria.Points.Add(p);
  219.  
  220.                 grupy[indexKolor].Add(k);
  221.                 k++;
  222.             }
  223.  
  224.             foreach (var srodek in srodki)
  225.             {
  226.                 var p = new DataPoint(srodek[0], srodek[1]) { Color = Color.Red };
  227.                 seria.Points.Add(p);
  228.             }
  229.  
  230.             chart1.Series.Add(seria);
  231.             GenerujNoweSrodki(grupy);
  232.         }
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement