Reputation: 1312
I have been looking at the code below which I found here: http://tehc0dez.blogspot.co.uk/2010/04/nice-curves-catmullrom-spline-in-c.html
/// <summary>
/// Calculates interpolated point between two points using Catmull-Rom Spline
/// </summary>
/// <remarks>
/// Points calculated exist on the spline between points two and three.
/// </remarks>/// <param name="p0">First Point</param>
/// <param name="p1">Second Point</param>
/// <param name="p2">Third Point</param>
/// <param name="p3">Fourth Point</param>
/// <param name="t">
/// Normalised distance between second and third point
/// where the spline point will be calculated
/// </param>
/// <returns>
/// Calculated Spline Point
/// </returns>
static public PointF PointOnCurve(PointF p0, PointF p1, PointF p2,
PointF p3, float t)
{
PointF ret = new PointF();
float t2 = t * t;
float t3 = t2 * t;
ret.X = 0.5f * ((2.0f * p1.X) + (-p0.X + p2.X) * t +
(2.0f * p0.X - 5.0f * p1.X + 4 * p2.X - p3.X) * t2 +
(-p0.X + 3.0f * p1.X - 3.0f * p2.X + p3.X) * t3);
ret.Y = 0.5f * ((2.0f * p1.Y) + (-p0.Y + p2.Y) * t +
(2.0f * p0.Y - 5.0f * p1.Y + 4 * p2.Y - p3.Y) * t2 +
(-p0.Y + 3.0f * p1.Y - 3.0f * p2.Y + p3.Y) * t3);
return ret;
}
The parameter I do not understand is t which is described as a normalised distance between second and third point. What is and how do I calculate a normalised distance in this context?.
Upvotes: 0
Views: 1176
Reputation: 20271
For a better explanation of the Catmull-Rom Spline, have a look at this page, figure 1 should help you along.
You have four points given, which define your spline. t
indicates the position between points 2 and 3, for which the coordinates shall be calculated. t=0
is point 2, t=1
is point 3. t=0.5
is halfway between points 2 and 3.
What you would typically do in order to draw the spline on the screen is to have t
run from 0 to 1 with a specific interval e.g. 0.1. That would give you the exact coordinates of the points with t
= 0.1, 0.2, 0.3,... 0.9. You can either choose a small interval and paint a dot for every resulting coordinate, or you can choose a bigger interval and draw a straight line between two neighbouring coordinates.
Upvotes: 3