Cubic Splines

 BBS: Inland Empire Archive
Date: 05-28-92 (19:40)             Number: 183
From: LOU CIRIELLO                 Refer#: NONE
  To: NATHAN LITKE                  Recvd: NO  
Subj: Cubic Splines                  Conf: (1) 80xxx

In a msg on <May 24 19:47>, Nathan Litke of 1:247/101 writes: NL> Well, I've finally realized that I am NOT looking for a Bezier curve; NL> rather, my application requires a CUBIC SPLINE. So, if anyone out NL> there knows anything about this formula, I would be very grateful if NL> they could drop me a note (source code especially would be appreciated!) NL> Thanks in advance. Well, you might try the following cubic paramaterization to connect successive points with a smooth curve: Let's say (X1, Y1),...(X4, Y4) are the first four consecutive points of an array you wish to connect with a smooth continuous curve. The coefficients of a parametric third order polynomial equation are to be found for both X and Y values. I'll work through the X equation first: 3 2 3 2 Let X(T) = A T + B T + C T + D , Y(T) = A T + B T + C T + D x x x x y y y y These equations must simultaneously predict the coordinates of all four points at four specific values of T. To make things easier, we can arbitrarily assign values of T to the first four points as follows: T X(T) Y(T) ----------------------- -2 X1 Y1 -1 X2 Y2 0 X3 Y3 1 X4 Y4 Doing so yields the following four sets of simultaneous equations which can then be solved for the coefficients of X(T): Ax, Bx, Cx, and Dx: (1) -8 A + 4 B - 2 C + D = X1 x x x x (2) - A + B - C + D = X2 x x x x (3) D = X3 x (4) A + B + C + D = X4 x x x x Notice the values chosen for T make these much easier to solve. By combining (2) & (4) you get immediately the solution for Bx. The complete solution for the coefficients of X(T) is: A = (X4 - X1) / 6 + (X2 - X3) / 2 x B = (X2 + X4) / 2 - X3 x C = (X1 - 6 X2 + 3 X3 + 2 X4) / 6 x D = X3 x By replacing the X coordinates with Y's in the above, you get the solution for the Y(T) coefficients: Ay, By, Cy, & Dy. To interpolate the first four points with a smooth curve, evaluate and plot X(T) & Y(T) from T = -2 to T = +1. Then to connect the fifth (and successive points thereafter) just shift the values of X & Y forward through the arrays, recalculate the coefficients, and evaluate X(T) & Y(T) from T = 0 to T = 1 to attach each new curve segment: for I = 5 to N Let X1 = X2, X2 = X3, X3 = X4, X4 = XARRAY[I] Let Y1 = Y2, Y2 = Y3, Y3 = Y4, Y4 = YARRAY[I] recalculate the coefficients for both X(T) & Y(T) evaluate & plot X(T) & Y(T) from T = 0 to T = 1 to connect a curve segment Next I Repeat this for all remaining points in the array, and you will have a smooth curve which, though I'm not certain is a cubic spline, is very continuous. Any questions? --- msgedsq 2.0.5 * Origin: Get the Point! Waterbury, CT (1:141/1170.5)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET