I need to draw a line in my website, actually a curve representing a third degree polynom.
What is the easiest way of finding a third degree equation that fits two points with given slopes in javascript?
Find the third degree equation for(or find the coeffecient a,b,c,d in general formula ax^3+bx^2+cx+d = y):
startX, startY, startSlope
endX, endY, endSlope
Essentially this is just straightforward math.
You’ve got an unknown expression y=ax3+bx2+cx+d. You can drop quite a few terms by defining x’ = (x-startX)/endX (i.e. startX’ = 0, endX’ = 1). You’ll also have to scale the slopes; startSlope’ = startSlope * 1/(endX-startX).
From this it follows that d’ = startY. That’s your first free parameter.
Next, note that the slope is trivially obtained by differentiation. dy/dx’ = 3a’x’2+2b’x’+c’. Therefore, c’ is just startSlope’.
a’ and b’ take a pair of equations: endY = a’+b’+c’+d’, and endSlope = 3a’+2b’+c’+d’. Therefore a’ = endSlope’ – 2*endY, and b’ = 3*endY – endSlope’.