using cubic polynomial function
f(x) = h3.x^3 + h2.x^2 + h1.x + h0
i reached upto the 4 hermite blending functions
h3 = (2.x^3 - 3.x^2 + 1).p0
h2 = (x^3 - 2.x^2 + x).g0
h1 = (-2.x^3 + 3.x^2).p1
h0 = (x^3 - x^2).g1
where p0,p1 is point 0,point 1 and g0,g1 is gradient at p0 and p1.
then adding up all the functions will leads to
f(x) = (2.x^3 - 3.x^2 + 1).p0 + (x^3 - 2.x^2 + x).g0 + (-2.x^3 + 3.x^2).p1 + (x^3 - x^2).g1
when i am substituting the value >= 0 x <= 1 , then f(x) result is wrong, i dont understand where am i going wrong. here is my code
public Vector getPoint(Vector p0,Vector p1,Vector p2,Vector p3,float x)
{
Vector ans = new Vector ();
Vector p0t = new Vector (p0);
Vector p1t = new Vector (p1);
Vector p2t = new Vector (p2);
Vector p3t = new Vector (p3);
float c3;
float c2;
float c1;
float c0;
c3 = (2 * x * x * x) - (3 * x * x) + 1;
c2 = (x * x * x) - (2 * x * x) + x;
c1 = (-2 * x * x * x) + (3 * x * x);
c0 = (x * x * x) - (x * x);
p0t.multiply (c3);
p1t.multiply (c2);
p2t.multiply (c1);
p3t.multiply (c0);
ans.add (p0t);
ans.add (p1t);
ans.add (p2t);
ans.add (p3t);
return ans;
}
public void renderCurve()
{
Vector p0,p1,p2,p3,p;
for (int i = 0; i < points.Length; i = i + 4)
{
p0 = new Vector (points[i].x,points[i].y,points[i].z);
p1 = new Vector (points[i + 1].x,points[i + 1].y,points[i + 1].z);
p2 = new Vector (points[i + 2].x,points[i + 2].y,points[i + 2].z);
p3 = new Vector (points[i + 3].x,points[i + 3].y,points[i + 3].z);
for (float t = 0; t <= 1; t = (t + Time.deltaTime * 10))
{
p = hermite.getPoint (p0,p1,p2,p3,t);
GameObject pobj = Instantiate (pointObject, new Vector3(p.x,p.y,0.0f), Quaternion.identity) as GameObject;
}
}
}
where i am doing this wrong ? please help....
Your original function is $$f(x) = h_3x^3 + h_2x^2 + h_1x + h_0$$ and then you say you are substituting in $$\begin{align}h_3 &= (2x^3 - 3x^2 + 1)p_0\\h_2 &= (x^3 - 2x^2 + x)g_0\\h_1 &= (-2x^3 + 3x^2)p_1\\h_0 &= (x^3 - x^2)g_1\end{align}$$
But then you give the result as $$f(x) = (2x^3 - 3x^2 + 1)p_0 + (x^3 - 2x^2 + x)g_0 + (-2x^3 + 3x^2)p_1 + (x^3 - x^2)g_1$$
Please look at the original function. Did you replace $h_3$ in it with the expression for $h_3$?
No. You did not. You replaced "$h_3x^3$" with just the expression for $h_3$. Assuming that you had accurately described what you intended up to this point, you should have had $$\begin{align}f(x) &= (2x^3 - 3x^2 + 1)x^3p_0 + (x^3 - 2x^2 + x)x^2g_0 + (-2x^3 + 3x^2)xp_1 + (x^3 - x^2)g_1\\&=(2x^6 - 3x^5 + x^3)p_0 + (x^5 - 2x^4 + x^3)g_0 + (-2x^4 + 3x^3)p_1 + (x^3 - x^2)g_1\end{align}$$