Построить график функции y=x^2. График строится из отрезков и алгоритма Брезенхема - C#

Узнай цену своей работы

Формулировка задачи:

Построить график функции y=x^2. График строится с помощью отрезков. Для построения отрезков использовать алгоритм Брезенхема.

Решение задачи: «Построить график функции y=x^2. График строится из отрезков и алгоритма Брезенхема»

textual
Листинг программы
  1. interface IPlotter
  2. {
  3.     void Plot(int x, int y);
  4. }
  5.  
  6. class BresenhamsOctantTransformation
  7. {
  8.     public Point SwitchToOctantZeroFrom(int octant, Point p)
  9.     {
  10.         switch (octant)
  11.         {
  12.             case 0: return new Point(p.X, p.Y);
  13.             case 1: return new Point(p.Y, p.X);
  14.             case 2: return new Point(p.Y, -p.X);
  15.             case 3: return new Point(-p.X, p.Y);
  16.             case 4: return new Point(-p.X, -p.Y);
  17.             case 5: return new Point(-p.Y, -p.X);
  18.             case 6: return new Point(-p.Y, p.X);
  19.             case 7: return new Point(p.X, -p.Y);
  20.             default: throw new ArgumentOutOfRangeException(nameof(octant));
  21.         }
  22.     }
  23.  
  24.     public Point SwitchFromOctantZeroTo(int octant, Point p)
  25.     {
  26.         switch (octant)
  27.         {
  28.             case 0: return new Point(p.X, p.Y);
  29.             case 1: return new Point(p.Y, p.X);
  30.             case 2: return new Point(-p.Y, p.X);
  31.             case 3: return new Point(-p.X, p.Y);
  32.             case 4: return new Point(-p.X, -p.Y);
  33.             case 5: return new Point(-p.Y, -p.X);
  34.             case 6: return new Point(p.Y, -p.X);
  35.             case 7: return new Point(p.X, -p.Y);
  36.             default: throw new ArgumentOutOfRangeException(nameof(octant));
  37.         }
  38.     }
  39. }
  40.  
  41. class BresenhamsPlotter : IPlotter
  42. {
  43.     private BresenhamsOctantTransformation _transformation;
  44.     private IPlotter _plotter;
  45.     private int _octant;
  46.    
  47.     public BresenhamsPlotter(int octant, IPlotter plotter, BresenhamsOctantTransformation trasformation)
  48.     {
  49.         _plotter = plotter;
  50.         _octant = octant;
  51.         _transformation = trasformation;
  52.     }
  53.    
  54.     public void Plot(int x, int y)
  55.     {
  56.         Point output = _transformation.SwitchFromOctantZeroTo(_octant, new Point(x, y));
  57.         _plotter.Plot(output.X, output.Y);
  58.     }
  59. }
  60.  
  61. class BresenhamsBasicAlgorithm
  62. {
  63.     public void DrawLine(Point start, Point end, IPlotter plotter)
  64.     {
  65.         if (Math.Abs(end.X - start.X) < 1e-6)
  66.         {
  67.             DrawVertical(start.X, start.Y, end.Y, plotter);
  68.             return;
  69.         }
  70.        
  71.         DrawLineImpl(start, end, plotter);
  72.     }
  73.    
  74.     private void DrawLineImpl(Point start, Point end, IPlotter plotter)
  75.     {
  76.         int dx = end.X - start.X;
  77.         int dy = end.Y - start.Y;
  78.        
  79.         int d = 2 * dy - dx;
  80.         int y = start.Y;
  81.        
  82.         for (int x = start.X; x <= end.X; x++)
  83.         {
  84.             plotter.Plot(x, y);
  85.            
  86.             if (d > 0)
  87.             {
  88.                 y = y + 1;
  89.                 d -= dx;
  90.             }
  91.            
  92.             d += dy;
  93.         }
  94.     }
  95.    
  96.     private void DrawVertical(int x, int yStart, int yEnd, IPlotter plotter)
  97.     {
  98.         int dy = yEnd > yStart ? 1 : -1;
  99.  
  100.         for (int y = yStart; true; y += dy)
  101.         {
  102.             plotter.Plot(x, y);
  103.            
  104.             if (y == yEnd) break;
  105.         }
  106.     }
  107. }
  108.  
  109. class BresenhamsOctantWiseAlgorithm
  110. {
  111.     private BresenhamsBasicAlgorithm _basicAlgorithm = new BresenhamsBasicAlgorithm();
  112.     private BresenhamsOctantTransformation _transformation = new BresenhamsOctantTransformation();
  113.    
  114.     public void DrawLine(Point start, Point end, IPlotter plotter)
  115.     {
  116.         if (start.X > end.X)
  117.         {
  118.             var temp = start;
  119.             start = end;
  120.             end = temp;
  121.         }
  122.        
  123.         int octant = GetOctant(end.X - start.X, end.Y - start.Y);
  124.        
  125.         start = _transformation.SwitchToOctantZeroFrom(octant, start);
  126.         end = _transformation.SwitchToOctantZeroFrom(octant, end);
  127.        
  128.         var bresenhamsPlotter = new BresenhamsPlotter(octant, plotter, _transformation);
  129.        
  130.         _basicAlgorithm.DrawLine(start, end, bresenhamsPlotter);
  131.     }
  132.  
  133.     private int GetOctant(int dx, int dy)
  134.     {
  135.         if (dy >= 0)
  136.             if (dy <= dx)
  137.                 return 0;
  138.             else
  139.                 return 1;
  140.         else
  141.             if (-dy <= dx)
  142.             return 7;
  143.         else
  144.             return 6;
  145.     }
  146. }
  147.  
  148. class StupidPlotter : IPlotter
  149. {
  150.     private Graphics _graphics;
  151.     private Pen _pen;
  152.    
  153.     public StupidPlotter(Graphics graphics, Pen pen)
  154.     {
  155.         _graphics = graphics;
  156.         _pen = pen;
  157.     }
  158.    
  159.     public void Plot(int x, int y)
  160.     {
  161.         _graphics.DrawEllipse(_pen, x - 0.1f, y - 0.1f, 0.2f, 0.2f);
  162.     }
  163. }
  164.  
  165. class MainForm : Form
  166. {
  167.     private Point _start;
  168.     private Point _end;
  169.     private bool _hold;
  170.    
  171.     public MainForm()
  172.     {
  173.         base.DoubleBuffered = true;
  174.        
  175.         MouseDown += (_, e) =>
  176.         {
  177.             if (e.Button != MouseButtons.Left) return;
  178.            
  179.             _hold = true;
  180.             _start = _end = e.Location;
  181.            
  182.             Invalidate();
  183.         };
  184.  
  185.         MouseUp += (_, e) =>
  186.         {
  187.             if (e.Button != MouseButtons.Left || !_hold) return;
  188.            
  189.             _hold = false;
  190.             _end = e.Location;
  191.            
  192.             Invalidate();
  193.         };
  194.  
  195.         MouseMove += (_, e) =>
  196.         {
  197.             if (!_hold) return;
  198.            
  199.             _end = e.Location;
  200.            
  201.             Invalidate();
  202.         };
  203.     }
  204.    
  205.     protected override void OnPaint(PaintEventArgs e)
  206.     {
  207.         var plotter = new StupidPlotter(e.Graphics, Pens.Red);
  208.         var bresenhamsAlgorithm = new BresenhamsOctantWiseAlgorithm();
  209.         bresenhamsAlgorithm.DrawLine(_start, _end, plotter);
  210.     }
  211. }
  212.  
  213. void Main()
  214. {
  215.     Application.Run(new MainForm());
  216. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 4.1 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы