Does anyone know an algorithm to make a spline?

If you could be a little specific as to what kind of spline, from what information it needs to be generated from, how it needs to output etc.

You could try some form of regression if its just points.

This is for an drawing program. Its sort of like in Autocad, input one point, then another, it draws a line, then another, it draws a spline through the points, then you can continue to add points if you'd like. Btw I'm not even sure of the math definition of a spline, so that's why I need help to draw one. PS I'm using a window defined by :ZStandard:ZInteger

Last year, earthnite and I create an *n*th degree polynomial regression tool, that can find the least-squares regression polynomial of degree n for a given set of points. However, finding a spline is more complicated.

Basically, drawing a spline involves generating a piecewise function that passes through every point and is continuous and smooth (twice-differentiable). In order to do this, you need to calculate the curvature at each point, which can be accomplished by solving a tridiagonal linear system, which I am doing with rref(.

```
{X0,X1,...,Xn}->L1
{Y0,Y1,...,Yn}->L2
SortA(L1,L2
Plot1(Scatter,L1,L2
dim(L1->N
{Ans,Ans+1->dim([A]
For(I,2,dim(L1)-2,N-1
L1(I-1->A
L1(I->C
L2(I->D
L1(I+1->E
1/(C-A->[A](I,I-1
2/(C-A)+2/(E-C->[A](I,I
1/(E-C->[A](I,I+1
3((D-L2(I-1))/(C-A)^^2+(L2(I+1)-D)/(E-C)^^2->[A](I,N+1
End
1/(L1(2)-L1(1->[A](1,2
2Ans->[A](1,1
3(L2(2)-L2(1))/((L1(2)-L1(1))^^2->[A](1,N+1
1/(L1(N)-L1(N-1->[A](N,N-1
2Ans->[A](N,N
3(L2(N)-L2(N-1))/(L1(N)-L1(N-1))^^2->[A](N,N+1
rref([A]->[A]
seq([A](I,N+1),I,1,N->L3
2->I
L2(1->Q
For(X,L1(1)+.1,L1(N),DeltaX
Q->P
I+(X>L1(I->I
(X-L1(I-1))/(L1(I)-L1(I-1->T
L3(I-1)(L1(I)-L1(I-1))-L2(I)+(L2(I-1->A
~L3(I)(L1(I)-L1(I-1))+L2(I)-L2(I-1->B
(1-T)L2(I-1)+TL2(I)+T(1-T)(A(1-T)+BT->Q
Line(X-.1,P,X,Q
End
Line(X,Q,L1(N),L2(N
```

The function above gives the first-derivatives at each point, which are then used to calculate the cubic function between each pair of points. Plotting the spline should simply require evaluating the function at each x value and drawing a between each (Xi-1,Yi-1) (Xi,Yi).

EDIT: The program above now graphs the cubic spline as well.

The spline is a function of X, so each X value must be unique.

I am using the token syntax from SourceCoder3 in case you were wondering about things.

I know how to make a spline, and have posted my solution above. I put time and effort into creating this, so I would appreciate it if you could comment about whether or not it suits your needs.

He probably doesn't care much, as all the effort he put into it was: ZStandard:ZInteger. Though, I'm sure others might find this information useful in the future.

Hey! I don't work on Sunday, and I was busy before, and I DO care. That window setting is all I needed for the various drawing functions to do their job since they don't really rely of a friendly window. all it needs is a square setting with the increment of 1. Please don't judge a programmer by their window! Anyways, I only asked this because I didn't understand how to make a spline.

Wow. this really good. thanks. I'll try to implement this in my drawing program. I wanted something like the spline command in autocad or in other drawing programs, and this does it well.

I know what its like to put effort into a program, and then its not wanted. So, thank you for putting in the effort. I'm now trying to make it fit my EZDraw program, and I did want it. Sorry I couldn't respond sooner.

No problem! I hope you can put it to good use. :)

Ok, I finally finished typing and testing it and -wow

And, like Earthnite said, it will be useful for others also. Why not put it on the programs page?

Oh, one way it could be better is if it didn't have to be a function (two or more y values) as it is , If you try to bend the spline back on itself, it just takes the next x value point. maybe i'm thinking of a beizer or beizer spline, though.

If I have more time, I might try to make some kind of parametric spline.

Bézier curves are the mathematical basis of the pen tool in photoshop (among many other applications), and may be what you're thinking of. They're really cool, and pretty easy to understand and code.

There is a definite distinction between a spline, and a Bezier curve - although a Bezier spline exists. A Bezier curve is parametric curve defined by control points, which may not be on the curve. A spline is a curve that passes through all of the points that define it (knots). In order to create a Bezier spline (a piecewise Bezier curve), you must first calculate additional control points for each segment. If you would like to do this, there is more information about it here.

I know. I was attempting to provide what was for me simple and fun point of entrance to these ideas.

I'm very interested in the drawing program your making. Can i download it? I will give you credit if I use it in my other programs.

yeah, umm, its kind of large, and I don't have a connect cable, but I could give you the basics so you could make one yourself…

Program 1

Title/Setup program: this is the program you call, and it sets the window and any vars

I use

```
:Zstandard
:ZInteger
```

because all my drawing programs don't rely on a specific window, but circles need a square window, and It is nice to have the increment be 1

then define pic1 (recall or store), depending on what you want,

0->A:0->B defines the input point (A,B)

now call the menu

Menu program

simply a menu that calls all the functional commands

Pen

line

circle

Black screen

spline

after the menus and programs, goto the top W/ a lbl

..anything else… shape? text?

Pen program

repeat loop

Recallpic 1

uses the arrow keys or number pad to move, use a piecewise expression to change (A,B) After a repeat getkey loop

Uses C to indicate whether the pen is black, off, or white, with possible values 1,0,-1 respectively numberkey 5 toggles black and off, numberkey 0 turns it white

uses Pton( and Ptoff( to change the point based on C

if the getkey # was for the mode button,then return to the menu

storepic1

end repeat loop

line

input points,output line

if mode button is pressed, return

circle

input points, output circle, uses a real var to toggle between empty and filled

if empty, use circle(

if filled, use the shaded circles routine.

ditto for mode

black screen

for(var,xmin,xmax

vertical var

End

spline

above, adapted

…and any other functions

sorry I can't upload right now, I can only post small programs. ti83's are awful for connectability to a computer