Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Line
- {
- public class Bresenhama
- {
- public Point[] Points;
- public Bresenhama(Point start, Point end)
- {
- CalcPoints(start, end, 4);
- }
- private static void Swap<T>(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; }
- private void CalcPoints(Point start, Point end, int width)
- {
- List<Point> points = new List<Point>();
- int x0 = start.X;
- int y0 = start.Y;
- int x1 = end.X;
- int y1 = end.Y;
- bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
- if (steep) { Swap<int>(ref x0, ref y0); Swap<int>(ref x1, ref y1); }
- if (x0 > x1) { Swap<int>(ref x0, ref x1); Swap<int>(ref y0, ref y1); }
- int dX = (x1 - x0), dY = Math.Abs(y1 - y0), err = (dX / 2), ystep = (y0 < y1 ? 1 : -1), y = y0;
- width = width / 2;
- for (int x = x0; x <= x1; ++x)
- {
- if (steep)
- {
- points.Add(new Point(y, x));
- for(int i=1; i< width; i++)
- {
- points.Add(new Point(y+i, x));
- points.Add(new Point(y-i, x));
- }
- }
- else
- {
- points.Add(new Point(x, y));
- for (int i = 1; i < width; i++)
- {
- points.Add(new Point(x, y + i));
- points.Add(new Point(x, y - i));
- }
- }
- err = err - dY;
- if (err < 0) { y += ystep; err += dX; }
- }
- this.Points = points.ToArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement