//вычисление вершин окружностей void calcVert(double rad) { for (int i = 0; i < ArrVertex.Count(); i++) { GraphV[i] = new GraphVertex((rad * Math.Sin(i * 2 * Math.PI / ArrVertex.Count()) + pictureBox.Width / 2) - VertRad, (rad * Math.Cos(i * 2 * Math.PI / ArrVertex.Count()) + pictureBox.Height / 2) - VertRad); } }
private void Graph_Load(object sender, EventArgs e) { //рисование графа GraphV = new GraphVertex[ArrVertex.Count()]; double radius = calcRad(); Pen pen = new Pen(Color.Black); Pen OutputPen = new Pen(Color.Red); Pen pen2 = new Pen(Color.Black); pen2.StartCap = System.Drawing.Drawing2D.LineCap.Triangle; Brush br = Brushes.Black; pen.Width = 5; OutputPen.Width = 5; pen2.Width = 1.5f; bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); graph = new Bitmap(pictureBox.Image); Graphics g = Graphics.FromImage(graph); calcVert(radius); Brush wh = Brushes.White; createEdges(); for (int i = 0; i < Edges.Count(); i++) { g.DrawLine(pen2, (float)(Edges[i].v1.x + VertRad), (float)(Edges[i].v1.y + VertRad), (float)(Edges[i].v2.x + VertRad), (float)(Edges[i].v2.y + VertRad)); } for (int i = 0; i < GraphV.Count(); i++) { //рисуем круги if (ArrVertex[i].isOutput) { g.DrawEllipse(OutputPen, (float)GraphV[i].x, (float)GraphV[i].y, (float)VertRad * 2, (float)VertRad * 2); } else { g.DrawEllipse(pen, (float)GraphV[i].x, (float)GraphV[i].y, (float)VertRad * 2, (float)VertRad * 2); } g.FillEllipse(wh, (float)GraphV[i].x, (float)GraphV[i].y, (float)VertRad * 2, (float)VertRad * 2); g.DrawString("C" + (i + 1).ToString(), new Font("Microsoft Sans Serif", 8.5F, FontStyle.Regular), br, (float)(GraphV[i].x + VertRad / 4), (float)(GraphV[i].y + VertRad / 4)); } pictureBox.Image = graph; }
//вычисление адиуса большой окружности double calcRad() { double rad = 20; double VertDiag; VertDiag = Math.Sqrt(VertRad * VertRad + VertRad * VertRad); double x1, y1, x2, y2, y, x, k = 0; //проверка каого-то правила с полудиагональю while (k < VertDiag * 4) { rad *= 1.2; //нахождение оптимального радиуса x1 = rad * Math.Sin(2 * Math.PI / ArrVertex.Count()) + pictureBox.Width / 2; y1 = rad * Math.Cos(2 * Math.PI / ArrVertex.Count()) + pictureBox.Height / 2; x2 = rad * Math.Sin(2 * 2 * Math.PI / ArrVertex.Count()) + pictureBox.Width / 2; y2 = rad * Math.Cos(2 * 2 * Math.PI / ArrVertex.Count()) + pictureBox.Height / 2; x = Math.Abs(x1) - Math.Abs(x2); y = Math.Abs(y1) - Math.Abs(y2); k = Math.Sqrt(x * x + y * y); } return(rad); }