ningshuxia
2024-05-24 7a89858cd237c4fc5d0c952804d35fcaa62be57d
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
namespace IStation.Curve
{
    public static class CurvePoint_Extensions
    {
 
        #region Point
 
        /// <summary>
        /// 判断是否是0点,用于判断函数是否成功返回
        /// </summary> 
        public static bool IsZeroPoint(this CurvePoint pt)
        {
            if (pt.X < 0.01 && pt.Y < 0.01)
                return true;
            else
                return false;
        }
 
        /// <summary>
        /// 点到线的距离
        /// </summary> 
        public static double GetDistance(this CurvePoint pt1, List<CurvePoint> CurvePoints)
        {
            if (CurvePoints == null)
                return 10000;
 
            double min_dis = double.MaxValue;
            double dis = 0;
            foreach (CurvePoint pt in CurvePoints)
            {
                dis = GetDistance(pt1, pt);
                if (dis < min_dis)
                    min_dis = dis;
            }
 
            return min_dis;
        }
 
        /// <summary>
        /// 两点之间的距离
        /// </summary> 
        public static double GetDistance(CurvePoint pt1, CurvePoint pt2)
        {
            return Math.Sqrt((pt1.X - pt2.X) * (pt1.X - pt2.X) + (pt1.Y - pt2.Y) * (pt1.Y - pt2.Y));
        }
 
 
        /// <summary>
        /// 计算总长度
        /// </summary>
        public static double CalcuTotalLength(this List<CurvePoint> ptList)
        {
            if (ptList == null || ptList.Count < 1)
            {
                return default;
            }
            double length = 0;
            for (int i = 1; i < ptList.Count; i++)
            {
                length += ptList[i].Distance(ptList[i - 1]);
            }
            return length;
        }
 
        #endregion
 
        #region List<CurvePoint>
 
        /// <summary>
        /// 获取Y最小的点
        /// </summary>
        public static CurvePoint GetPointByMinY(this List<CurvePoint> ptList)
        {
            if (ptList == null || ptList.Count < 1)
            {
                return default;
            }
            return ptList.OrderBy(x => x.Y).FirstOrDefault();
        }
 
        /// <summary>
        /// 获取Y最大的点
        /// </summary>
        public static CurvePoint GetPointByMaxY(this List<CurvePoint> ptList)
        {
            if (ptList == null || !ptList.Any())
            {
                return default;
            }
            return ptList.OrderBy(x => x.Y).LastOrDefault();
        }
 
        /// <summary>
        /// 获取Y最小的点索引
        /// </summary>
        public static int GetPointIndexByMinY(this List<CurvePoint> ptList)
        {
            var pt = GetPointByMinY(ptList);
            return ptList.IndexOf(pt);
        }
 
        /// <summary>
        /// 获取Y最大的点索引
        /// </summary>
        public static int GetPointIndexByMaxY(this List<CurvePoint> ptList)
        {
            var pt = GetPointByMaxY(ptList);
            return ptList.IndexOf(pt);
        }
 
        /// <summary>
        /// 根据X 获取点索引
        /// </summary>
        public static int GetPointIndexByX(this List<CurvePoint> ptList, double x)
        {
            if (ptList == null || !ptList.Any())
            {
                return -1;
            }
            int index = 0;
            foreach (var pt in ptList)
            {
                if (Math.Abs(pt.X - x) < 0.0001)
                {
                    return index;
                }
                index++;
            }
            return -1;
        }
 
        /// <summary>
        /// 获取X方向距离最小的点
        /// </summary>
        public static CurvePoint GetClosestPointByX(this List<CurvePoint> ptList, double x)
        {
            if (ptList == null || ptList.Count < 1)
            {
                return default;
            }
            var pt = ptList.OrderBy(t => Math.Abs(t.X - x)).FirstOrDefault();
            return pt;
        }
 
        /// <summary>
        /// 获取X方向距离最小的点索引
        /// </summary>
        public static int GetClosestPointIndexByX(this List<CurvePoint> ptList, double x)
        {
            var pt = GetClosestPointByX(ptList, x);
            return ptList.IndexOf(pt);
        }
 
 
        /// <summary>
        /// 获得Y值的坐标位置,可能有多个点(是线形插值,不是曲线上的点)
        /// </summary> 
        public static List<double> GetInterPointX(this List<CurvePoint> ptList, double y)
        {
            if (ptList == null || ptList.Count < 1)
            {
                return default;
            }
            var list = ptList.ToList();
            var equalPoints = new List<double>();
            int num = list.Count();
            for (int i = 0; i < num - 1; i++)//少一个点
            {
                if ((y >= list[i].Y && y <= list[i + 1].Y) || (y <= list[i].Y && y >= list[i + 1].Y))
                {//直线插值
                    double x;
                    if (Math.Abs(list[i].Y - list[i + 1].Y) < 0.01)
                        x = (list[i].X + list[i + 1].X) * 0.5;
                    else
                        x = list[i].X + (list[i + 1].X - list[i].X) * (y - list[i].Y) / (list[i + 1].Y - list[i].Y);
 
                    equalPoints.Add(x);
                }
            }
            return equalPoints;
        }
 
        /// <summary>
        /// 获得Y值的坐标位置,可能有多个点(是线形插值,不是曲线上的点)
        /// </summary> 
        public static List<CurvePoint> GetPointsByInterPointX(this List<CurvePoint> ptList, double y)
        {
            if (ptList == null || ptList.Count < 1)
            {
                return default;
            }
            var list = ptList.ToList();
            var equalPoints = new List<CurvePoint>();
            int num = list.Count();
            for (int i = 0; i < num - 1; i++)//少一个点
            {
                if ((y >= list[i].Y && y <= list[i + 1].Y) || (y <= list[i].Y && y >= list[i + 1].Y))
                {//直线插值
                    double x;
                    if (Math.Abs(list[i].Y - list[i + 1].Y) < 0.01)
                        x = (list[i].X + list[i + 1].X) * 0.5;
                    else
                        x = list[i].X + (list[i + 1].X - list[i].X) * (y - list[i].Y) / (list[i + 1].Y - list[i].Y);
 
                    equalPoints.Add(new CurvePoint(x, y));
                }
            }
            return equalPoints;
        }
 
        /// <summary>
        /// 获得X值的坐标位置,可能有多个点(是线形插值,不是曲线上的点)
        /// </summary>
        public static List<double> GetInterPointY(this List<CurvePoint> ptList, double x)
        {
            if (ptList == null || ptList.Count < 1)
            {
                return default;
            }
            var list = ptList.ToList();
            var equalPoints = new List<double>();
            int num = ptList.Count;
            for (int i = 0; i < num - 1; i++)//少一个点
            {
                if ((x >= list[i].X && x <= list[i + 1].X) || (x <= list[i].X && x >= list[i + 1].X))
                {//直线插值
                    double y;
                    if (Math.Abs(list[i].X - list[i + 1].X) < 0.01)
                        y = (list[i].Y + list[i + 1].Y) * 0.5;
                    else
                        y = list[i].Y + (list[i + 1].Y - list[i].Y) * (x - list[i].X) / (list[i + 1].X - list[i].X);
 
                    equalPoints.Add(y);
                }
            }
            return equalPoints;
        }
 
        /// <summary>
        /// 获得X值的坐标位置,可能有多个点(是线形插值,不是曲线上的点)
        /// </summary>
        public static List<CurvePoint> GetPointsByInterPointY(this List<CurvePoint> ptList, double x)
        {
            if (ptList == null || ptList.Count < 1)
            {
                return default;
            }
            var list = ptList.ToList();
            var equalPoints = new List<CurvePoint>();
            int num = ptList.Count;
            for (int i = 0; i < num - 1; i++)//少一个点
            {
                if ((x >= list[i].X && x <= list[i + 1].X) || (x <= list[i].X && x >= list[i + 1].X))
                {//直线插值
                    double y;
                    if (Math.Abs(list[i].X - list[i + 1].X) < 0.01)
                        y = (list[i].Y + list[i + 1].Y) * 0.5;
                    else
                        y = list[i].Y + (list[i + 1].Y - list[i].Y) * (x - list[i].X) / (list[i + 1].X - list[i].X);
 
                    equalPoints.Add(new CurvePoint(x, y));
                }
            }
            return equalPoints;
        }
 
 
 
        /// <summary>
        /// 获得X值的坐标位置,可能有多个点,取第一个点(是线形插值,不是曲线上的点)
        /// </summary> 
        public static bool GetInterPointY(this List<CurvePoint> points, double x, out double y)
        {
            y = 0;
            if (points == null)
                return false;
 
            int num = points.Count();
            for (int i = 0; i < num - 1; i++)//少一个点
            {
                if ((x >= points[i].X && x <= points[i + 1].X) || (x <= points[i].X && x >= points[i + 1].X))
                {//直线插值
                    if (Math.Abs(points[i].X - points[i + 1].X) < 0.01)
                        y = (points[i].Y + points[i + 1].Y) * 0.5;
                    else
                        y = points[i].Y + (points[i + 1].Y - points[i].Y) * (x - points[i].X) / (points[i + 1].X - points[i].X);
 
                    return true;
                }
            }
            return false;
        }
 
