Created
September 24, 2012 16:34
-
-
Save vonWolfehaus/3776882 to your computer and use it in GitHub Desktop.
Simple interpolation technique for plotting missing points between two key points
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
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