Skip to content

Instantly share code, notes, and snippets.

@vonWolfehaus
Created September 24, 2012 16:34
Show Gist options
  • Save vonWolfehaus/3776882 to your computer and use it in GitHub Desktop.
Save vonWolfehaus/3776882 to your computer and use it in GitHub Desktop.
Simple interpolation technique for plotting missing points between two key points
function lineInterpolate(point1, point2, distance) {
var xabs = Math.abs( point1.x - point2.x );
var yabs = Math.abs( point1.y - point2.y );
var xdiff = point2.x - point1.x;
var ydiff = point2.y - point1.y;
var length = Math.sqrt( ( Math.pow( xabs, 2 ) + Math.pow( yabs, 2 ) ) );
var steps = length / distance;
var xstep = xdiff / steps;
var ystep = ydiff / steps;
var newx = 0;
var newy = 0;
var result = new Array();
for( var s = 0; s < steps; s++ ) {
newx = point1.x + ( xstep * s );
newy = point1.y + ( ystep * s );
result.push( {
x: newx,
y: newy
} );
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment