# Interpolating Z-Values when given complete and incomplete XYZ pairs

I am building an application that works with PolyLineZ (ESRI Shapefile) data and rewrites outlying Z values. The minimum and maximum Z-values are defined by the user through the interface

Let's take the following as an example, let's say the minimum is 0 and the maximum is 10:

XY      Z
1,1     0
1,3     1
1,5     7
1,7     11*
1,10    10


The value with the 11 would need to be interpolated as it does not fall into the range defined by the user. This is a very simplified example obviously. Some PolyLines can be missing more Z values.

What I've done:

I've researched linear interpolation. Looked at example youtube videos, having a hard time wrapping my head around it. It almost seems like what they're talking about doesn't apply to my scenario.

What I need:

An "English" explanation of the theory behind linear/bilinear/trilinear interpolation so that I can implement it into my program. My math skills aren't the greatest, so I have a hard time understanding wikipedias definition of it.

Examples work the best for me, well commented code examples make the most sense to me... but beggars can't be choosers.

I'm also assuming that linear interpolation is what I need to research?

First step, create a routine for calculating distances:

internal static double calculateDistance(XYPoints a, XYPoints b)
{
double xd = b.X - a.X;
double yd = b.Y - a.Y;
return Math.Sqrt(xd * xd + yd * yd);
}


I changed the variable names to something more logical (my variable names were different)

//get distance frpm previous point to point in question
double prevdistance = calculateDistance(prevXYpoint, currentXYPoint);
//get distance frpm point in question to the next point
double nextdistance = calculateDistance(currentXYPoint, nextXYPoint);
//generate a ratio
double fraction = prevdistance / (nextdistance + prevdistance);
//find out the difference between the two known points
double diffsBetweensZ = nextZpointValue - prevZpointValue;
//interpolate!
double newZvalue = (prevZpointValue + (diffsBetweensZ * fraction));


I checked this on several sets of data and it's the most accurate thing I can find... what blows my mind is that I couldn't find any existing code anywhere to do this.