コード例 #1
0
        private void WirePoints(object sender, RoutedEventArgs e)
        {
            GraphEdge edge = Graph.WireSelected();

            if (edge != null)
            {
                CanvasGrid.Children.Add(edge.EdgeLine);
                Canvas.SetZIndex(edge.EdgeLine, 0);
            }
        }
コード例 #2
0
ファイル: Graph.cs プロジェクト: Nan1t/Graphs
        public static GraphEdge Wire(GraphNode node1, GraphNode node2)
        {
            if (!IsWired(node1, node2))
            {
                Line line = new Line();
                line.StrokeThickness = 3;
                line.Stroke          = Brushes.Black;

                Binding bindX1 = new Binding {
                    Source = node1, Path = new PropertyPath(Canvas.LeftProperty)
                };
                Binding bindY1 = new Binding {
                    Source = node1, Path = new PropertyPath(Canvas.TopProperty)
                };
                Binding bindX2 = new Binding {
                    Source = node2, Path = new PropertyPath(Canvas.LeftProperty)
                };
                Binding bindY2 = new Binding {
                    Source = node2, Path = new PropertyPath(Canvas.TopProperty)
                };

                bindX1.Converter = new PosConverter();
                bindY1.Converter = new PosConverter();
                bindX2.Converter = new PosConverter();
                bindY2.Converter = new PosConverter();

                line.SetBinding(Line.X1Property, bindX1);
                line.SetBinding(Line.Y1Property, bindY1);
                line.SetBinding(Line.X2Property, bindX2);
                line.SetBinding(Line.Y2Property, bindY2);

                GraphEdge edge = new GraphEdge();
                edge.NodeFirst  = node1;
                edge.NodeSecond = node2;
                edge.EdgeLine   = line;

                Edges[node1].Add(edge);
                Edges[node2].Add(edge);

                Adjency[node1].Add(node2);
                Adjency[node2].Add(node1);

                return(edge);
            }

            return(null);
        }
コード例 #3
0
ファイル: AStarTest.cs プロジェクト: senchov/ECS
        private void AddFakeGraphEdges(Graph <NavGraphNode, GraphEdge> graph)
        {
            int nodeQuntity = graph.GetNodesQuantity();

            for (int i = 0; i < nodeQuntity; i++)
            {
                NavGraphNode outerNode = graph.GetNode(i);
                for (int j = 0; j < nodeQuntity; j++)
                {
                    NavGraphNode innerNode = graph.GetNode(j);
                    if (IsNodeConected(outerNode, innerNode))
                    {
                        GraphEdge edge = new GraphEdge(outerNode.Index, innerNode.Index, 0);
                        graph.AddEdge(edge, outerNode.Index);
                    }
                }

                //DebugConnectedNodes(graph, outerNode);
            }
        }
コード例 #4
0
        /*
         * Format
         * x,y,index; x,y,index; x,y,index; x,y,index - Vertexes
         * index|index,index,index; index|index,index - Adjency
         */

        public static void Load(string file)
        {
            Graph.Clear();

            StreamReader reader = new StreamReader(@file, true);
            string       line   = null;
            int          index  = 0;

            while ((line = reader.ReadLine()) != null)
            {
                if (index == 0) // Vertexes data
                {
                    string[] arr = line.Split(';');

                    for (int i = 0; i < arr.Length; i++)
                    {
                        string[] data = arr[i].Split('.');

                        if (data.Length < 3)
                        {
                            continue;
                        }

                        GraphNode node = new GraphNode();

                        node.Index = int.Parse(data[2]);
                        node.IndexLabel.Content = node.Index;

                        Canvas.SetLeft(node, double.Parse(data[0]));
                        Canvas.SetTop(node, double.Parse(data[1]));
                        MainWindow.GetInstance().CanvasGrid.Children.Add(node);

                        Canvas.SetZIndex(node, 1);
                        Graph.AddNode(node);
                    }
                }

                if (index == 1) // Adjency data
                {
                    string[] arr = line.Split(';');
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string[] keyData = arr[i].Split('|');

                        if (keyData.Length < 2)
                        {
                            continue;
                        }

                        int       key  = int.Parse(keyData[0]);
                        GraphNode node = Graph.GetNode(key);

                        if (node != null)
                        {
                            string[] neighbours = keyData[1].Split(',');

                            for (int j = 0; j < neighbours.Length - 1; j++)
                            {
                                int       nKey = int.Parse(neighbours[j]);
                                GraphNode n    = Graph.GetNode(nKey);

                                if (n != null)
                                {
                                    GraphEdge edge = Graph.Wire(node, n);

                                    if (edge != null)
                                    {
                                        MainWindow.GetInstance().CanvasGrid.Children.Add(edge.EdgeLine);
                                        Canvas.SetZIndex(edge.EdgeLine, 0);
                                    }
                                }
                            }
                        }
                    }
                }

                index++;
            }
        }
コード例 #5
0
ファイル: Graph.cs プロジェクト: Nan1t/Graphs
        public static void MakeSpanningTree(GraphNode root)
        {
            Canvas            canvas = MainWindow.GetInstance().CanvasGrid;
            Queue <GraphNode> queue  = new Queue <GraphNode>();

            queue.Enqueue(root);

            double distTop  = 10;
            double distLeft = canvas.ActualWidth / 2;

            while (queue.Count > 0)
            {
                GraphNode current = queue.Dequeue();

                if (!current.IsVisited)
                {
                    Canvas.SetTop(current, distTop);
                    Canvas.SetLeft(current, distLeft);
                    current.IsVisited = true;

                    Console.WriteLine("New level");
                }

                distTop += 64;

                foreach (GraphNode neighbour in Adjency[current])
                {
                    if (!neighbour.IsVisited)
                    {
                        neighbour.IsVisited = true;
                        queue.Enqueue(neighbour);

                        distLeft -= 64;

                        Canvas.SetTop(neighbour, distTop);
                        Canvas.SetLeft(neighbour, distLeft);

                        distLeft += 64 * 2;

                        List <GraphNode> toRemove = new List <GraphNode>();

                        foreach (GraphNode remainNode in Adjency[neighbour])
                        {
                            if (remainNode != current && remainNode.IsVisited)
                            {
                                GraphEdge edge = GetEdgeBetween(remainNode, neighbour);

                                if (edge != null)
                                {
                                    canvas.Children.Remove(edge.EdgeLine);
                                    canvas.Children.Remove(edge.WeightLabel);
                                    toRemove.Add(remainNode);
                                    Adjency[remainNode].Remove(neighbour);
                                    Edges[neighbour].Remove(edge);
                                    Edges[remainNode].Remove(edge);
                                }
                            }
                        }

                        foreach (GraphNode n in toRemove)
                        {
                            Adjency[neighbour].Remove(n);
                        }
                    }
                }
            }
        }
コード例 #6
0
 private void UpdateEdgeAdjacency(GraphEdge edge)
 {
     AdjacencyMatrix[edge.FromIndex][edge.ToIndex] = 1;
 }