cloudflight
2024-06-08 531e9b2ccf0ddf87f776f43378650d5169292bcb
Hydraulic/Hydro.CommonBase/Helper/CurveFitHelper.cs
@@ -28,8 +28,29 @@
            this.data = data;
            double[] xlist = data.Select(p => (double)p.X).ToArray();
            coefficients = Fit.Polynomial(xlist, data.Select(p => (double)p.Y).ToArray(), 2);
            isFitted = true;
            if (data.Count>2)
            {
                coefficients = Fit.Polynomial(xlist, data.Select(p => (double)p.Y).ToArray(), 2);
                isFitted = true;
            }
            else if(data.Count==2)
            {
                //使用两个点的线性拟合
                var tuple = Fit.Line(xlist, data.Select(p => (double)p.Y).ToArray());
                coefficients= new DenseVector(new double[] { tuple.Item1, tuple.Item2 });
                isFitted = true;
            }
            else if (data.Count==1)
            {
                coefficients = new DenseVector(new double[] { data[0].Y });
                isFitted = true;
            }
            else
            {
                coefficients = new DenseVector(new double[] { 0});
                isFitted = true;
            }
            var list = xlist.ToList();
            xMax = list.Max();
            xMin = list.Min();
@@ -37,7 +58,7 @@
        public double Evaluate(double x)
        {
            while (!isFitted) ;
            if (!isFitted) return double.NaN;
            double y = 0;
            for (int i = 0; i < coefficients.Count; i++)
            {
@@ -50,7 +71,8 @@
        public List<PointF> GetFitCurve(int count)
        {
            List<PointF> curve = new List<PointF>();
            for (double x = xMin; x < xMax; x += (xMax - xMin) / count)
            for (double x = xMin; x <= xMax && curve.Count<count; x += (xMax - xMin) / count)
            {
                double y = Evaluate(x);
                curve.Add(new PointF((float)x, (float)y));