Last active
September 25, 2016 05:06
-
-
Save jam2-hey/424a9fc8507436883a911e6f1d79d9d3 to your computer and use it in GitHub Desktop.
Bresenham's line algorithm is JS (es6) http://stackoverflow.com/questions/4672279/bresenham-algorithm-in-javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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