namespace IStation.Curve
|
{
|
/// <summary>
|
/// 不拟合
|
/// </summary>
|
public class TwinRelateCurve0 : IFitPoint
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public TwinRelateCurve0(CurveExpress express)
|
{
|
_express = express;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
public TwinRelateCurve0(List<CurvePoint> points)
|
{
|
if (points == null || points.Count < 4)
|
{
|
throw new Exception("出现数值计算问题, 无法解出不拟合线!");
|
}
|
_express = BuildCurveExpress(points);
|
}
|
|
//曲线表达式
|
private CurveExpress _express = null;
|
|
|
/// <summary>
|
/// 获取拟合点Y
|
/// </summary>
|
public double GetFitPointY(double x)
|
{
|
if (_express == null)
|
{
|
return default;
|
}
|
//尽量用贝塞尔曲线
|
if (_express.DefinePoints != null && _express.DefinePoints.Count > 4)
|
{
|
var b = BezierCurveHelper.CreateOpenCurves(_express.DefinePoints);
|
var s = BezierCurveHelper.GetSectPointsByX(b, x);
|
if (s != null && s.Count > 0)
|
{
|
return s.Last().Y;
|
}
|
}
|
|
if (_express.DefinePoints != null && _express.DefinePoints.Count > 1)
|
{
|
var sect = _express.DefinePoints.GetInterPointY(x);
|
if (sect == null || sect.Count < 1)
|
{
|
return default;
|
}
|
return sect.First();
|
}
|
return default;
|
}
|
|
/// <summary>
|
/// 获取拟合点列表
|
/// </summary>
|
public List<CurvePoint> GetFitPoints(int pointNumber)
|
{
|
if (_express == null)
|
{
|
return default;
|
}
|
return _express.DefinePoints;
|
}
|
|
/// <summary>
|
/// 通过X区间获取拟合点列表
|
/// </summary>
|
public List<CurvePoint> GetFitPointsByXRange(double minX, double maxX, int pointNumber)
|
{
|
if (_express == null)
|
{
|
return default;
|
}
|
return _express.DefinePoints;
|
}
|
|
/// <summary>
|
/// 创建曲线表达式
|
/// </summary>
|
public static CurveExpress BuildCurveExpress(List<CurvePoint> points)
|
{
|
if (points == null || points.Count < 4)
|
{
|
return default;
|
}
|
points = points.OrderBy(x => x.X).ToList();
|
var express = new CurveExpress
|
{
|
FitType = eFitType.ThroughPoint,
|
DefinePoints = points.Select(x => new CurvePoint(x)).ToList()
|
};
|
express.Min = points.Min(x => x.X);
|
express.Max = points.Max(x => x.X);
|
|
return express;
|
}
|
|
}
|
}
|