Skip to content

Instantly share code, notes, and snippets.

@loktar00
Created September 20, 2011 02:24
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 loktar00/1228163 to your computer and use it in GitHub Desktop.
Save loktar00/1228163 to your computer and use it in GitHub Desktop.
Finds all the points between 2 coordinates.
// find all points between 2 pooints http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
var x1 = mouseX,
x2 = lastX,
y1 = mouseY,
y2 = lastY;
var steep = (Math.abs(y2 - y1) > Math.abs(x2 - x1));
if (steep){
var x = x1;
x1 = y1;
y1 = x;
var y = y2;
y2 = x2;
x2 = y;
}
if (x1 > x2) {
var x = x1;
x1 = x2;
x2 = x;
var y = y1;
y1 = y2;
y2 = y;
}
var dx = x2 - x1,
dy = Math.abs(y2 - y1),
error = 0,
de = dy / dx,
yStep = -1,
y = y1;
if (y1 < y2) {
yStep = 1;
}
for (var x = x1; x < x2; x++) {
var sandX = x,
sandY = y;
if (steep) {
sandX = y;
sandY = x;
}
error += de;
if (error >= 0.5) {
y += yStep;
error -= 1.0;
}
if(screenData[sandX][sandY]===0){
screenData[sandX][sandY]=1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment