Last active
August 29, 2015 13:57
-
-
Save willbreitkreutz/9672728 to your computer and use it in GitHub Desktop.
Convert between linear referenced M values and Station Values given conversion locations as an array.
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
var toStation = function(m,calib,fmt){ | |
var retValue; | |
var r = null; | |
if(m < calib[0][0] || m > calib[calib.length-1][0]){ | |
return 'm is out of bounds, valid range is ' + calib[0][0] + ' to ' + calib[calib.length-1][0]; | |
} | |
for(var i = 0; i < calib.length; i++){ | |
if(m >= calib[i][0] && m < calib[i+1][0]){ | |
r = ((m - calib[i][0]) / (calib[i+1][0] - calib[i][0]) * (calib[i+1][1] - calib[i][1])) + calib[i][1]; | |
break; | |
} | |
} | |
if(fmt===true){ | |
retValue = parseInt(r).toString(); | |
if(retValue.length>=3){ | |
retValue = retValue.substr(0,retValue.length-2) + '+' + retValue.substr(-2); | |
}else{ | |
retValue = retValue.length < 2 ? '0+0' + retValue : '0+' + retValue; | |
} | |
}else{ | |
retValue = r; | |
} | |
return retValue; | |
} | |
var toM = function(station,calib){ | |
var numStation = Number(station.toString().replace('+','')); | |
var r = null; | |
if(numStation < calib[0][1] || numStation > calib[calib.length-1][1]){ | |
return 'station is out of bounds, valid range is ' + calib[0][1] + ' to ' + calib[calib.length-1][1]; | |
} | |
for(var i = 0; i < calib.length; i++){ | |
if(numStation >= calib[i][1] && numStation < calib[i+1][1]){ | |
r = ((numStation - calib[i][1]) / (calib[i+1][1] - calib[i][1]) * (calib[i+1][0] - calib[i][0])) + calib[i][0]; | |
break; | |
} | |
} | |
return r; | |
} | |
console.log(toStation(27,[[0,0],[200,250],[750,900]],true)); | |
console.log(toStation(352,[[0,0],[200,250],[750,900]],true)); | |
console.log(toStation(800,[[0,0],[200,250],[750,900]],true)); | |
console.log(toM('4+45',[[0,0],[200,250],[750,900]])); | |
console.log(toM(203,[[0,0],[200,250],[750,900]])); | |
console.log(toM('-1+32',[[0,0],[200,250],[750,900]])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
console.log()
statements are included for running in firebug or other JS run-time for testing.