Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kirankumaramruthaluri/48f427017a760a4f3909 to your computer and use it in GitHub Desktop.
Save kirankumaramruthaluri/48f427017a760a4f3909 to your computer and use it in GitHub Desktop.
Bresenham's Line Drawing Algorithm
function bline(x0, y0, x1, y1) {
var dx = Math.abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
var dy = Math.abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
var err = (dx>dy ? dx : -dy)/2;
while (true) {
setPixel(x0,y0);
if (x0 === x1 && y0 === y1) break;
var e2 = err;
if (e2 > -dx) { err -= dy; x0 += sx; }
if (e2 < dy) { err += dx; y0 += sy; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment