Advertisement
Koelion

Untitled

Nov 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 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.  
  7. namespace Line
  8. {
  9.     public class Bresenhama
  10.     {
  11.         public Point[] Points;
  12.         public Bresenhama(Point start, Point end)
  13.         {
  14.             CalcPoints(start, end, 4);
  15.         }
  16.  
  17.         private static void Swap<T>(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; }
  18.         private void CalcPoints(Point start, Point end, int width)
  19.         {
  20.             List<Point> points = new List<Point>();
  21.  
  22.             int x0 = start.X;
  23.             int y0 = start.Y;
  24.             int x1 = end.X;
  25.             int y1 = end.Y;
  26.  
  27.             bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
  28.             if (steep) { Swap<int>(ref x0, ref y0); Swap<int>(ref x1, ref y1); }
  29.             if (x0 > x1) { Swap<int>(ref x0, ref x1); Swap<int>(ref y0, ref y1); }
  30.             int dX = (x1 - x0), dY = Math.Abs(y1 - y0), err = (dX / 2), ystep = (y0 < y1 ? 1 : -1), y = y0;
  31.  
  32.             width = width / 2;
  33.  
  34.             for (int x = x0; x <= x1; ++x)
  35.             {
  36.                 if (steep)
  37.                 {
  38.                     points.Add(new Point(y, x));
  39.                     for(int i=1; i< width; i++)
  40.                     {
  41.                         points.Add(new Point(y+i, x));
  42.                         points.Add(new Point(y-i, x));
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     points.Add(new Point(x, y));
  48.                     for (int i = 1; i < width; i++)
  49.                     {
  50.                         points.Add(new Point(x, y + i));
  51.                         points.Add(new Point(x, y - i));
  52.                     }
  53.                 }
  54.                 err = err - dY;
  55.                 if (err < 0) { y += ystep; err += dX; }
  56.             }
  57.  
  58.             this.Points = points.ToArray();
  59.  
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement