function makeItArrayIfItsNot ( input ) { return Object . prototype . toString . call ( input ) !== '[object Array]' ? [ input ] : input ; } param { Number } point param { Array } intervals param { Boolean } useRightBorder returns { Number } function findIntervalBorderIndex ( point , intervals , useRightBorder ? ) { if ( point < intervals [ 0 ] ) return 0 ; if ( point > intervals [ intervals . length - 1 ] ) return intervals . length - 1 ; var indexOfNumberToCompare , leftBorderIndex = 0 , rightBorderIndex = intervals . length - 1 ; while ( rightBorderIndex - leftBorderIndex !== 1 ) { indexOfNumberToCompare = leftBorderIndex + Math . floor ( ( rightBorderIndex - leftBorderIndex ) / 2 ) ; point >= intervals [ indexOfNumberToCompare ] ? ( leftBorderIndex = indexOfNumberToCompare ) : ( rightBorderIndex = indexOfNumberToCompare ) ; } return useRightBorder ? rightBorderIndex : leftBorderIndex ; } param { Number | Array } pointsToEvaluate param { Array } functionValuesX param { Array } functionValuesY returns { Array } param x param x0 param y0 param x1 param y1 returns { Number } function linearInterpolation ( x , x0 , y0 , x1 , y1 ) { var a = ( y1 - y0 ) / ( x1 - x0 ) ; var b = - a * x0 + y0 ; return a * x + b ; } export function linear ( pointsToEvaluate , functionValuesX , functionValuesY ) { var results = [ ] ; pointsToEvaluate = makeItArrayIfItsNot ( pointsToEvaluate ) ; pointsToEvaluate . forEach ( function ( point ) { var index = findIntervalBorderIndex ( point , functionValuesX ) ; if ( index == functionValuesX . length - 1 ) index -- ; results . push ( linearInterpolation ( point , functionValuesX [ index ] , functionValuesY [ index ] , functionValuesX [ index + 1 ] , functionValuesY [ index + 1 ] ) ) ; } ) ; return results ; }