public static void ChangeNodesPosition(RectangularGraph graph) { var numberNodes = NumberOfNodes(graph); MakeGraphFalse(graph); var matrix = new Matr <bool>(numberNodes, numberNodes); GetNetworkMatrix(graph, matrix); MakeGraphFalse(graph); var levels = SetLevels(matrix); var horizontalOrder = Barycenter(matrix, levels); var maxLevel = Maximum(levels); var maxHeights = new int[maxLevel + 1]; var rectangles = GetRectangles(graph); for (int i = 0; i <= maxLevel; i++) { maxHeights[i] = 0; var maxWidth = 0; var count = 0; for (int j = 0; j < numberNodes; j++) { if (levels[j] == i) { count++; if (maxHeights[i] < rectangles[j].Height) { maxHeights[i] = rectangles[j].Height; } if (maxWidth < rectangles[j].Width) { maxWidth = rectangles[j].Width; } } } int horizontalShift = (RectangularGraph.FormWidth - count * maxWidth) / (count + 1); for (int j = 0; j < numberNodes; j++) { if (levels[j] == i) { rectangles[j].Left = horizontalShift * (horizontalOrder[j] + 1) + horizontalOrder[j] * maxWidth; } } } var maxHeight = Maximum(maxHeights); var verticalShift = (RectangularGraph.FormHeigth - (maxLevel + 1) * maxHeight) / (maxLevel + 2); for (int i = 0; i <= maxLevel; i++) { for (int j = 0; j < numberNodes; j++) { if (levels[j] == i) { rectangles[j].Top = verticalShift * (i + 1) + i * maxHeight; } } } graph = CreateGraph(matrix, rectangles); }
public static void MakeGraphFalse(RectangularGraph graph) { graph.Flag = false; if (graph.NextElements == null) { return; } foreach (var nextElement in graph.NextElements) { MakeGraphFalse(nextElement); } }
public static void GetNetworkMatrix(RectangularGraph graph, Matr <bool> matrix) { if (graph.NextElements == null) { return; } foreach (var node in graph.NextElements) { matrix[graph.Content.Identificator, node.Content.Identificator] = true; if (node.Flag == false) { node.Flag = true; GetNetworkMatrix(node, matrix); } } }
public static int NumberOfNodes(RectangularGraph graph) { int result = 1; graph.Flag = true; if (graph.NextElements == null) { return(result); } foreach (var nextElement in graph.NextElements) { if (nextElement.Flag == false) { nextElement.Flag = true; result += NumberOfNodes(nextElement); } } return(result); }
public static void CreateGraphByStructure(GraphStructure structure, RectangularGraph graph) { structure.Flag = true; if (structure.Output != null) { foreach (var node in structure.Output) { if (!node.Flag) { RectangularGraph temp = new RectangularGraph(30, 30); temp.NextElements = new List <RectangularGraph>(); temp.Content.Identificator = node.Identificator; graph.NextElements.Add(temp); CreateGraphByStructure(node, temp); } } } else { graph.NextElements = null; } }
public static Rectangle[] GetRectangles(RectangularGraph graph) { List <Rectangle> temp = new List <Rectangle>(); temp.Add(graph.Content); graph.Flag = true; if (graph.NextElements == null) { temp.Sort(Rectangle.CompareByIdentificator); return(temp.ToArray()); } foreach (var nextElement in graph.NextElements) { if (nextElement.Flag == false) { nextElement.Flag = true; temp.AddRange(GetRectangles(nextElement)); } } temp.Sort(Rectangle.CompareByIdentificator); return(temp.ToArray()); }
public static RectangularGraph CreateGraph(Matr <bool> matrix, Rectangle[] rectangles) { RectangularGraph[] temp = new RectangularGraph[matrix.Columns]; for (int i = 0; i < matrix.Lines; i++) { temp[i] = new RectangularGraph(rectangles[i].Width, rectangles[i].Height); temp[i].Content.Left = 150 + 40 * (i % 3); temp[i].Content.Top = 150 + 40 * (i / 3); temp[i].Content.Identificator = i; } for (int i = 0; i < matrix.Lines; i++) { temp[i].NextElements = new List <RectangularGraph>(); for (int j = 0; j < matrix.Lines; j++) { if (matrix[i, j]) { temp[i].NextElements.Add(temp[j]); } } } return(temp[0]); }
public static RectangularGraph GenerateGraphStructure(int number, int maxLines) { var nodes = new GraphStructure[number]; for (int i = 0; i < number; i++) { nodes[i].Identificator = i; nodes[i].Input = new List <GraphStructure>(); nodes[i].Output = new List <GraphStructure>(); } var random = new Random(); for (int i = 1; i < number; i++) { var count = random.Next(maxLines); for (int j = 0; j < count; j++) { bool error; int maxiterations = 0; do { var value = random.Next(1, number); error = (value == i) || (nodes[i].Input.Contains(nodes[value])) || (nodes[i].Output.Contains(nodes[value])); if (!error) { nodes[i].Output.Add(nodes[value]); nodes[value].Input.Add(nodes[i]); UpdateInputs(nodes[value], nodes[i]); } maxiterations++; } while (error && maxiterations < 20); } } for (int i = 1; i < number; i++) { if (nodes[i].Input.Count == 0) { nodes[0].Output.Add(nodes[i]); } } MakeGraphStructureFalse(nodes[0]); var result = new RectangularGraph[number]; for (int i = 0; i < number; i++) { result[i] = new RectangularGraph(30, 30); } for (int i = 0; i < number; i++) { result[i].NextElements = new List <RectangularGraph>(); result[i].Content.Identificator = i; if (nodes[i].Output != null) { foreach (var node in nodes[i].Output) { result[i].NextElements.Add(result[node.Identificator]); } } } return(result[0]); }