-
-
Save kaiiserni/719e5f8163922432ae6375f904f3d9bb to your computer and use it in GitHub Desktop.
An implementation of Bresenham's line algorithm
This file contains 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
// Bresenham's line algorithm | |
var getRay = function(x0,y0,x1,y1){ | |
var dx = Math.abs(x1-x0); | |
var dy = Math.abs(y1-y0); | |
var sx = x0 < x1 ? 1 : -1; | |
var sy = y0 < y1 ? 1 : -1; | |
var err = dx-dy; | |
var ray = []; | |
while(x0!=x1 || y0!=y1){ | |
ray.push([x0,y0]); | |
var e2 = 2*err; | |
if(e2>-dy){ | |
err = err - dy; | |
x0 += sx; | |
} | |
if(e2<dx){ | |
err = err + dx; | |
y0 += sy; | |
} | |
} | |
return ray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment