I’m writing a C++ program to generate a cubic spline trajectory for a set of points. These points need not be sorted along the x-axis. For example, it may be a circle, etc.
I have found some libraries on the web, for example, the ALGLIB library or a class here https://www.marcusbannerman.co.uk/index.php/home/42-articles/96-cubic-spline-class.html, but all of these libraries sort the data points. I do not need this because what I want to generate is something like a circle. Is there anyway to achieve this?
Splines are piecewise functions with respect to some independent variable (usually
t, though they seem to usexin the code you have linked). Since the specific function to be evaluated depends on the control points closest to the input valuet, it make sense to sort the control points bytso that you can quickly determine the function that needs to be evaluated.However even if they were not sorted, you still could not create a circle with a single one dimensional spline. Your spline function
y = f(t)only gives you one value for any givent. If you are graphingywith respect totand want a circle with radius 1 about the origin, you would needf(0)to equal both1and-1, which doesn’t make any sense.To get something like a circle you instead need a two dimensional spline, or two splines; one for the
xvalue and one for theyvalue. Once you have these two spline functionsf(t)andg(t), then you simply evaluate both functions at the sametand that will give you thexandyvalues of your spline for thatt.