sneak/Shared/Line.cs
Colin McMillen f1f717da63 remove unused using statement
GitOrigin-RevId: d3730b153c23fe158f1cd2bd3bd1f222d1a0bc46
2020-02-13 14:52:23 -05:00

46 lines
1.3 KiB
C#

using Microsoft.Xna.Framework;
using System;
namespace SemiColinGames {
class Line {
public static Point[] Rasterize(Point p1, Point p2) {
return Line.Rasterize(p1.X, p1.Y, p2.X, p2.Y);
}
// 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 Point[] Rasterize(int x1, int y1, int x2, int y2) {
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.
// The size of the output is the size of the longer dimension, plus one.
int resultSize = Math.Max(dx, -dy) + 1;
var result = new Point[resultSize];
int i = 0;
result[0] = 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[i] = new Point(x1, y1);
}
return result;
}
}
}