Skip to content

Instantly share code, notes, and snippets.

@jam2-hey
Last active September 25, 2016 05:06
Show Gist options
  • Save jam2-hey/424a9fc8507436883a911e6f1d79d9d3 to your computer and use it in GitHub Desktop.
Save jam2-hey/424a9fc8507436883a911e6f1d79d9d3 to your computer and use it in GitHub Desktop.
class Bresenham {
static plot(x0, y0, x1, y1) {
let dots = [];
let dx = Math.abs(x1 - x0);
let dy = Math.abs(y1 - y0);
let sx = (x0 < x1) ? 1 : -1;
let sy = (y0 < y1) ? 1 : -1;
let err = dx - dy;
dots.push({ x: x0, y: y0 });
while(!((x0 == x1) && (y0 == y1))) {
let e2 = err << 1;
if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
dots.push({ x: x0, y: y0 });
}
return dots;
}
}
export default Bresenham;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment