Created
February 22, 2015 10:15
-
-
Save kirankumaramruthaluri/48f427017a760a4f3909 to your computer and use it in GitHub Desktop.
Bresenham's Line Drawing Algorithm
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
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