/// <summary>
/// 直线插值
/// </summary>
/// <param name="linePoints">点击</param>
/// <param name="x">X插值点</param>
/// <returns>插值的Y值</returns>
public static double? LineInterpolation(List<PointF> linePoints, double x)
{
int? idx1 = null;
for (int index = 0; index < linePoints.Count; index++)
{
PointF linePoint = linePoints[index];
if (linePoint.X >= x)
{
idx1 = index;
break;
}
}
int? idx2 = null;
for (int index = linePoints.Count - 1; index >= 0; index--)
{
PointF linePoint = linePoints[index];
if (linePoint.X <= x)
{
idx2 = index;
break;
}
}
if (!idx1.HasValue || !idx2.HasValue)
{
return null;
}
PointF p1 = linePoints[idx1.Value];
PointF p2 = linePoints[idx2.Value];
if (idx1.Value == idx2.Value || idx1 == idx2)
{
return p1.Y;
}
double xDiff = p1.X - p2.X;
double yDiff = p1.Y - p2.Y;
double y = p1.Y + (x - p1.X) * yDiff / xDiff;
return y;
}