Does anyone know how draw a curve line when a set of given points. I use the spline but it did not go throught these given points. Does anyone have an example I can look at?The easiest way I can think of would be to use a Catmull-Rom spline.
In matrix form : (for a point q on the curve at t)
q(t) = 0.5 * [ t^3 t^2 t 1 ] * [ -1 3 -3 1 ] * [ p1 ] [ 2 -5 4 -1 ] [ p2 ] [ -1 0 1 0 ] [ p3 ] [ 0 2 0 0 ] [ p4 ]Where p0,p1,p2,p3 are _points_ along the curve, (note however that the curve drawn actually only passes through points p2 and p3)
(sorry my ascii matrices aren't too good :) (that was a constant by a 1x4 matrix * 4x4 matrix * 4x1 matrix)
This becomes :
q(t) = 0.5 * ((-p1 + 3*p2 -3*p3 + p4)*t*t*t + (2*p1 -5*p2 + 4*p3 - p4)*t*t + (-p1+p3)*t + 2*p2)For values of t between 0 and 1 the curve passes through P2 at t=0 and it passes through P3 at t=1. Another nice property of these curves is that the tangent vector at a point P is parallel to the line joining P's two surrounding points.
To do more than two points just step through the array of points using the previous point, the current point and the next two points as the four points for the spline. For each of these segments draw a curve for 0<t<1. This curve will be between the current point and the next point.
This method is not particularly fast though. A fast method for display of this class of splines is given in a SIGGRAPH 89 Paper, "A Recursive Evaluation Algorithm for a Class of Catmull-Rom Splines," by Barry, P. and R.Goldsman.