Click here to Skip to main content
12,761,826 members (40,078 online)
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 19 Oct 2005

Interpolation with Polynomials and Splines

, 20 Oct 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
A port of the Java sample project written by Tobias von Petersdorff
Sample Image - SplineInterpolation.jpg


First of all, take a look here, where Tobias von Petersdorff has published his interpolation form written in Java.

Have you tried to find some sample code written in C# that helps you to draw a Spline? I've tried with no success.

This is a port of his sample project.

switch (mode) { 
// use divided difference algorithm to compute Newton form coefficients
for (int k=1; k<=np-1; k++) 
for (int i=0; i<=np-1-k; i++) 

// for equidistant points along x-axis evaluate polynomial and draw line
float dt = ((float) this.graphPanel.Width-1)/npp;
for (int k=0; k<=npp; k++) 
// evaluate polynomial at t
y = yCoords[0];
for (int i=1; i<=np-1; i++) 
// draw line

g.DrawLine(linePen, (int)oldx,(int)oldy,(int)x,(int)y);
catch(Exception ex)
Console.WriteLine(ex.Message + " x1={0} y1={1} x2={2} y2={3}", oldx, oldy, x, y);
if (np>1)
float[] a = new float[np];
float x1;
float x2;
float[] h = new float[np];
for (int i=1; i<=np-1; i++)
h[i] = xCoords[i] - xCoords[i-1];
if (np>2)
float[] sub = new float[np-1];
float[] diag = new float[np-1];
float[] sup = new float[np-1];
for (int i=1; i<=np-2; i++)
diag[i] = (h[i] + h[i+1])/3;
sup[i] = h[i+1]/6;
sub[i] = h[i]/6;
a[i] = (yCoords[i+1]-yCoords[i])/h[i+1]-(yCoords[i]-yCoords[i-1])/h[i];
solveTridiag(sub,diag,sup,ref a,np-2);
// note that a[0]=a[np-1]=0
// draw
g.DrawLine(linePen, (int)oldx,(int)oldy,(int)oldx,(int)oldy);
catch(Exception ex)
Console.WriteLine(ex.Message + " x1={0} y1={1} x2={0} y2={1}", oldx, oldy);

for (int i=1; i<=np-1; i++) 
{ // loop over intervals between nodes
for (int j=1; j<=precision; j++)
x1 = (h[i]*j)/precision;
x2 = h[i] - x1;
y = ((-a[i-1]/6*(x2+h[i])*x1+yCoords[i-1])*x2 +
if ((lastPen == null) || (lastPen == linePen2))
linePen = linePen1;
linePen = linePen2;
lastPen = linePen;

g.DrawLine(linePen, (int)oldx,(int)oldy,(int)x,(int)y);
catch(Exception ex)
Console.WriteLine(ex.Message + " x1={0} y1={1} x2={2} y2={3}", oldx, oldy, x, y);



  • 20th October, 2005: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Marco Roello
Web Developer
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionBest Cubic Spline Interpolation example on the net. Pin
gollyw9-Jun-15 23:35
membergollyw9-Jun-15 23:35 
GeneralMy vote of 5 Pin
Gun Gun Febrianza30-Oct-14 18:38
member Gun Gun Febrianza30-Oct-14 18:38 
QuestionHow to get the tangent of a given point after the interpolation? Pin
Pi Zion27-Apr-14 18:20
memberPi Zion27-Apr-14 18:20 
GeneralMy vote of 2 Pin
DanielSheets16-Sep-13 10:29
memberDanielSheets16-Sep-13 10:29 
Question[My vote of 2] not enough explanation Pin
CIDev18-Jul-13 4:23
memberCIDev18-Jul-13 4:23 
QuestionInterpolation with Polynomials and Splines Pin
Member 100400279-May-13 5:43
memberMember 100400279-May-13 5:43 
QuestionThis rocks Pin
John S Battista16-Aug-12 16:54
memberJohn S Battista16-Aug-12 16:54 
GeneralNot works when points are close one to another Pin
Retro_0128-May-10 2:27
memberRetro_0128-May-10 2:27 
GeneralIt works only in one dimension Pin
kuratowski10-Nov-09 13:52
memberkuratowski10-Nov-09 13:52 
Generalquestion for Marco Roello [modified] Pin
flashenoise30-May-06 1:42
memberflashenoise30-May-06 1:42 
QuestionNice. Did you try Graphics.DrawCurve() ? Pin
axelriet21-Oct-05 1:31
memberaxelriet21-Oct-05 1:31 
AnswerRe: Nice. Did you try Graphics.DrawCurve() ? Pin
Marco Roello21-Oct-05 2:39
memberMarco Roello21-Oct-05 2:39 
GeneralRe: Nice. Did you try Graphics.DrawCurve() ? Pin
axelriet21-Oct-05 2:55
memberaxelriet21-Oct-05 2:55 
GeneralBad source Link Pin
Marco Roello20-Oct-05 22:15
memberMarco Roello20-Oct-05 22:15 
QuestionThanks. How about the sources? Pin
_MMM_20-Oct-05 17:15
suss_MMM_20-Oct-05 17:15 
AnswerRe: Thanks. How about the sources? Pin
Anonymous20-Oct-05 21:32
sussAnonymous20-Oct-05 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 20 Oct 2005
Article Copyright 2005 by Marco Roello
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid