From 531e9b2ccf0ddf87f776f43378650d5169292bcb Mon Sep 17 00:00:00 2001
From: cloudflight <cloudflight@126.com>
Date: 星期六, 08 六月 2024 11:59:03 +0800
Subject: [PATCH] 2024年6月8日

---
 Hydraulic/Hydro.CommonBase/Helper/CurveFitHelper.cs |   30 ++++++++++++++++++++++++++----
 1 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/Hydraulic/Hydro.CommonBase/Helper/CurveFitHelper.cs b/Hydraulic/Hydro.CommonBase/Helper/CurveFitHelper.cs
index eedff37..5a02a15 100644
--- a/Hydraulic/Hydro.CommonBase/Helper/CurveFitHelper.cs
+++ b/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));

--
Gitblit v1.9.3