From c97331d8eca8f9ef6a0c18ffce3518578b5638ff Mon Sep 17 00:00:00 2001
From: lx <ex_lixiang17@cosl.com.cn>
Date: 星期五, 15 八月 2025 10:21:11 +0800
Subject: [PATCH] update cr/or fit line

---
 ErrorAnalysis.Service/Utility.cs |  125 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 125 insertions(+), 0 deletions(-)

diff --git a/ErrorAnalysis.Service/Utility.cs b/ErrorAnalysis.Service/Utility.cs
index 6c10062..e491bad 100644
--- a/ErrorAnalysis.Service/Utility.cs
+++ b/ErrorAnalysis.Service/Utility.cs
@@ -1,5 +1,6 @@
 锘縰sing System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -23,5 +24,129 @@
             double y = y0 + (value - x0) * (y1 - y0) / (x1 - x0);
             return y;
         }
+
+        public static (double slope, double intercept) FitLine(PointF[] points)
+        {
+            // 璁$畻鍚勬眰鍜岄」
+            int n = points.Length;
+            double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
+
+            foreach (var p in points)
+            {
+                sumX += p.X;
+                sumY += p.Y;
+                sumXY += p.X * p.Y;
+                sumX2 += p.X * p.X;
+            }
+
+            // 璁$畻鏂滅巼鍜屾埅璺�
+            double denominator = n * sumX2 - sumX * sumX;
+            if (Math.Abs(denominator) < 1e-10) // 閬垮厤闄や互0
+                throw new InvalidOperationException("鐐归泦杩囦簬闆嗕腑锛屾棤娉曟嫙鍚堢洿绾�");
+
+            double slope = (n * sumXY - sumX * sumY) / denominator;
+            double intercept = (sumY - slope * sumX) / n;
+
+            return (slope, intercept);
+        }
+
+        public static double GetLineValue(double x, double slope, double intercept) => slope * x + intercept;
+
+        public static (double a, double b, double c) FitParabola(PointF[] points)
+        {
+            if (points.Length < 3)
+                throw new ArgumentException("鑷冲皯闇�瑕�3涓偣杩涜鎶涚墿绾挎嫙鍚�");
+
+            // 1. 璁$畻鍚勯」姹傚拰鍊糩1,6](@ref)
+            int n = points.Length;
+            double sx = 0, sy = 0, sx2 = 0, sx3 = 0, sx4 = 0, sxy = 0, sx2y = 0;
+
+            foreach (var p in points)
+            {
+                double x = p.X;
+                double y = p.Y;
+                double x2 = x * x;
+                double x3 = x2 * x;
+                double x4 = x2 * x2;
+
+                sx += x;
+                sy += y;
+                sx2 += x2;
+                sx3 += x3;
+                sx4 += x4;
+                sxy += x * y;
+                sx2y += x2 * y;
+            }
+
+            // 2. 鏋勫缓姝h鏂圭▼鐭╅樀[1,6](@ref)
+            double[,] matrix = {
+            { sx4, sx3, sx2, sx2y },
+            { sx3, sx2, sx,  sxy  },
+            { sx2, sx,  n,   sy   }
+        };
+
+            // 3. 浣跨敤楂樻柉娑堝厓娉曟眰瑙�
+            double[] coefficients = GaussElimination(matrix);
+
+            return (coefficients[0], coefficients[1], coefficients[2]);
+        }
+
+        // 楂樻柉娑堝厓娉曞疄鐜帮紙甯﹂儴鍒嗕富鍏冮�夋嫨锛�
+        private static double[] GaussElimination(double[,] matrix)
+        {
+            int n = matrix.GetLength(0);
+            double[] result = new double[n];
+
+            // 鍓嶅悜娑堝厓
+            for (int i = 0; i < n - 1; i++)
+            {
+                // 閮ㄥ垎涓诲厓閫夋嫨
+                int maxRow = i;
+                for (int k = i + 1; k < n; k++)
+                {
+                    if (Math.Abs(matrix[k, i]) > Math.Abs(matrix[maxRow, i]))
+                        maxRow = k;
+                }
+
+                // 琛屼氦鎹�
+                if (maxRow != i)
+                {
+                    for (int j = 0; j <= n; j++)
+                    {
+                        double temp = matrix[i, j];
+                        matrix[i, j] = matrix[maxRow, j];
+                        matrix[maxRow, j] = temp;
+                    }
+                }
+
+                // 娑堝厓杩囩▼
+                for (int k = i + 1; k < n; k++)
+                {
+                    double factor = matrix[k, i] / matrix[i, i];
+                    for (int j = i; j <= n; j++)
+                    {
+                        matrix[k, j] -= factor * matrix[i, j];
+                    }
+                }
+            }
+
+            // 鍥炰唬姹傝В
+            for (int i = n - 1; i >= 0; i--)
+            {
+                result[i] = matrix[i, n];
+                for (int j = i + 1; j < n; j++)
+                {
+                    result[i] -= matrix[i, j] * result[j];
+                }
+                result[i] /= matrix[i, i];
+            }
+
+            return result;
+        }
+
+        public static double GetParabolaValue(double x, double a, double b, double c)
+        {
+            return a * x * x + b * x + c;
+        }
     }
 }

--
Gitblit v1.9.3