using Microsoft.Xna.Framework; using System; using System.Collections.Generic; namespace SemiColinGames { public static class Line { public static void Rasterize(Point p1, Point p2, IList result) { Line.Rasterize(p1.X, p1.Y, p2.X, p2.Y, result); } // Rasterizes a line using Bresenham's line-drawing algorithm. // // References: // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm // http://members.chello.at/~easyfilter/bresenham.html // http://members.chello.at/~easyfilter/Bresenham.pdf (section 1.6) public static void Rasterize(int x1, int y1, int x2, int y2, IList result) { int dx = Math.Abs(x2 - x1); int stepX = x1 < x2 ? 1 : -1; int dy = -Math.Abs(y2 - y1); int stepY = y1 < y2 ? 1 : -1; int error = dx + dy; int errorXY; // Error value e_xy from the PDF. int i = 0; result.Clear(); result.Add(new Point(x1, y1)); while (x1 != x2 || y1 != y2) { i++; errorXY = 2 * error; if (errorXY >= dy) { // e_xy + e_x > 0 error += dy; x1 += stepX; } if (errorXY <= dx) { // e_xy + e_y < 0 error += dx; y1 += stepY; } result.Add(new Point(x1, y1)); } } } }