Skip to content

Instantly share code, notes, and snippets.

@mrosenberg
Created July 20, 2018 20:59
Show Gist options
  • Save mrosenberg/5efdde6840c8e7ccf83db545b71f0a26 to your computer and use it in GitHub Desktop.
Save mrosenberg/5efdde6840c8e7ccf83db545b71f0a26 to your computer and use it in GitHub Desktop.
JS parabolic spend
function defaultCurve( length ) {
// first point
let x1 = 1;
let y1 = 1.9;
// mid point
let x2 = length / 2;
let y2 = 0.5;
// end point
let x3 = length;
let y3 = y1;
// equation vars
let A1 = -Math.pow( x1, 2 ) + Math.pow( x2, 2 );
let B1 = -x1 + x2;
let B2 = -x2 + x3;
let D1 = -y1 + y2;
let A2 = -Math.pow( x2, 2 ) + Math.pow( x3, 2 );
let D2 = -y2 + y3;
let B = -( B2 / B1 );
let A3 = B * A1 + A2;
let D3 = B * D1 + D2;
// final equation vars
let a = D3 / A3;
let b = ( D1 - A1 * a ) / B1;
let c = y1 - a * Math.pow( x1, 2 ) - b * x1;
return function plot( x ) {
return a * Math.pow( x, 2 ) + b * x + c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment