getLine function

dynamic getLine(
  1. dynamic x0,
  2. dynamic y0,
  3. dynamic x1,
  4. dynamic y1,
)

Given the start and end coordinates, return all the coordinates lying on the line formed by these coordinates, based on Bresenham's algorithm. http://en.wikipedia.org/wiki/Bresenham's_line_algorithm#Simplification @param {number} x0 Start x coordinate @param {number} y0 Start y coordinate @param {number} x1 End x coordinate @param {number} y1 End y coordinate @return {Array.<Array.

Implementation

getLine(x0, y0, x1, y1) {
    var line = [],
        sx, sy, dx, dy, err, e2;

    dx = abs(x1 - x0);
    dy = abs(y1 - y0);

    sx = (x0 < x1) ? 1 : -1;
    sy = (y0 < y1) ? 1 : -1;

    err = dx - dy;

    while (true) {
        line.add([x0, y0]);

        if (x0 == x1 && y0 == y1) {
            break;
        }

        e2 = 2 * err;
        if (e2 > -dy) {
            err = err - dy;
            x0 = x0 + sx;
        }
        if (e2 < dx) {
            err = err + dx;
            y0 = y0 + sy;
        }
    }

    return line;
}