tangxu
2022-10-26 fdddb786ac2cd2b1d89bab2932aedfec4d05e358
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 
namespace IStation.Model
{
    /// <summary>
    /// 样条曲线(二次拟合)B=0
    /// </summary>
    public class TwinRelateCurve2B0 : IFitCurvePoint
    {
        public TwinRelateCurve2B0(CurveExpress express)
        {
            _express = express;
        }
        public TwinRelateCurve2B0(List<Model.CurvePoint> points)
        {
            if (points == null || points.Count < 3)
            {
                throw new Exception("出现数值计算问题, 无法解出二次拟合曲线(B=0)!");
            }
            _express = BuildCurveExpress(points);
        }
 
        private CurveExpress _express = null;
 
        /// <summary>
        /// 创建曲线表达式
        /// </summary>
        public static CurveExpress BuildCurveExpress(List<CurvePoint> points)
        {
            if (points == null || points.Count < 3)
            {
                return default;
            }
            points = points.OrderBy(x => x.X).ToList();
            var count = points.Count;
            var express = new CurveExpress();
            express.FitPow = 2;
            express.FitType = eCurveFitType.ConicCurveB0;
            express.DefinePoints = points.Select(x => new CurvePoint(x)).ToList();
            express.Min = points.First().X;
            express.Max = points.Last().X;
 
            double rTotalH = 0.0;
            double rTotalHQ2 = 0.0;
            double rTotalQ2 = 0.0;
            double rTotalQ4 = 0.0;
 
            for (int i = 0; i < count; i++)
            {
                rTotalH = rTotalH + points[i].Y;
                rTotalHQ2 = rTotalHQ2 + points[i].Y * points[i].X * points[i].X;
                rTotalQ2 = rTotalQ2 + points[i].X * points[i].X;
                rTotalQ4 = rTotalQ4 + Math.Pow(points[i].X, 4);
            }
            express.Index2 = (rTotalH * rTotalQ2 - count * rTotalHQ2) / ((rTotalQ2 * rTotalQ2) - count * rTotalQ4);
            express.Index0 = (rTotalQ2 * rTotalHQ2 - rTotalQ4 * rTotalH) / ((rTotalQ2 * rTotalQ2) - count * rTotalQ4);
 
            return express;
        }
 
        /// <summary>
        /// 获取拟合点Y
        /// </summary>
        public double GetFitPointY(double x)
        {
            if (_express == null)
                return default;
            return _express.Index2 * x * 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 < 1)
                pointNumber = 12;
            if (pointNumber > 10000)
                pointNumber = 10000;
 
            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;
        }
 
 
        /// <summary>
        /// 通过区间获取拟合点列表
        /// </summary>
        public static List<CurvePoint> GetFitPointsByRange(CurveExpress express, double x_min, double x_max, int pointNumber)
        {
            if (express == null)
                return default;
            return new TwinRelateCurve2B0(express).GetFitPointsByRange(x_min,x_max,pointNumber);
        }
 
        /// <summary>
        /// 获取拟合点
        /// </summary>
        public static List<CurvePoint> GetFitPoints(CurveExpress express, int pointNumber)
        {
            if (express == null)
                return default;
            return GetFitPointsByRange(express, express.Min, express.Max, pointNumber);
        }
 
 
 
    }
}