Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Windows.Forms;
- using System.Windows.Forms.DataVisualization.Charting;
- namespace ksrednich
- {
- public partial class Form1 : Form
- {
- public List<List<double>> punkty;
- public int iloscSrodkow;
- public List<List<double>> srodki = new List<List<double>>();
- public int ziarno = 1000;
- 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 };
- public Form1()
- {
- InitializeComponent();
- chart1.Series.Clear();
- iloscSrodkow = Convert.ToInt32(textBox1.Text);
- PobierzDane(@"C:/Users/laptop/Desktop/spiralka.txt", out punkty);
- }
- public void PobierzDane(string sciezka, out List<List<double>> punkty)
- {
- punkty = new List<List<double>>();
- textBox2.Text = "";
- try
- {
- using (StreamReader sr = new StreamReader(sciezka))
- {
- string linia;
- var i = 1;
- while (null != (linia = sr.ReadLine()))
- {
- linia = linia.Replace('\t', ' ');
- linia = linia.Trim();
- while (linia.Contains(" ")) linia = linia.Replace(" ", " ");
- var liniaTablica = linia.Split(' ');
- var probka = new List<double>();
- if (liniaTablica.Length != 2)
- {
- textBox2.Text += "Nieprawidłowa linia nr " + i + " ";
- }
- else
- {
- var j = 1;
- foreach (var p in liniaTablica)
- {
- var wartosc = p;
- if (wartosc.Length > 0)
- {
- wartosc = wartosc.Replace('.', ',');
- try
- {
- probka.Add(Convert.ToDouble(wartosc));
- }
- catch (Exception)
- {
- textBox2.Text += "Nieprawidłowa wartość " + j + " w lini nr " + i;
- }
- }
- j++;
- }
- if (probka.Count == 2)
- {
- punkty.Add(probka);
- }
- }
- i++;
- }
- }
- }
- catch (Exception)
- {
- textBox2.Text += "Plik nie może zostać wczytany. ";
- }
- if (textBox2.Text.Length == 0)
- {
- textBox2.Text = "Błędy: 0";
- }
- }
- public void RysujWykres()
- {
- chart1.Series.Clear();
- var seria = new Series();
- foreach (var punkt in punkty)
- {
- seria.Points.Add(new DataPoint(punkt[0], punkt[1]));
- }
- seria.ChartType = SeriesChartType.Point;
- chart1.Series.Add(seria);
- }
- public void LosujSrodki(int ilosc)
- {
- var random = new Random(ziarno);
- while (srodki.Count != ilosc)
- {
- var randomInt = random.Next(0, punkty.Count);
- if (!srodki.Contains(punkty[randomInt]))
- {
- srodki.Add(punkty[randomInt]);
- }
- }
- }
- public int UstawKolor(Dictionary<int, double> odleglosciPunktuOdSrodkow)
- {
- var colorNumber = -1;
- var min = (double)int.MaxValue;
- for (int i = 0; i < srodki.Count; i++)
- {
- if (odleglosciPunktuOdSrodkow[i] < min)
- {
- min = odleglosciPunktuOdSrodkow[i];
- colorNumber = i;
- }
- }
- return colorNumber;
- }
- public double LiczOdleglosc(int indexPunktu, int indexSrodka)
- {
- var pointX = punkty[indexPunktu][0];
- var pointY = punkty[indexPunktu][1];
- var srodekX = srodki[indexSrodka][0];
- var srodekY = srodki[indexSrodka][1];
- return Math.Sqrt(((srodekX - pointX) * (srodekX - pointX)) + ((srodekY - pointY) * (srodekY - pointY)));
- }
- public List<List<int>> StworzGrupy()
- {
- List<List<int>> grupy = new List<List<int>>();
- for (int i = 0; i < iloscSrodkow; i++)
- {
- grupy.Add(new List<int>());
- }
- return grupy;
- }
- public List<double> GenerujSrodek(List<int> grupa)
- {
- var sumaX = 0.00;
- var sumaY = 0.00;
- foreach (var indexPunktu in grupa)
- {
- sumaX += punkty[indexPunktu][0];
- sumaY += punkty[indexPunktu][1];
- }
- return new List<double>() { sumaX / grupa.Count, sumaY / grupa.Count };
- }
- public void GenerujNoweSrodki(List<List<int>> grupy)
- {
- srodki.Clear();
- for (int i = 0; i < iloscSrodkow; i++)
- {
- srodki.Add(GenerujSrodek(grupy[i]));
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- srodki.Clear();
- RysujWykres();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- chart1.Series.Clear();
- var iloscSrodkowZPola = Convert.ToInt32(textBox1.Text);
- if (iloscSrodkowZPola != iloscSrodkow)
- {
- iloscSrodkow = iloscSrodkowZPola;
- srodki.Clear();
- }
- if (srodki.Count == 0)
- {
- LosujSrodki(iloscSrodkow);
- }
- Dictionary<int, Dictionary<int, double>> odleglosci = new Dictionary<int, Dictionary<int, double>>();
- for (int i = 0; i < punkty.Count; i++)
- {
- if (!odleglosci.ContainsKey(i))
- {
- odleglosci.Add(i, new Dictionary<int, double>());
- }
- for (int j = 0; j < iloscSrodkow; j++)
- {
- var odleglosc = LiczOdleglosc(i, j);
- odleglosci[i].Add(j, odleglosc);
- }
- }
- var grupy = StworzGrupy();
- var seria = new Series() { ChartType = SeriesChartType.Point };
- var k = 0;
- foreach (var punkt in punkty)
- {
- var indexKolor = UstawKolor(odleglosci[k]); // indexKolor to też numer grupy
- var p = new DataPoint(punkt[0], punkt[1]) { Color = kolory[indexKolor] };
- seria.Points.Add(p);
- grupy[indexKolor].Add(k);
- k++;
- }
- foreach (var srodek in srodki)
- {
- var p = new DataPoint(srodek[0], srodek[1]) { Color = Color.Red };
- seria.Points.Add(p);
- }
- chart1.Series.Add(seria);
- GenerujNoweSrodki(grupy);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement