Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApp3
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         public struct point
  20.         {
  21.             public double X;
  22.             public double Y;
  23.             public point(double X, double Y) : this()
  24.             {
  25.                 this.X = X;
  26.                 this.Y = Y;
  27.             }
  28.         }
  29.         Bitmap img;
  30.         Graphics graphics;
  31.  
  32.  
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             forel();
  36.         }
  37.  
  38.         private void panel1_Paint(object sender, PaintEventArgs e)
  39.         {
  40.  
  41.         }
  42.  
  43.         private void button1_Click(object sender, EventArgs e)
  44.         {
  45.             forel();
  46.  
  47.         }
  48.         public int kx, ky;
  49.         public void forel()
  50.         {
  51.             double R = (double)numericUpDown1.Value;
  52.             richTextBox1.Clear();
  53.             img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  54.             graphics = Graphics.FromImage(img);
  55.             pictureBox1.Image = img;
  56.             Brush brush = Brushes.White;
  57.             Pen pen = new Pen(Color.Red);
  58.             Pen pen1 = new Pen(Color.Purple);
  59.             //Brush brush1 = new SolidColorBrush(color);
  60.             graphics.FillRectangle(brush, 0, 0, img.Width, img.Height);
  61.             kx = 20;
  62.             ky = 20;
  63.             List<point> X = generateArray(10);
  64.             drawPoints(X);
  65.             int k = 1;
  66.             do
  67.             {
  68.                 point center = getCenterOfMass(X);
  69.                 double radius = getFirstRadius(X, center);
  70.                 if (radius == 0)
  71.                 {
  72.                     radius = 1;
  73.                 }
  74.                 //graphics.DrawEllipse(pen1, (float)(center.X * kx - radius * kx), (float)(center.Y * ky - radius * ky),
  75.                     //(float)(2 * radius * kx), (float)(2 * radius * ky));
  76.                 point oldCenter = new point();
  77.                 List<point> clasterPoint;
  78.                
  79.                 do
  80.                 {
  81.                     clasterPoint = new List<point>();
  82.                     radius *= R;
  83.                     oldCenter = center;
  84.                     center = getRandomPoint(X);
  85.                     clasterPoint = getPointsIn(X, radius, center);
  86.                     center = getCenterOfMass(clasterPoint);
  87.                     //graphics.DrawEllipse(pen, (float)(center.X * kx - radius * kx), (float)(center.Y * ky - radius * ky),
  88.                        //(float)(2 * radius * kx), (float)(2 * radius * ky));
  89.                 } while (oldCenter.X != center.X && oldCenter.Y != center.Y);
  90.                 graphics.DrawEllipse(pen, (float)(center.X * kx - radius * kx), (float)(center.Y * ky - radius * ky),
  91.                        (float)(2 * radius * kx), (float)(2 * radius * ky));
  92.                 graphics.DrawString(k.ToString(), new Font("Arial", 10), Brushes.Lime, (float)center.X*kx, (float)center.Y*ky);
  93.  
  94.                 richTextBox1.AppendText("Кластер № " + k.ToString() + " содержит " + clasterPoint.Count.ToString() + " т.\n");
  95.                 for (int i = 0; i < clasterPoint.Count; i++)
  96.                 {
  97.                     richTextBox1.AppendText("(" + Math.Round(clasterPoint[i].X,3).ToString() + " ; " + Math.Round(clasterPoint[i].Y,3).ToString() + ")\n");
  98.                 }
  99.                 X = clean(radius, X, center);
  100.                 k++;
  101.             } while (X.Count != 0);
  102.         }
  103.         public List<point> clean(double uR, List<point> X, point center)
  104.         {
  105.             for (int i = 0; i < X.Count; i++)
  106.             {
  107.                 if (Math.Sqrt(Math.Pow(X[i].X - center.X, 2) + Math.Pow(X[i].Y - center.Y, 2)) <= uR)
  108.                     X.RemoveAt(i);
  109.             }
  110.             return X;
  111.         }
  112.         public point getCenterOfMass(List<point> Array)
  113.         {
  114.             point center = new point();
  115.             double sumX = 0;
  116.             double sumY = 0;
  117.             for (int i = 0; i < Array.Count; i++)
  118.             {
  119.                 sumX += Array[i].X;
  120.                 sumY += Array[i].Y;
  121.             }
  122.             center.X = sumX / Array.Count;
  123.             center.Y = sumY / Array.Count;
  124.             return center;
  125.         }
  126.         //возвращает массив точек принадлежащих окружности
  127.         public List<point> getPointsIn(List<point> X, double uR, point center)
  128.         {
  129.             List<point> new_array = new List<point>();
  130.             for (int i = 0; i < X.Count; i++)
  131.             {
  132.                 if (Math.Sqrt(Math.Pow(X[i].X - center.X, 2) + Math.Pow(X[i].Y - center.Y, 2)) <= uR)
  133.                     new_array.Add(new point(X[i].X, X[i].Y));
  134.             }
  135.             return new_array;
  136.         }
  137.         public point getRandomPoint(List<point> Array)
  138.         {
  139.             point point = new point();
  140.             Random random = new Random();
  141.             int randomIndex = random.Next(0, Array.Count);
  142.             point.X = Array[randomIndex].X;
  143.             point.Y = Array[randomIndex].Y;
  144.             return point;
  145.         }
  146.         public double getFirstRadius(List<point> X, point center)
  147.         {
  148.             double radius = 0;
  149.             for (int i = 0; i < X.Count; i++)
  150.             {
  151.                 if (radius < Math.Sqrt(Math.Pow(center.X - X[i].X, 2) + Math.Pow(center.Y - X[i].Y, 2)))
  152.                 {
  153.                     radius = Math.Sqrt(Math.Pow(center.X - X[i].X, 2) + Math.Pow(center.Y - X[i].Y, 2));
  154.                 }
  155.             }
  156.             return radius;
  157.         }
  158.         public List<point> generateArray(int count)
  159.         {
  160.             Random random = new Random();
  161.             List<point> Array = new List<point>();
  162.             for (int i = 0; i < count; i++)
  163.             {
  164.                 point P = new point(13 * random.NextDouble() + 11, 17 * random.NextDouble() + 9);
  165.                 Array.Add(P);
  166.             }
  167.             return Array;
  168.         }
  169.         public void drawPoints(List<point> Array)
  170.         {
  171.             for (int i = 0; i < Array.Count; i++)
  172.                 graphics.FillEllipse(Brushes.Green, (float)(Array[i].X * kx - 2), (float)(Array[i].Y * ky - 2), 4, 4);
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement