Advertisement
shoodymon

TreeDrawer.cs

May 5th, 2024
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows.Shapes;
  10.  
  11. namespace Trees_CourseProject
  12. {
  13.     public class TreeDrawer
  14.     {
  15.         private Canvas canvas;
  16.  
  17.         public TreeDrawer(Canvas canvas)
  18.         {
  19.             this.canvas = canvas;
  20.         }
  21.  
  22.         private void DrawNode(int nodeValue, double x, double y, Brush color)
  23.         {
  24.             // Отрисовываем узел
  25.             Ellipse ellipse = new Ellipse();
  26.             ellipse.Width = 35;
  27.             ellipse.Height = 35;
  28.             ellipse.Stroke = Brushes.Black;
  29.             ellipse.Fill = color;
  30.             Canvas.SetLeft(ellipse, x - ellipse.Width / 2);
  31.             Canvas.SetTop(ellipse, y);
  32.  
  33.             // Отрисовываем значение узла
  34.             Border border = new Border();
  35.             border.Child = new TextBlock
  36.             {
  37.                 Text = nodeValue.ToString(),
  38.                 FontWeight = FontWeights.Bold,
  39.                 VerticalAlignment = System.Windows.VerticalAlignment.Center,
  40.                 HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
  41.                 FontSize = 16
  42.             };
  43.             Canvas.SetLeft(border, x - border.ActualWidth / 2 - 4.5);
  44.             Canvas.SetTop(border, y + ellipse.Height / 2 - border.ActualHeight / 2 - 10);
  45.  
  46.             // Добавляем узел на холст
  47.             canvas.Children.Add(ellipse);
  48.             canvas.Children.Add(border);
  49.         }
  50.  
  51.         private void DrawArrow(double x1, double y1, double x2, double y2, double arrowLength)
  52.         {
  53.             Line line = new Line();
  54.             line.Stroke = Brushes.Black;
  55.             line.X1 = x1;
  56.             line.Y1 = y1;
  57.             line.X2 = x2;
  58.             line.Y2 = y2;
  59.             canvas.Children.Add(line);
  60.  
  61.             // Вычисляем угол наклона линии
  62.             double theta = Math.Atan2(y2 - y1, x2 - x1);
  63.             double phi = Math.PI / 6;
  64.  
  65.             // Отрисовываем треугольник стрелки
  66.             Polygon polygon = new Polygon();
  67.             polygon.Fill = Brushes.Black;
  68.             PointCollection points = new PointCollection();
  69.             points.Add(new System.Windows.Point(x2, y2));
  70.             points.Add(new System.Windows.Point(x2 - arrowLength * Math.Cos(theta - phi), y2 - arrowLength * Math.Sin(theta - phi)));
  71.             points.Add(new System.Windows.Point(x2 - arrowLength * Math.Cos(theta + phi), y2 - arrowLength * Math.Sin(theta + phi)));
  72.             polygon.Points = points;
  73.             canvas.Children.Add(polygon);
  74.         }
  75.  
  76.         public void DrawBinarySearchTree(BinarySearchTree.TreeNode root, Canvas canvas)
  77.         {
  78.             canvas.Children.Clear(); // Очищаем холст перед отрисовкой нового дерева
  79.             if (root != null)
  80.             {
  81.                 DrawBST(root, canvas.ActualWidth / 2, 20, canvas.ActualWidth / 4, 80);
  82.             }
  83.         }
  84.  
  85.         public void DrawAVLTree(AVLTree.AVLNode root, Canvas canvas)
  86.         {
  87.             canvas.Children.Clear();
  88.             if (root != null)
  89.             {
  90.                 DrawAVL(root, canvas.ActualWidth / 2, 20, canvas.ActualWidth / 4, 80);
  91.             }
  92.         }
  93.  
  94.         public void DrawRedBlackTree(RedBlackTree.RBNode root, Canvas canvas)
  95.         {
  96.             canvas.Children.Clear();
  97.             if (root != null)
  98.             {
  99.                 DrawRB(root, canvas.ActualWidth / 2, 20, canvas.ActualWidth / 4, 80);
  100.             }
  101.         }
  102.  
  103.         private void DrawBST(BinarySearchTree.TreeNode node, double x, double y, double offsetX, double offsetY)
  104.         {
  105.             DrawNode(node.Value, x, y, Brushes.LightGreen);
  106.             if (node.Left != null)
  107.             {
  108.                 DrawBST(node.Left, x - offsetX, y + offsetY, offsetX / 2, offsetY);
  109.                 DrawArrow(x, y + 35, x - offsetX, y + offsetY, 10);
  110.             }
  111.             if (node.Right != null)
  112.             {
  113.                 DrawBST(node.Right, x + offsetX, y + offsetY, offsetX / 2, offsetY);
  114.                 DrawArrow(x, y + 35, x + offsetX, y + offsetY, 10);
  115.             }
  116.         }
  117.  
  118.         private void DrawAVL(AVLTree.AVLNode node, double x, double y, double offsetX, double offsetY)
  119.         {
  120.             DrawNode(node.Value, x, y, Brushes.LightBlue);
  121.             if (node.Left != null)
  122.             {
  123.                 DrawAVL(node.Left, x - offsetX, y + offsetY, offsetX / 2, offsetY);
  124.                 DrawArrow(x, y + 35, x - offsetX, y + offsetY, 10);
  125.             }
  126.             if (node.Right != null)
  127.             {
  128.                 DrawAVL(node.Right, x + offsetX, y + offsetY, offsetX / 2, offsetY);
  129.                 DrawArrow(x, y + 35, x + offsetX, y + offsetY, 10);
  130.             }
  131.         }
  132.  
  133.         private void DrawRB(RedBlackTree.RBNode node, double x, double y, double offsetX, double offsetY)
  134.         {
  135.             DrawNode(node.Value, x, y, node.NodeColor == RedBlackTree.Color.Black ? Brushes.LightGray : Brushes.Red);
  136.             if (node.Left != null)
  137.             {
  138.                 DrawRB(node.Left, x - offsetX, y + offsetY, offsetX / 2, offsetY);
  139.                 DrawArrow(x, y + 35, x - offsetX, y + offsetY, 10);
  140.             }
  141.             if (node.Right != null)
  142.             {
  143.                 DrawRB(node.Right, x + offsetX, y + offsetY, offsetX / 2, offsetY);
  144.                 DrawArrow(x, y + 35, x + offsetX, y + offsetY, 10);
  145.             }
  146.         }
  147.  
  148.     }
  149.  
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement