Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApp3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public struct point
- {
- public double X;
- public double Y;
- public point(double X, double Y) : this()
- {
- this.X = X;
- this.Y = Y;
- }
- }
- Bitmap img;
- Graphics graphics;
- private void Form1_Load(object sender, EventArgs e)
- {
- forel();
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- forel();
- }
- public int kx, ky;
- public void forel()
- {
- double R = (double)numericUpDown1.Value;
- richTextBox1.Clear();
- img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
- graphics = Graphics.FromImage(img);
- pictureBox1.Image = img;
- Brush brush = Brushes.White;
- Pen pen = new Pen(Color.Red);
- Pen pen1 = new Pen(Color.Purple);
- //Brush brush1 = new SolidColorBrush(color);
- graphics.FillRectangle(brush, 0, 0, img.Width, img.Height);
- kx = 20;
- ky = 20;
- List<point> X = generateArray(10);
- drawPoints(X);
- int k = 1;
- do
- {
- point center = getCenterOfMass(X);
- double radius = getFirstRadius(X, center);
- if (radius == 0)
- {
- radius = 1;
- }
- //graphics.DrawEllipse(pen1, (float)(center.X * kx - radius * kx), (float)(center.Y * ky - radius * ky),
- //(float)(2 * radius * kx), (float)(2 * radius * ky));
- point oldCenter = new point();
- List<point> clasterPoint;
- do
- {
- clasterPoint = new List<point>();
- radius *= R;
- oldCenter = center;
- center = getRandomPoint(X);
- clasterPoint = getPointsIn(X, radius, center);
- center = getCenterOfMass(clasterPoint);
- //graphics.DrawEllipse(pen, (float)(center.X * kx - radius * kx), (float)(center.Y * ky - radius * ky),
- //(float)(2 * radius * kx), (float)(2 * radius * ky));
- } while (oldCenter.X != center.X && oldCenter.Y != center.Y);
- graphics.DrawEllipse(pen, (float)(center.X * kx - radius * kx), (float)(center.Y * ky - radius * ky),
- (float)(2 * radius * kx), (float)(2 * radius * ky));
- graphics.DrawString(k.ToString(), new Font("Arial", 10), Brushes.Lime, (float)center.X*kx, (float)center.Y*ky);
- richTextBox1.AppendText("Кластер № " + k.ToString() + " содержит " + clasterPoint.Count.ToString() + " т.\n");
- for (int i = 0; i < clasterPoint.Count; i++)
- {
- richTextBox1.AppendText("(" + Math.Round(clasterPoint[i].X,3).ToString() + " ; " + Math.Round(clasterPoint[i].Y,3).ToString() + ")\n");
- }
- X = clean(radius, X, center);
- k++;
- } while (X.Count != 0);
- }
- public List<point> clean(double uR, List<point> X, point center)
- {
- for (int i = 0; i < X.Count; i++)
- {
- if (Math.Sqrt(Math.Pow(X[i].X - center.X, 2) + Math.Pow(X[i].Y - center.Y, 2)) <= uR)
- X.RemoveAt(i);
- }
- return X;
- }
- public point getCenterOfMass(List<point> Array)
- {
- point center = new point();
- double sumX = 0;
- double sumY = 0;
- for (int i = 0; i < Array.Count; i++)
- {
- sumX += Array[i].X;
- sumY += Array[i].Y;
- }
- center.X = sumX / Array.Count;
- center.Y = sumY / Array.Count;
- return center;
- }
- //возвращает массив точек принадлежащих окружности
- public List<point> getPointsIn(List<point> X, double uR, point center)
- {
- List<point> new_array = new List<point>();
- for (int i = 0; i < X.Count; i++)
- {
- if (Math.Sqrt(Math.Pow(X[i].X - center.X, 2) + Math.Pow(X[i].Y - center.Y, 2)) <= uR)
- new_array.Add(new point(X[i].X, X[i].Y));
- }
- return new_array;
- }
- public point getRandomPoint(List<point> Array)
- {
- point point = new point();
- Random random = new Random();
- int randomIndex = random.Next(0, Array.Count);
- point.X = Array[randomIndex].X;
- point.Y = Array[randomIndex].Y;
- return point;
- }
- public double getFirstRadius(List<point> X, point center)
- {
- double radius = 0;
- for (int i = 0; i < X.Count; i++)
- {
- if (radius < Math.Sqrt(Math.Pow(center.X - X[i].X, 2) + Math.Pow(center.Y - X[i].Y, 2)))
- {
- radius = Math.Sqrt(Math.Pow(center.X - X[i].X, 2) + Math.Pow(center.Y - X[i].Y, 2));
- }
- }
- return radius;
- }
- public List<point> generateArray(int count)
- {
- Random random = new Random();
- List<point> Array = new List<point>();
- for (int i = 0; i < count; i++)
- {
- point P = new point(13 * random.NextDouble() + 11, 17 * random.NextDouble() + 9);
- Array.Add(P);
- }
- return Array;
- }
- public void drawPoints(List<point> Array)
- {
- for (int i = 0; i < Array.Count; i++)
- graphics.FillEllipse(Brushes.Green, (float)(Array[i].X * kx - 2), (float)(Array[i].Y * ky - 2), 4, 4);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement