private static PointF GetVertexTopLeftPoint(GraphVertex v) { var coordinates = v.Name.Split(':').Select(x => Int32.Parse(x.Trim())).ToArray(); var point = new PointF(coordinates[0] * 2 - 1, coordinates[1] * 2 - 1); return(point); }
private static void DrawVertex(GraphVertex v) { using (GraphicsPath capPath = new GraphicsPath()) { var color = ColorHelper.GetRandomColor(); var brush = new SolidBrush(color); var pen = new Pen(color, 0.03f); var point = GetVertexTopLeftPoint(v); var centerPoint = VertexCenterPoint(point); var rect = new RectangleF(point.X, point.Y, 1, 1); int edgeCounterH = 1; int edgeCounterV = 1; capPath.AddLine(-1, -2, 1, -2); capPath.AddLine(-1, -2, 0, 0); capPath.AddLine(1, -2, 0, 0); pen.CustomEndCap = new CustomLineCap(null, capPath); foreach (var e in v.Edges) { var connectionPoint = VertexCenterPoint(GetVertexTopLeftPoint(e.ConnectedVertex)); var from = centerPoint; var offsetV = 0.1f * edgeCounterV; var offsetH = 0.1f * edgeCounterH; if (connectionPoint.X > centerPoint.X) { from.X += VertexRadius; connectionPoint.X -= VertexRadius; edgeCounterV++; connectionPoint.Y += offsetV; from.Y += offsetV; } else if (connectionPoint.X < centerPoint.X) { from.X -= VertexRadius; connectionPoint.X += VertexRadius; } if (connectionPoint.Y > from.Y) { from.Y += VertexRadius; connectionPoint.Y -= VertexRadius; edgeCounterH++; connectionPoint.X += offsetH; from.X += offsetH; } else if ((connectionPoint.Y < from.Y)) { from.Y -= VertexRadius; connectionPoint.Y += VertexRadius; } Graphics.DrawLine(pen, from, connectionPoint); Graphics.DrawString(e.EdgeWeight.ToString(), EdgeWeightFont, brush, new RectangleF(from.X, from.Y, 2, 2)); } Graphics.DrawEllipse(pen, rect); Graphics.DrawString(v.Name, NodeNameFont, Brushes.Black, rect, StringFormat); } }
/// <summary> /// Получение информации о вершине графа /// </summary> /// <param name="v">Вершина</param> /// <returns>Информация о вершине</returns> private GraphVertexInfo GetVertexInfo(GraphVertex v) { foreach (var i in infos) { if (i.Vertex.Equals(v)) { return(i); } } return(null); }
/// <summary> /// Формирование пути /// </summary> /// <param name="startVertex">Начальная вершина</param> /// <param name="endVertex">Конечная вершина</param> /// <returns>Путь</returns> private string GetPath(GraphVertex startVertex, GraphVertex endVertex) { var path = endVertex.ToString() + ";"; while (startVertex != endVertex) { endVertex = GetVertexInfo(endVertex).PreviousVertex; path = endVertex.ToString() + ";" + path; } return(path); }
/// <summary> /// Поиск кратчайшего пути по вершинам /// </summary> /// <param name="startVertex">Стартовая вершина</param> /// <param name="finishVertex">Финишная вершина</param> /// <returns>Кратчайший путь</returns> public string FindShortestPath(GraphVertex startVertex, GraphVertex finishVertex) { InitInfo(); var first = GetVertexInfo(startVertex); first.EdgesWeightSum = 0; while (true) { var current = FindUnvisitedVertexWithMinSum(); if (current == null) { break; } SetSumToNextVertex(current); } return(GetPath(startVertex, finishVertex)); }
/// <summary> /// Добавить ребро /// </summary> /// <param name="vertex">Вершина</param> /// <param name="edgeWeight">Вес</param> public void AddEdge(GraphVertex vertex, int edgeWeight) { AddEdge(new GraphEdge(vertex, edgeWeight)); }