Fit curve to two lines

1.7k Views Asked by At

I'm currently making a mobile game. Using two "connected" lines, I'd like to find the equation for the curve that fit these. The curve would model the trajectory of a projectile. How do i fit a cubic function to the two "connected" lines? example

3

There are 3 best solutions below

1
On

Assume $p_1=(x_1,y_1)$ and is the left point,$p_2=(x_2,y_2)$ and is the right point, $m_1$ is the slope of the line through $p_1$, and $m_2$ is the slope of the line through $p_2$.

Solve the equations for $\{a_0,a_1,a_2,a_3\}$: $$\begin{align} a_0x_1^3+a_1x_1^2+a_2x_1+a_3&=y_1\\ a_0x_2^3+a_1x_2^2+a_2x_2+a_3&=y_2\\ 3a_0x_1^2+2a_1x_1+a_2&=m_1\\ 3a_0x_2^2+2a_1x_2+a_2&=m_2 \end{align}$$

The first two equations will give you a set of values of $\{a_0,a_1,a_2,a_3\}$ such that the cubic $a_0x_1^3+a_1x_1^2+a_2x_1+a_3$ goes through $p_1$ and $p_2$.

The last two equations take the slope of the two lines in consideration by finding the derivative of the cubic.

Also, as Hikaru said in the comments, check out Bézier Curves. They come up a lot in computer programming, animation, etc.

1
On

At time $t=0$ you are in point $P_0$ and have velocity $v_0$.

At time $t=1$ you are in point $P_1$ and have velocity $v_1$.

You can make a (quadratic) weight function to take care of the transition.

E.g. $x(t) = (P_0 + v_0*t)(1-t)^2 + (P_1 + v_1*(1-t))t^2$

Be careful of the sign of $v_1$ though.

For practical purposes, you could modify the velocity weight.

(If time $t$ is already in your calculations, use $\lambda$)

0
On
  • Given slopes m1,m2
  • First point is origin and for convenience of calculation assume second tangent drawn at point of inflection
  • Coordinates of the second point not necessary under this assumption, it can be subsequently found

Let the equation of cubic be

$$ y = x^3 - 3 A x^2 + m1 \, *x \tag1 $$

$$ y'= 3 x^2-6 A \,x + m1 \tag2 $$

$$ y^{''} = 6\,x - 6 A =0, \,\rightarrow \, x= A \tag3$$

Let slope at PI $x=A$ be m2

$$ y^{'} =3 A^2 -6 A \cdot A + m1 = m2 \tag4$$

Simplify

$$ A = \sqrt{(m1-m2)/3} \tag5 $$

Plug in into 1)

$$ y = x^3 - \sqrt{3 (m1-m2)} x^2 + m1 * x \tag6 $$

$$ y2 = A^3 - \sqrt{3 (m1-m2)} A^2 + m1 * A \tag7 $$

When value of $A$ is plugged in, you will find $y2$ involves only the slopes

For example take $ m1=0.5, m2= -1, \rightarrow A= 0.7071 \tag8 $enter image description here

$$ y = x^3- 1.5 *1.4142 x^2 -0.5 x \tag 9 $$

which can be written as a formula in terms of given slopes. It is given here upto inflection point, the full cubic is not sketched.