using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.Model
|
{
|
/// <summary>
|
/// 直线
|
/// </summary>
|
public class TwinRelateCurve1 : IFitCurvePoint
|
{
|
public TwinRelateCurve1(CurveExpress express)
|
{
|
_express = express;
|
}
|
public TwinRelateCurve1(List<Model.CurvePoint> points)
|
{
|
if (points == null || points.Count < 2)
|
{
|
throw new Exception("出现数值计算问题, 无法解出直线!");
|
}
|
|
_express = BuildCurveExpress(points);
|
}
|
|
private CurveExpress _express = null;
|
|
/// <summary>
|
/// 创建曲线表达式
|
/// </summary>
|
public static CurveExpress BuildCurveExpress(List<CurvePoint> points)
|
{
|
if (points == null || points.Count < 2)
|
{
|
return default;
|
}
|
points = points.OrderBy(x => x.X).ToList();
|
if (!CurveLineHelper.GetKandB(points.First(), points.Last(), out double k, out double b))
|
{
|
return default;
|
}
|
var express = new CurveExpress();
|
express.FitPow = 1;
|
express.FitType = eCurveFitType.LinearCurve;
|
express.DefinePoints = points.Select(x => new CurvePoint(x)).ToList();
|
express.Min = points.First().X;
|
express.Max = points.Last().X;
|
express.Index0 = b;
|
express.Index1 = k;
|
|
return express;
|
}
|
|
/// <summary>
|
/// 获取拟合点Y
|
/// </summary>
|
public double GetFitPointY(double x)
|
{
|
if (_express == null)
|
return default;
|
return _express.Index1 * x + _express.Index0;
|
}
|
|
/// <summary>
|
/// 获取拟合点列表
|
/// </summary>
|
public List<CurvePoint> GetFitPoints(int pointNumber)
|
{
|
if (_express == null)
|
return default;
|
return GetFitPointsByRange(_express.Min, _express.Max, pointNumber);
|
}
|
|
/// <summary>
|
/// 通过区间获取拟合点列表
|
/// </summary>
|
public List<CurvePoint> GetFitPointsByRange(double x_min, double x_max, int pointNumber)
|
{
|
if (_express == null)
|
return default;
|
if (pointNumber < 2)
|
pointNumber = 2;
|
if (pointNumber > 100)
|
pointNumber = 100;
|
|
double space = (x_max - x_min) / (pointNumber - 1);
|
if (space < 0.0001)
|
return default;
|
var points = new List<CurvePoint>();
|
for (int i = 0; i < pointNumber; i++)
|
{
|
double x = space * i + x_min;
|
double y = GetFitPointY(x);
|
points.Add(new CurvePoint(x, y));
|
}
|
return points;
|
}
|
}
|
}
|