        /// <summary>
        /// ptList 是一个多边形,判断点是否在多边形里
        /// 原理是 通过p做一个+X方向的射线, 偶数个交点 就是在外部,奇数个就是在内部
        /// </summary>
        public static bool IsInPolygon(this IEnumerable<CurvePoint> ptList, CurvePoint p, bool isClosed = true)
        {
            if (ptList == null || ptList.Count() < 1)
                return default;
            var list = ptList.ToList();
            int crossNum = 0;
 
            for (int i = 0; i < list.Count() - 1; i++)
            {
                if (p.IsIntersectLineLeft(list[i], list[i + 1]))
                {
                    crossNum++;
                }
            }
 
            if (isClosed)
            {
                if (p.IsIntersectLineLeft(list[list.Count - 1], list[0]))
                {
                    crossNum++;
                }
            }
 
            if ((crossNum % 2) == 0)
                return false;
            else
                return true;
        }
 
        /// <summary>
        ///  是否完全相同
        /// </summary> 
        public static bool IsEqualValueX(this List<CurvePoint> ptList, List<CurvePoint> comparerPtList)
        {
            if (ptList == null || ptList.Count() == 0)
            {
                if (comparerPtList == null || comparerPtList.Count() == 0)
                {
                    return true;
                }
 
                return false;
            }
 
            if (comparerPtList == null || comparerPtList.Count() == 0)
            {
                if (ptList == null || ptList.Count() == 0)
                {
                    return true;
                }
 
                return false;
            }
 
            if (ptList.Count != comparerPtList.Count)
            {
                return false;
            }
 
            for (int i = 0; i < ptList.Count; i++)
            {
                if (Math.Abs(ptList[i].X - comparerPtList[i].X) > 0.001)
                {
                    return false;
                }
            }
 
            return true;
        }
 
 
        #region 获取拟合点
 
        /// <summary>
        /// 获取拟合点Y
        /// </summary>
        public static double GetFitPointY(this List<CurvePoint> ptList, double x)
        {
            if (ptList == null)
                return default;
            var express = FitHelper.BuildCurveExpress(ptList, eFitType.CubicCurve);
            if (express == null)
                return default;
            return express.GetFitPointY(x);
        }
 
        /// <summary>
        /// 获取拟合点Y
        /// </summary>
        public static double GetFitPointY(this List<CurvePoint> ptList, eFitType fitType, double x)
        {
            if (ptList == null)
                return default;
            var express = FitHelper.BuildCurveExpress(ptList, fitType);
            if (express == null)
                return default;
            return express.GetFitPointY(x);
        }
 
        /// <summary>
        /// 获取拟合点
        /// </summary>
        public static List<CurvePoint> GetFitPoints(this List<CurvePoint> ptList, eFitType fitType = eFitType.CubicCurve, int pointNumber = 20)
        {
            if (ptList == null)
                return default;
            var express = FitHelper.BuildCurveExpress(ptList, fitType);
            if (express == null)
                return default;
            return express.GetFitPoints(pointNumber);
        }
 
 
        /// <summary>
        /// 获取拟合点
        /// </summary>
        public static List<CurvePoint> GetFitPointsByExtend(this List<CurvePoint> ptList, eFitType fitType, double ratioExtend, int pointNumber = 20)
        {
            if (ptList == null)
                return default;
            var fitPow = FitHelper.GetFitPow(fitType);
            if (ptList.Count < fitPow + 1)
                return default;
            var express = FitHelper.BuildCurveExpress(ptList, fitType);
            return express.GetFitPointsByExtend(ratioExtend, pointNumber);
        }
 
        #endregion
 
        #region 取Y的最大最小值
 
 
        /// <summary>
        /// 取Y的最大最小值
        /// </summary> 
        public static bool GetMinMaxPointY(this List<CurvePoint> ptList, out double maxY, out double minY)
        {
            if (ptList == null || !ptList.Any())
            {
                maxY = minY = 0;
                return false;
            }
            var fitPoints = ptList.ToList();
            maxY = fitPoints.Max(x => x.Y);
            minY = fitPoints.Min(x => x.Y); ;
            return true;
        }
 
        /// <summary>
        /// 取points中Y的最大值(一定在points中)
        /// </summary> 
        public static CurvePoint GetMaxPointY(this List<CurvePoint> ptList)
        {
            if (ptList == null || !ptList.Any())
            {
                return new CurvePoint(0, 0);
            }
            return ptList.OrderByDescending(x => x.Y).First();
        }
 
        #endregion 取Y的最大最小值
 
 
        #endregion
    }
}