public Claster(ClasterPoint _MassPoint, Color color) { MassPoint = _MassPoint; ClasterColor = color; }
public void PushPoint(ClasterPoint point) { Points.Add(point); }
private void AddMassCenterClick(object sender, EventArgs e) { try { Match match = Regex.Match((sender as Button).Text, @"Сделать (.*?) точку"); int index = Convert.ToInt32(match.Groups[1].Value) - 1; bool IsAdded = false; bool IsSame = false; Random random = new Random(); for (int i = 0; i < clasters.Length; i++) { if (clasters[i] == null) { clasters[i] = new Claster(new ClasterPoint(points[index]), Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255))); for (int j = 0; j < clasters.Length; j++) { if (clasters[j] != null && j != i) { ClasterPoint CheckPoint = clasters[i].MassPoint; if (CheckPoint.x == clasters[j].MassPoint.x && CheckPoint.y == clasters[j].MassPoint.y) { IsSame = true; break; } } } if (!IsSame) { ClasterLabelData[i] = new Label(); ClasterLabelData[i].Text = $"Кластер {i + 1}"; ClasterLabelData[i].ForeColor = clasters[i].ClasterColor; ClasterLabelData[i].Location = StartClasterLabel.Location; clasters[i].TextBoxX = new TextBox(); clasters[i].TextBoxX.Text = clasters[i].MassPoint.x.ToString(); clasters[i].TextBoxX.Width = StartClasterCoard.Width; clasters[i].TextBoxX.Height = StartClasterCoard.Width; clasters[i].TextBoxX.ForeColor = clasters[i].ClasterColor; clasters[i].TextBoxX.Location = new System.Drawing.Point(StartClasterCoard.Location.X, StartClasterCoard.Location.Y); clasters[i].TextBoxY = new TextBox(); clasters[i].TextBoxY.Text = clasters[i].MassPoint.y.ToString(); clasters[i].TextBoxY.Width = StartClasterCoard.Width; clasters[i].TextBoxY.Height = StartClasterCoard.Width; clasters[i].TextBoxY.ForeColor = clasters[i].ClasterColor; clasters[i].TextBoxY.Location = new System.Drawing.Point(clasters[i].TextBoxX.Location.X + clasters[i].TextBoxX.Width + 3, clasters[i].TextBoxX.Location.Y); StartClasterCoard.Location = new Point(StartClasterCoard.Location.X, StartClasterCoard.Location.Y + StartClasterCoard.Height + 20); StartClasterLabel.Location = new Point(StartClasterLabel.Location.X, StartClasterLabel.Location.Y + StartClasterCoard.Height + 20); Controls.Add(clasters[i].TextBoxX); Controls.Add(clasters[i].TextBoxY); Controls.Add(ClasterLabelData[i]); IsAdded = true; Refresh(); break; } else { clasters[i] = null; MessageBox.Show("Данная точка уже выбрана центром масс", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning); break; } } } if (!IsAdded && !IsSame) { MessageBox.Show("Все центры масс выбраны", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public ClasterPoint(ClasterPoint copyPoint) { x = copyPoint.x; y = copyPoint.y; }
private void Generate_Click(object sender, EventArgs e) { Random random = new Random(); if (points == null || clasters == null) { try { points = new ClasterPoint[Convert.ToInt32(PointAmount.Text)]; clasters = new Claster[Convert.ToInt32(ClasterAmount.Text)]; PointDataX = new TextBox[points.Length]; PointDataY = new TextBox[points.Length]; PointLabelData = new Label[points.Length]; AddCenterMassBTN = new Button[points.Length]; ClasterLabelData = new Label[clasters.Length]; int Width = 100; int Height = 50; int LastPosX = StartPoint.Location.X; int LastPosY = StartPoint.Location.Y; for (int i = 0; i < points.Length; i++) { points[i] = new ClasterPoint((float)random.NextDouble(), (float)random.NextDouble(), clasters.Length); PointLabelData[i] = new Label(); PointLabelData[i].Text = (i + 1).ToString(); PointLabelData[i].Width = 20; PointLabelData[i].Height = 20; PointLabelData[i].Location = new System.Drawing.Point(LastPosX, LastPosY); PointDataX[i] = new TextBox(); PointDataX[i].Text = points[i].x.ToString(); PointDataX[i].Width = Width; PointDataX[i].Height = Height; PointDataX[i].Location = new System.Drawing.Point(PointLabelData[i].Location.X + 25 + 1, LastPosY); PointDataY[i] = new TextBox(); PointDataY[i].Text = points[i].y.ToString(); PointDataY[i].Width = Width; PointDataY[i].Height = Height; PointDataY[i].Location = new System.Drawing.Point(PointDataX[i].Location.X + Width + 1, LastPosY); AddCenterMassBTN[i] = new Button(); AddCenterMassBTN[i].Text = $"Сделать {i + 1} точку центром масс кластера"; AddCenterMassBTN[i].Width = Width + 100; AddCenterMassBTN[i].Height = Height / 2; AddCenterMassBTN[i].Click += AddMassCenterClick; AddCenterMassBTN[i].Location = new System.Drawing.Point(PointDataY[i].Location.X + Width + 1, LastPosY); LastPosY += (25 + 1); Controls.Add(PointLabelData[i]); Controls.Add(PointDataX[i]); Controls.Add(PointDataY[i]); Controls.Add(AddCenterMassBTN[i]); Application.DoEvents(); Refresh(); Thread.Sleep(1); } MessageBox.Show("Генерация выполнена", "Оповещение", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Очистите выборку", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }