Sei sulla pagina 1di 3

/****************************************************************************

Below is implementation for a linear interpolation algorithm for generating


curves. The
algorithm uses a Newton table to generate the coefficients for a polynomial.
The polynomial is
then used to calculate the final x and y coordinates that will draw the
curve. There is a Xlist
for the x coordinates and a Ylist for the y coordinates.
example:
Given the table: [1] [6]
[(8-6)/(3-1)] = [1]
[3] [8]
[(1-1)/(5-1)] = [0]
for the polynomial are: 6,1,0
[(10-8)/(5-3)] = [1]
[5] [10]

The coefficients

Once we find the coefficients, we need to find Ni(T) which is 1*(TT0)*...*(T-Ti-1) elements in
the Newton Basis. From there we need to multiply Ni(t) with all of the
desired coefficients from the newton table to get our final x,y coordinates
for the interpolating polynomial curve.
****************************************************************************/
/****************************************************************************
Function to recursively calculate the coefficients from the newton table
***************************************************************************/
private double NewtonRecursive(int column, int row, bool Xlist)
{
if (Xlist)
{
//this is the first column which contains the coordinates of the control
points
if (column == 0)
{
return NewtonXlist[column][row];
}
//the formula for calculating the coefficient in the table(see above
example)
NewtonXlist[column][row] = (NewtonRecursive(column - 1, row + 1,
true) - NewtonRecursive(column - 1, row, true)) / column;
return NewtonXlist[column][row];
}
//We're using the y list to get the y coordinates from the polynomial
else
{
if (column == 0)

{
return NewtonYlist[column][row];
}
NewtonYlist[column][row] = (NewtonRecursive(column - 1,
row + 1, false) - NewtonRecursive(column - 1, row,
false))/column;
return NewtonYlist[column][row];
}
}
/****************************************************************************
This function is used to recalculate the newton table if we happen to
add another point to it.If another point is added, we add one extra element
to the bottom of each column in the table
***************************************************************************/
private void CalcNewtonCoefficent(List<Point2D> cPs)
{
//we need to clear our lists since we're adding a new point
int PointNum = cPs.Count;
NewtonXlist.Clear();
NewtonYlist.Clear();
//construct the list and initalize all elements to zero
while(PointNum > 0)
{
NewtonXlist.Add(new List<double>());
NewtonYlist.Add(new List<double>());
for (int i = 0; i < PointNum; ++i)
{
//Add another element to the table where the new point will added for
calculation
NewtonXlist[NewtonXlist.Count - 1].Add(0.0);
NewtonYlist[NewtonYlist.Count - 1].Add(0.0);
}
//Move onto the next column and add another element to it
--PointNum;
}
//Add the original points into the table
for (int i = 0; i < cPs.Count; ++i)
{
NewtonXlist[0][i] = pts_[i].x;
NewtonYlist[0][i] = pts_[i].y;
}
//do recursive part

if (NewtonXlist.Count > 0)
{
//x list
NewtonRecursive(NewtonXlist.Count - 1, 0, true);
}
if (NewtonYlist.Count > 0)
{
//y list
NewtonRecursive(NewtonYlist.Count - 1, 0, false);
}
}
/****************************************************************************
Main function which will return the final x and y coordinates from the
newton calculation
***************************************************************************/
private Point2D PolyInterpolate(float t)
{
double xCoord = 0.0;
double yCoord = 0.0;
double NewtonVal = 1.0;
//change the T base to be between 1 to 100 instead of 0 to 1
t = t * (pts_.Count - 1);
//This is when we only have 1 point for our calcualtion (n0(t) = 1)
xCoord = (NewtonXlist[0][0]);
yCoord = (NewtonYlist[0][0]);
//Following the formula at the top, we use the sum of Ni(T) and up to (t-ti)
to find the final x and y coordinates
for (int i = 1; i < pts_.Count; ++i)
{
//calculate the sum of (t-ti)
NewtonVal *= (t - (i - 1));
//gets current Ni(T) and does coefficient * Ni(T)
xCoord += (NewtonXlist[i][0] * NewtonVal);
yCoord += (NewtonYlist[i][0] * NewtonVal);
}
//return the final x and y coordinates to add to the interpolating
polynomial
curve
return new Point2D((float)xCoord, (float)yCoord);
}

Potrebbero piacerti anche