Last active
August 5, 2016 15:20
-
-
Save dmgig/a8a84d3ff5376727d7bb6786bfc38f01 to your computer and use it in GitHub Desktop.
Get points between two points on simple grid (must be in a straight line)
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
/** | |
* Get points between two points on simple grid. Points must be in a straight line. | |
* Results will include start and end points | |
* | |
* @param A array x, y coordinates of one point | |
* @param B array x, y coordinates of second point | |
* @return false if points are not in a straight line or if points are the same else | |
* array of arrays of x, y coordinates between the two points. | |
* | |
* fixed so that coordinates can be in any direction from each other. | |
* http://stackoverflow.com/questions/13491676/get-all-pixel-coordinates-between-2-points | |
* http://jharaphula.com/javascript-function-to-find-all-points-between-any-two-points/ | |
*/ | |
var lineMath = function(A, B){ | |
A = [Number(A[0]), Number(A[1])]; | |
B = [Number(B[0]), Number(B[1])]; | |
// if points are the same, return the point | |
if(A[0] == B[0] || A[1] == B[1]){ | |
return A; | |
} | |
// if points are not in a straight line, return (bool) false | |
if(Math.abs(A[0] - B[0]) == Math.abs(A[1] - B[1])){ | |
return false; | |
} | |
function slope(a, b) { | |
if (a[0] == b[0]) { | |
return null; | |
} | |
return (b[1] - a[1]) / (b[0] - a[0]); | |
} | |
function intercept(point, slope) { | |
if (slope === null) { | |
// vertical line | |
return point[0]; | |
} | |
return point[1] - slope * point[0]; | |
} | |
var m = slope(A, B); | |
var b = intercept(A, m); | |
var swapped = false; | |
if (A[0] > B[0] || A[1] > B[1]){ | |
var A2, B2; | |
swapped = true; | |
B2 = [A[0], A[1]]; | |
A2 = [B[0], B[1]]; | |
A = A2; B = B2; | |
} | |
var coordinates = []; | |
var y; | |
if(A[0] != B[0]){ | |
if(A[0] < B[0]){ | |
for (x = A[0]; x <= B[0]; x++) { | |
y = m * x + b; | |
coordinates.push([x, y]); | |
} | |
}else{ | |
for (x = A[0]; x >= B[0]; x--) { | |
y = m * x + b; | |
coordinates.push([x, y]); | |
} | |
} | |
}else{ | |
for (y = A[1]; y <= B[1]; y++) { | |
coordinates.push([A[0], y]); | |
} | |
} | |
if(swapped) | |
coordinates.reverse(); | |
return coordinates; | |
} |
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
/** out */ | |
QUnit.test( "lineMath 0 degree", function( assert ) { | |
var A = [0,2], B = [0,0]; | |
var coords = lineMath(A, B); | |
assert.deepEqual( coords, [A, [0,1], B] ); | |
}); | |
QUnit.test( "lineMath 45 degree", function( assert ) { | |
var A = [0,2], B = [2,0]; | |
var coords = lineMath(A, B); | |
assert.deepEqual( coords, [A, [1,1], B] ); | |
}); | |
QUnit.test( "lineMath 90 degree", function( assert ) { | |
var A = [0,2], B = [2,2]; | |
var coords = lineMath(A, B); | |
assert.deepEqual( coords, [A, [1,2], B] ); | |
}); | |
QUnit.test( "lineMath 135 degree", function( assert ) { | |
var A = [0,2], B = [2,4]; | |
var coords = lineMath(A, B); | |
assert.deepEqual( coords, [A, [1,3], B] ); | |
}); | |
/** in **/ | |
QUnit.test( "lineMath 180 degree", function( assert ) { | |
var A = [0,0], B = [0,2]; | |
var coords = lineMath(A, B); | |
assert.deepEqual( coords, [A, [0,1], B] ); | |
}); | |
QUnit.test( "lineMath 225 degree", function( assert ) { | |
var A = [2,0], B = [0,2]; | |
var coords = lineMath(A, B); | |
assert.deepEqual( coords, [A, [1,1], B] ); | |
}); | |
QUnit.test( "lineMath 270 degree", function( assert ) { | |
var A = [2,2], B = [0,2]; | |
var coords = lineMath(A, B); | |
assert.deepEqual( coords, [A, [1,2], B] ); | |
}); | |
QUnit.test( "lineMath 315 degree", function( assert ) { | |
var A = [2,4], B = [0,2]; | |
var coords = lineMath(A, B); | |
assert.deepEqual( coords, [A, [1,3], B] ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment