public void LineEdgeMouseClick(object sender, MouseButtonEventArgs e) { //StringActionUndo(); Petzold.Media2D.ArrowLine line = new Petzold.Media2D.ArrowLine(); line = (Petzold.Media2D.ArrowLine)sender; if (DeleteingEdgeEnabled) { MainWindow_.InfoCurr.Clear(); RemoveEdge(line); DeleteingEdgeEnabled = false; MyCursors.DefaultCursor(); } if (IsOriented) { foreach (var edge in EdgesList) { if (edge.line == line) { GraphVertex StartV = edge.StartVertex; GraphVertex EndV = edge.EndVertex; ConnectVertex(EndV, StartV); RemoveEdge(line); break; } } } }
public GraphEdge(GraphVertex startV, GraphVertex endV, bool oriented, TextBox text, double w) { line = new ArrowLine(); this.StartVertex = startV; this.EndVertex = endV; line.Stroke = System.Windows.Media.Brushes.Black; this.line.X1 = Canvas.GetLeft(startV.ellipse) + startV.ellipse.ActualWidth / 2; this.line.Y1 = Canvas.GetTop(startV.ellipse) + startV.ellipse.ActualHeight / 2; this.line.X2 = Canvas.GetLeft(endV.ellipse) + endV.ellipse.ActualWidth / 2; this.line.Y2 = Canvas.GetTop(endV.ellipse) + endV.ellipse.ActualHeight / 2; double u_l = Math.Atan2(line.X1 - line.X2, line.Y1 - line.Y2); double u = Math.PI / 33; line.X1 = line.X1 + (-20) * Math.Sin(u_l); line.Y1 = line.Y1 + (-20) * Math.Cos(u_l); line.X2 = line.X2 + (20) * Math.Sin(u_l); line.Y2 = line.Y2 + (20) * Math.Cos(u_l); if (oriented) { this.line.ArrowEnds = ArrowEnds.End; } else { this.line.ArrowEnds = ArrowEnds.None; } line.StrokeThickness = 3; line.MouseEnter += new System.Windows.Input.MouseEventHandler(EdgeLineMouseEnter); line.MouseLeave += new System.Windows.Input.MouseEventHandler(EdgeLineMouseLeave); line.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(EdgeClick_empty_); WeightEdge = w; textBox = text; line.Tag = startV.ellipse.Tag.ToString() + ";" + endV.ellipse.Tag.ToString(); }
public void ReadEdge(Petzold.Media2D.ArrowLine line, TextBox textBox, GraphVertex v1, GraphVertex v2) { textBox.TextChanged += new System.Windows.Controls.TextChangedEventHandler(ChangeEdgeWeight); textBox.KeyUp += new KeyEventHandler(KeyEnterClickTextBox); textBox.MouseEnter += new MouseEventHandler(IsMouseOnTextBox); textBox.MouseDoubleClick += new MouseButtonEventHandler(TextBoxVertexDoubleClick); textBox.MouseMove += new MouseEventHandler(MouseMoveOnTextBox); GraphEdge tempEdge = new GraphEdge(line, textBox, Convert.ToDouble(textBox.Text), v1, v2); foreach (var t in VertexesList) { if (t.ellipse == v1.ellipse) { t.lines.Add(tempEdge); } if (t.ellipse == v2.ellipse) { t.lines.Add(tempEdge); } } tempEdge.line.MouseLeftButtonUp += new MouseButtonEventHandler(LineEdgeMouseClick); GraphPlain.Children.Add(tempEdge.line); GraphPlain.Children.Add(textBox); EdgesList.Add(tempEdge); }
public GraphEdge(ArrowLine line_, TextBox text, double w, GraphVertex v1, GraphVertex v2) { line = line_; this.StartVertex = v1; this.EndVertex = v2; line.MouseEnter += new System.Windows.Input.MouseEventHandler(EdgeLineMouseEnter); line.MouseLeave += new System.Windows.Input.MouseEventHandler(EdgeLineMouseLeave); WeightEdge = w; textBox = text; }
private static double EdgeCostSearching(GraphVertex Source, GraphVertex Target, List <GraphEdge> listEdges) { foreach (var edge in listEdges) { if ((edge.StartVertex == Source && edge.EndVertex == Target) || (edge.StartVertex == Target && edge.EndVertex == Source)) { return(edge.WeightEdge); } } return(0); }
public static string ShortestWayDijsktraAlgorithmUnDirected(GraphVertex vertexD, List <GraphEdge> listEdge, List <GraphVertex> listVertex) { string s = ""; UndirectedGraph <GraphVertex, UndirectedEdge <GraphVertex> > graph = new UndirectedGraph <GraphVertex, UndirectedEdge <GraphVertex> >(); foreach (var vert in listVertex) { graph.AddVertex(vert); } foreach (var edge in listEdge) { graph.AddEdge(new UndirectedEdge <GraphVertex>(edge.StartVertex, edge.EndVertex)); } Dictionary <UndirectedEdge <GraphVertex>, double> edgeCost = new Dictionary <UndirectedEdge <GraphVertex>, double>(); int i = 0; foreach (var edge in graph.Edges) { double eCost = EdgeCostSearching(edge.Source, edge.Target, listEdge); edgeCost.Add(edge, eCost); i++; } IEnumerable <UndirectedEdge <GraphVertex> > pathh; Func <UndirectedEdge <GraphVertex>, double> getW = edge => edgeCost[edge]; UndirectedDijkstraShortestPathAlgorithm <GraphVertex, UndirectedEdge <GraphVertex> > diijkstra = new UndirectedDijkstraShortestPathAlgorithm <GraphVertex, UndirectedEdge <GraphVertex> >(graph, getW); UndirectedVertexDistanceRecorderObserver <GraphVertex, UndirectedEdge <GraphVertex> > distObs = new UndirectedVertexDistanceRecorderObserver <GraphVertex, UndirectedEdge <GraphVertex> >(getW); using (distObs.Attach(diijkstra)) { UndirectedVertexPredecessorRecorderObserver <GraphVertex, UndirectedEdge <GraphVertex> > predObs = new UndirectedVertexPredecessorRecorderObserver <GraphVertex, UndirectedEdge <GraphVertex> >(); using (predObs.Attach(diijkstra)) { diijkstra.Compute(vertexD); foreach (KeyValuePair <GraphVertex, double> kvp in distObs.Distances) { s += "From " + vertexD.Name + " to " + kvp.Key.Name + " is " + kvp.Value + " by "; if (predObs.TryGetPath(kvp.Key, out pathh)) { foreach (var t in pathh) { s += "edge " + t.Source.Name + "<->" + t.Target.Name + " "; } } s += System.Environment.NewLine; } } } return(s); }
public void ConnectVertex(GraphVertex e1, GraphVertex e2) { if (e1 == e2) { return; } if (EdgeAlreadyExist(e1, e2)) { Xceed.Wpf.Toolkit.MessageBox.Show("Ребро уже было добавлено!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } TextBox textBox = new System.Windows.Controls.TextBox(); GraphEdge tempEdge_ = new GraphEdge(e1, e2, IsOriented, textBox, 0); RewriteTextBoxEdge(tempEdge_, textBox); textBox.TextChanged += new System.Windows.Controls.TextChangedEventHandler(ChangeEdgeWeight); textBox.KeyUp += new KeyEventHandler(KeyEnterClickTextBox); textBox.CaretBrush = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); textBox.Width = 30; textBox.Height = 25; textBox.FontSize = 16; textBox.FontWeight = FontWeights.Bold; var color = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); textBox.Background = Brushes.White; textBox.BorderThickness = new Thickness(0, 0, 0, 0); textBox.TextAlignment = TextAlignment.Center; textBox.Text = "1"; textBox.Tag = tempEdge_.line.Tag.ToString(); textBox.MouseEnter += new MouseEventHandler(IsMouseOnTextBox); textBox.MouseDoubleClick += new MouseButtonEventHandler(TextBoxVertexDoubleClick); textBox.MouseMove += new MouseEventHandler(MouseMoveOnTextBox); //textBox.MouseLeftButtonUp += new MouseButtonEventHandler(MouseLeftDown); GraphEdge tempEdge = new GraphEdge(e1, e2, IsOriented, textBox, Convert.ToDouble(textBox.Text)); foreach (var t in VertexesList) { if (t.ellipse == e1.ellipse) { t.lines.Add(tempEdge); } if (t.ellipse == e2.ellipse) { t.lines.Add(tempEdge); } } tempEdge.line.MouseLeftButtonUp += new MouseButtonEventHandler(LineEdgeMouseClick); GraphPlain.Children.Add(tempEdge.line); GraphPlain.Children.Add(textBox); EdgesList.Add(tempEdge); }
private bool EdgeAlreadyExist(GraphVertex v1, GraphVertex v2) { if (AddEdgeEnabled) { foreach (var edge in EdgesList) { if ((edge.StartVertex == v1 && edge.EndVertex == v2) || (edge.StartVertex == v2 && edge.EndVertex == v1)) { return(true); } } } return(false); }
public void SearchShortestPathAStar(GraphVertex startVertex, GraphVertex endVertex) { var ASTAR = Algorithms.ShortestWayAstarAlgorithm(VertexesList, EdgesList, startVertex, endVertex); if (ASTAR != null) { RecolorByAstar(ASTAR); IsHighLighted = true; } else { Xceed.Wpf.Toolkit.MessageBox.Show("Пути не нашёл((", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } //IsEulirianRun = false; }
public void ShortestPathDijkstra(GraphVertex vertexD) { if (!IsOriented) { string result = Algorithms.ShortestWayDijsktraAlgorithmUnDirected(vertexD, EdgesList, VertexesList); Xceed.Wpf.Toolkit.MessageBox.Show(result, "Результат Деисктры", MessageBoxButton.OK, MessageBoxImage.Information); DijkstraEnabled = false; MainWindow_.InfoCurr.Clear(); } else { string result = Algorithms.ShortestWayDijsktraAlgorithmDirected(vertexD, EdgesList, VertexesList); Xceed.Wpf.Toolkit.MessageBox.Show(result, "Результат Деисктры", MessageBoxButton.OK, MessageBoxImage.Information); DijkstraEnabled = false; MainWindow_.InfoCurr.Clear(); } }
public static IEnumerable <Edge <GraphVertex> > ShortestWayAstarAlgorithm(List <GraphVertex> listVertex, List <GraphEdge> listEdge, GraphVertex start, GraphVertex end) { AdjacencyGraph <GraphVertex, Edge <GraphVertex> > graph = new AdjacencyGraph <GraphVertex, Edge <GraphVertex> >(); foreach (var vert in listVertex) { graph.AddVertex(vert); } foreach (var edge in listEdge) { graph.AddEdge(new Edge <GraphVertex>(edge.StartVertex, edge.EndVertex)); } Dictionary <Edge <GraphVertex>, double> edgeCost = new Dictionary <Edge <GraphVertex>, double>(); int i = 0; foreach (var edge in graph.Edges) { double eCost = EdgeCostSearching(edge.Source, edge.Target, listEdge); edgeCost.Add(edge, eCost); i++; } Func <Edge <GraphVertex>, double> getW = edge => edgeCost[edge]; //--------------------------------- IEnumerable <Edge <GraphVertex> > edgessAstar; AStarShortestPathAlgorithm <GraphVertex, Edge <GraphVertex> > astar = new AStarShortestPathAlgorithm <GraphVertex, Edge <GraphVertex> >(graph, getW, x => 0.0); VertexDistanceRecorderObserver <GraphVertex, Edge <GraphVertex> > distObsA = new VertexDistanceRecorderObserver <GraphVertex, Edge <GraphVertex> >(getW); using (distObsA.Attach(astar)) { VertexPredecessorRecorderObserver <GraphVertex, Edge <GraphVertex> > predObs = new VertexPredecessorRecorderObserver <GraphVertex, Edge <GraphVertex> >(); using (predObs.Attach(astar)) { astar.Compute(start); if (predObs.TryGetPath(end, out edgessAstar)) { return(edgessAstar); } } } return(null); }