Draw a graph in a circle with a vertex in the middle

  Kiến thức lập trình
  1. The program will present directed and undirected graphs with specified parameters:
    • number of vertices n;
    • location of peaks;
    • adjacency matrix A.
  2. Create a program for creating an image of directed and undirected graphs in a graphics window.
    The above-mentioned parameters of the graph are set on the basis of the four-digit but mayor variant n1n2n3n4(n1=3,n2=4,n3=2,n4=7 The number of vertices n is equal to 10+n3
    Place the graphs in a circle with the vertex in the center of the circle it should look something like this, but now I only get this way.
    Here’s the code, help me if you can!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
namespace lab
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using WinFormsApp3;
 
    internal static class Program
    {
        class Graph
        {
            private int n;
            private int[,] adjacencyMatrix;
            private int n1, n2, n3, n4;
            public Graph(int n1, int n2, int n3, int n4)
            {
                this.n1 = n1;
                this.n2 = n2;
                this.n3 = n3;
                this.n4 = n4;
                n = 10;
                adjacencyMatrix = new int[n, n];
                GenerateAdjacencyMatrix();
            }
            private void GenerateAdjacencyMatrix()
            {
                Random rand = new Random(n1 * 1000 + n2 * 100 + n3 * 10 + n4);
                double k = 1.0 - n3 * 0.02 - n4 * 0.005 - 0.25;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        double randomValue = rand.NextDouble() * 2.0;
                        double multipliedValue = randomValue * k;
                        int roundedValue = (multipliedValue < 1.0) ? 0 : 1;
                        adjacencyMatrix[i, j] = roundedValue;
                    }
                }
            }
            public void DrawGraph(PaintEventArgs e)
            {
                int circleRadius = 150;
                int centerX = 300;
                int centerY = 300;
                int vertexSize = 30;
                double angleIncrement = 2 * Math.PI / n;
                double currentAngle = 0;
                for (int i = 0; i < n; i++)
                {
                    int x = (int)(centerX + circleRadius * Math.Cos(currentAngle));
                    int y = (int)(centerY + circleRadius * Math.Sin(currentAngle));
                    e.Graphics.FillEllipse(Brushes.Blue, x - vertexSize / 2, y - vertexSize / 2, vertexSize,
 
                    vertexSize);
 
                    e.Graphics.DrawString((i + 1).ToString(), new Font("Arial", 12), Brushes.Black, x + 6, y +
 
                    6);
 
                    currentAngle += angleIncrement;
                }
                for (int i = 0; i < n; i++)
 
                {
                    for (int j = i + 1; j < n; j++)
                    {
                        if (adjacencyMatrix[i, j] == 1)
                        {
                            int x1 = (int)(centerX + circleRadius * Math.Cos(i * angleIncrement));
                            int y1 = (int)(centerY + circleRadius * Math.Sin(i * angleIncrement));
                            int x2 = (int)(centerX + circleRadius * Math.Cos(j * angleIncrement));
                            int y2 = (int)(centerY + circleRadius * Math.Sin(j * angleIncrement));
                            e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2);
                        }
                    }
                }
            }
        }
        class MainForm : Form
        {
            private Graph graph;
            public MainForm(int n1, int n2, int n3, int n4)
            {
                graph = new Graph(n1, n2, n3, n4);
                this.Paint += MainForm_Paint;
            }
            private void MainForm_Paint(object sender, PaintEventArgs e)
            {
                graph.DrawGraph(e);
            }
        }
 
        static void Main()
        {
            int n1 = 3, n2 = 4, n3 = 2, n4 = 7;
            Application.Run(new MainForm(n1, n2, n3, n4));
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Помогите, пожалуйста, как-то исправить это, ибо я не силен в windows forms

New contributor

Aligat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT