Last active
May 16, 2017 14:36
-
-
Save j-medland/5e9589c943d657693a23e8d34d6a160e to your computer and use it in GitHub Desktop.
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 pricePerArea (area) { | |
var areas = [0.15, 0.5, 0.75, 1, 5, 10, 15, 20, 35, 50, 75, 100, 150, 200, 350, 500, 750, 1000, 1250, 1500, 2000, 2500, 3000] | |
var prices = [8, 8, 5, 4.5, 4, 3.8, 2.8, 2.6, 2.4, 2.2, 2, 1.6, 1.4, 1.2, 0.9, 0.75, 0.65, 0.5, 0.4, 0.35, 0.25, 0.15, 0.12] | |
// catch edge cases | |
if (area <= areas[0]) { | |
return prices[0] | |
} | |
if (area >= areas.slice(-1)[0]) { | |
return prices.slice(-1)[0] | |
} | |
// find the index of lower area value | |
var i = areas.findIndex(function (a, i) { | |
return area > a && area <= areas[i + 1] | |
}) | |
// calculate the gradient | |
var a = (prices[i + 1] - prices[i]) / (areas[i + 1] - areas[i]) | |
// calculate the offset | |
var b = -a * areas[i] + prices[i] | |
// interpolate | |
return a * area + b | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment