Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Dmitra/5acebc843684ee52908f54a37f8009b3 to your computer and use it in GitHub Desktop.
Save Dmitra/5acebc843684ee52908f54a37f8009b3 to your computer and use it in GitHub Desktop.
//Finds an unknown point distance L along a quadratic curve from a known point.
//Alex Pilafian 2017 - [email protected] - github.com/sikanrong
//If you reuse this code please give me attribution, my dude! I worked hard on this.
//parabola defined by ax^2 + bx + c, a and b are passed in to constructor while c is omitted because it isn't relevant to our calculations.
//u is known point x-value
//L is known length to travel down the curve for our unknown point.
//v is the unknown point x-value, once we have v we can calculate the correspondiing unknown y just by pluging it
//back into our parabola function
var sigFigs = 100000; //round to 5 significant figures
function ParabolicArcSolver(a, b, u, L){
var du = (b + 2*a*u);
var lu = Math.sqrt(1 + Math.pow(du, 2));
var taylor = function(){
return u + (L / lu)
}
var newton = function(v){
var dv = (b + 2*a*v);
var lv = Math.sqrt(1 + Math.pow(dv, 2));
return v + ((lv*(4*a*L + du*lu - dv*lv + Math.asinh(du) - Math.asinh(dv)))/(2*a*(2 + 2 * Math.pow(dv, 2))))
}
//get a good first guess for newton from taylor
var firstGuess = taylor();
//Recursively run newton until it converges on an answer
var doApproximation = function(v){
var lastNewton = newton(v)
if((v - lastNewton) <= 1/sigFigs){
return (Math.floor(Math.round(v*sigFigs)) / sigFigs);
}else{
return doApproximation(lastNewton);
}
}
return doApproximation(firstGuess);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment