Example #1
0
        //Создаем графически граф
        private void DrowGraph(FindPath obj, Grid can)
        {
            //Создаем список вершина графа
            List <Ellipse> el = new List <Ellipse> {
            };

            //Размер вершин и расстояние между ними
            int lengthOfPath = numberOfVertex * 90,
                width        = 30,
                height       = 30;

            if (lengthOfPath > 2000)
            {
                lengthOfPath = 2000;
            }

            //Выводим вершины
            for (int i = 0; i < numberOfVertex; i++)
            {
                el.Add(new Ellipse());
                el[i].Width             = width;
                el[i].Height            = height;
                el[i].Fill              = Brushes.Black;
                el[i].VerticalAlignment = VerticalAlignment;

                Random rd   = new Random();
                double left = rd.Next(lengthOfPath) - (width / 2);
                double top  = rd.Next(lengthOfPath) - (height / 2);

                Canvas.SetLeft(el[i], left);
                Canvas.SetTop(el[i], top);
                Canvas c = new Canvas();

                c.Children.Add(el[i]);

                can.MinHeight = lengthOfPath;

                can.Children.Add(c);
            }

            int[,] array = new int[numberOfVertex, numberOfVertex];
            array        = obj.GetMatrix();

            //Проводим линии между вершинами
            for (int i = 0; i < numberOfVertex; i++)
            {
                for (int j = i + 1; j < numberOfVertex; j++)
                {
                    if (array[i, j] != 0)
                    {
                        Line ln = new Line();
                        ln.StrokeThickness = 1;
                        ln.Stroke          = Brushes.Black;
                        ln.X1 = Canvas.GetLeft(el[i]) + (width / 2);
                        ln.Y1 = Canvas.GetTop(el[i]) + (height / 2);
                        ln.X2 = Canvas.GetLeft(el[j]) + (width / 2);
                        ln.Y2 = Canvas.GetTop(el[j]) + (height / 2);
                        TextBox text = new TextBox();
                        text.Text        = $"{array[i, j]}";
                        text.FontSize    = 15;
                        text.Foreground  = Brushes.Black;
                        text.Background  = Brushes.Transparent;
                        text.BorderBrush = Brushes.Transparent;

                        if (Canvas.GetLeft(el[i]) > Canvas.GetLeft(el[j]))
                        {
                            Canvas.SetLeft(text, Canvas.GetLeft(el[j]) + (Canvas.GetLeft(el[i]) - Canvas.GetLeft(el[j])) / 2);
                        }
                        else
                        {
                            Canvas.SetLeft(text, Canvas.GetLeft(el[i]) + (Canvas.GetLeft(el[j]) - Canvas.GetLeft(el[i])) / 2);
                        }

                        if (Canvas.GetTop(el[i]) > Canvas.GetTop(el[j]))
                        {
                            Canvas.SetTop(text, Canvas.GetTop(el[j]) + (Canvas.GetTop(el[i]) - Canvas.GetTop(el[j])) / 2);
                        }
                        else
                        {
                            Canvas.SetTop(text, Canvas.GetTop(el[i]) + (Canvas.GetTop(el[j]) - Canvas.GetTop(el[i])) / 2);
                        }

                        Canvas c = new Canvas();
                        c.Children.Add(text);
                        can.Children.Add(c);
                        can.Children.Add(ln);
                    }
                }
            }

            //Подписываем вершины
            for (int i = 0; i < numberOfVertex; i++)
            {
                TextBox text = new TextBox();
                text.Text        = $"{i}";
                text.FontSize    = 15;
                text.Foreground  = Brushes.White;
                text.Background  = Brushes.Transparent;
                text.BorderBrush = Brushes.Transparent;

                Canvas.SetLeft(text, Canvas.GetLeft(el[i]) + (width / 4));
                Canvas.SetTop(text, Canvas.GetTop(el[i]) + (height / 4));

                Canvas c = new Canvas();
                c.Children.Add(text);
                can.Children.Add(c);
            }
        }