12,761,826 members (40,078 online)
alternative version

#### Stats

82.7K views
47 bookmarked
Posted 19 Oct 2005

# Interpolation with Polynomials and Splines

, 20 Oct 2005 CPOL
 Rate this:
A port of the Java sample project written by Tobias von Petersdorff

## Introduction

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) {
case(InterpolationMode.POLY):
// 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++)
{
yCoords[i]=(yCoords[i+1]-yCoords[i])/(xCoords[i+k]-xCoords[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++)
{
x=k*dt;
// evaluate polynomial at t
y = yCoords[0];
for (int i=1; i<=np-1; i++)
{
y=y*(x-xCoords[i])+yCoords[i];
}
// draw line

try
{
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);
}
oldx=x;
oldy=y;
}
break;
case(InterpolationMode.NAT_SPL):
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
oldx=xCoords[0];
oldy=yCoords[0];
try
{
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 +
(-a[i]/6*(x1+h[i])*x2+yCoords[i])*x1)/h[i];
x=xCoords[i-1]+x1;
if ((lastPen == null) || (lastPen == linePen2))
linePen = linePen1;
else
linePen = linePen2;
lastPen = linePen;

try
{
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);
}

oldx=x;
oldy=y;
}
}
}
break;
}```

## History

• 20th October, 2005: Initial post

## Share

 Web Developer Italy
No Biography provided

## You may also be interested in...

 First Prev Next
 Best Cubic Spline Interpolation example on the net. gollyw9-Jun-15 23:35 gollyw 9-Jun-15 23:35
 My vote of 5 Gun Gun Febrianza30-Oct-14 18:38 Gun Gun Febrianza 30-Oct-14 18:38
 How to get the tangent of a given point after the interpolation? Pi Zion27-Apr-14 18:20 Pi Zion 27-Apr-14 18:20
 My vote of 2 DanielSheets16-Sep-13 10:29 DanielSheets 16-Sep-13 10:29
 [My vote of 2] not enough explanation CIDev18-Jul-13 4:23 CIDev 18-Jul-13 4:23
 Interpolation with Polynomials and Splines Member 100400279-May-13 5:43 Member 10040027 9-May-13 5:43
 This rocks John S Battista16-Aug-12 16:54 John S Battista 16-Aug-12 16:54
 Not works when points are close one to another Retro_0128-May-10 2:27 Retro_01 28-May-10 2:27
 It works only in one dimension kuratowski10-Nov-09 13:52 kuratowski 10-Nov-09 13:52
 question for Marco Roello [modified] flashenoise30-May-06 1:42 flashenoise 30-May-06 1:42
 Nice. Did you try Graphics.DrawCurve() ? axelriet21-Oct-05 1:31 axelriet 21-Oct-05 1:31
 Re: Nice. Did you try Graphics.DrawCurve() ? Marco Roello21-Oct-05 2:39 Marco Roello 21-Oct-05 2:39
 Re: Nice. Did you try Graphics.DrawCurve() ? axelriet21-Oct-05 2:55 axelriet 21-Oct-05 2:55
 Thanks. How about the sources? _MMM_20-Oct-05 17:15 _MMM_ 20-Oct-05 17:15
 Re: Thanks. How about the sources? Anonymous20-Oct-05 21:32 Anonymous 20-Oct-05 21:32
 Last Visit: 31-Dec-99 19:00     Last Update: 26-Feb-17 17:57 Refresh 1

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

Web02 | 2.8.170217.1 | Last Updated 20 Oct 2005
Article Copyright 2005 by Marco Roello