Skip to content

Instantly share code, notes, and snippets.

@gossi
Created February 28, 2012 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gossi/1934685 to your computer and use it in GitHub Desktop.
Save gossi/1934685 to your computer and use it in GitHub Desktop.
Bresenham Algorithmus
function setPixel(x, y) {
console.log("Pixel auf %s/%s", x, y);
}
function bresenham(start, end) {
var dx = end.x - start.y,
dy = end.y - start.y,
x = start.x,
y = start.y,
err = dx / 2;
setPixel(x, y);
while (x < end.x) {
// fast forward
x++;
err -= dy;
// slow forward... if needed
if (err < 0) {
y++;
err += dx;
}
setPixel(x, y);
}
}
bresenham({x:1,y:1},{x:7,y:5});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment