示例#1
0
        private void Field_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((cursor.IsChecked == true))
            {
                List <Vertex> vertices = new List <Vertex> {
                    new Vertex("школа", 150, 100), new Vertex("дом", 100, 60)
                };
                //List<Edge> edges = new List<Edge> { new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[0],
                //    vertices[1]),new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[0], vertices[1]),
                //    new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[0], vertices[1]), new Edge("d", vertices[1], vertices[2]),new Edge("d", vertices[2], vertices[2]),new Edge("d", vertices[2], vertices[1]) };

                List <Edge> edges = new List <Edge> {
                    new Edge("f", vertices[0], vertices[1]), new Edge("f", vertices[0], vertices[1]), new Edge("f", vertices[0], vertices[1])
                };

                DrawGraph drgr = new DrawGraph(field, new Graph(vertices, edges));
                drgr.Draw();



                //Vertex vertex = new Vertex("ee", 350, 150);
                //Vertex vertex2 = new Vertex("lol", 550, 250);
                //List<Vertex> vertices = new List<Vertex> { vertex, vertex2 };
                //List<Edge> edges = new List<Edge> { new Edge("d", vertex, vertex), new Edge("d", vertex2, vertex2), new Edge("t", vertex, vertex2) };

                //DrawGraph dr = new DrawGraph(field, new Graph(vertices, edges));
                //dr.Draw();
            }
            else if (vertex.IsChecked == true)
            {
                Ellipse el = EllipseFab.GetEllipse(new Point(e.GetPosition(field).X, e.GetPosition(field).Y), new Point(0, 0), Brushes.Red, "uu");

                el.MouseDown += new MouseButtonEventHandler(Ellipse_MouseDown);

                // el.MouseMove += new MouseEventHandler(mouse_Move);
                // el.MouseUp += new MouseButtonEventHandler(mouse_Up);

                field.Children.Add(el);
                var path = BezPathFab.GetPath(new Point(e.GetPosition(field).X, e.GetPosition(field).Y), new Point((e.GetPosition(field).X + 50) / 2 + 20,
                                                                                                                   (e.GetPosition(field).Y + 100) / 2 - 50), new Point(e.GetPosition(field).X - 50, e.GetPosition(field).Y - 75), Brushes.Red, "", true);

                path.MouseDown += new MouseButtonEventHandler(Path_MouseDown);
                field.Children.Add(path);



                //Path.
                //field.Children.
                //new Grid().Children.Add();
            }

            else if (hand.IsChecked == true)
            {
            }
        }
示例#2
0
        // рисуй все ключи (вершины), а затем только рисуй ребра, проходя по значениям
        public void Draw()
        {
            panel.Children.Clear();

            // идем по графу и рисуем
            var data = graph.GetGraph();

            Vertex[] dataKeys = data.Keys.ToArray();

            // рисуем вершины
            foreach (var vertex in dataKeys)
            {
                panel.Children.Add(EllipseFab.GetEllipse(new Point(vertex.X, vertex.Y), new Point(0, 0), brush, vertex.Name));
            }

            // рисуем ребра
            List <Edge> temp = new List <Edge>();

            for (int j = 0; j < dataKeys.Length - 1; j++)
            {
                for (int i = j + 1; i < dataKeys.Length; i++)      // к последнему нет смысла обращаться
                {
                    foreach (var edge in data[dataKeys[j]])
                    {
                        if (edge.To == dataKeys[i])     //ReferenceEqual    // сравниваем попарно (точно знаем, что from это ключ)
                        {
                            temp.Add(edge);
                        }
                    }

                    // в обратную сторону
                    foreach (var edge in data[dataKeys[i]])
                    {
                        if (edge.To == dataKeys[j])
                        {
                            temp.Add(edge);
                        }
                    }

                    Point middle = new Point();
                    for (int k = 0; k < temp.Count; k++)
                    {
                        // формируем middle точку
                        if (k == 0)
                        {
                            middle.X = (temp[k].To.X + temp[k].From.X) / 2;
                            middle.Y = (temp[k].To.Y + temp[k].From.Y) / 2;
                        }
                        else if (k % 2 != 0)    // нечетные
                        {
                            middle.X = (temp[k].To.X + temp[k].From.X) / 2 - k * 15 - 10;
                            middle.Y = (temp[k].To.Y + temp[k].From.Y) / 2;
                        }
                        else
                        {
                            middle.X = (temp[k].To.X + temp[k].From.X) / 2 + (k - 1) * 15 + 10;
                            middle.Y = (temp[k].To.Y + temp[k].From.Y) / 2;
                        }

                        panel.Children.Add(BezPathFab.GetPath(new Point(temp[k].From.X, temp[k].From.Y), middle,
                                                              new Point(temp[k].To.X, temp[k].To.Y), brush, temp[k].Name, temp[k].Orient));
                    }

                    temp.Clear();
                }
            }

            //собираем петли                        // можно оптимизировать
            foreach (var key in data.Keys)
            {
                foreach (var edge in data[key])
                {
                    if (edge.From == edge.To)
                    {
                        panel.Children.Add(BezPathFab.GetPath(new Point(edge.From.X - 15, edge.From.Y - 15), new Point(edge.To.X - 70,
                                                                                                                       edge.To.Y + 70), new Point(edge.To.X + 15, edge.To.Y + 15), brush, edge.Name, false));

                        break;                      //петля для данной вершины всегда одна
                    }
                }
            }
        }