Shuxia Ning
2024-12-19 b0c978129ba55cf81e8470b6c9326745a5dbc7d1
WinFrmUI/Yw.WinFrmUI.Phart.Core/00-core/Point2dListExtensions.cs
@@ -48,7 +48,7 @@
        /// <summary>
        /// 修改曲线0点位置的值,曲线值会根据比例逐渐变化,直到fixPtX,不再变化
        /// </summary> 
        public static List<Yw.Geometry.Point2d> AmendByZeroPointY(this List<Yw.Geometry.Point2d> pt_list, double fix_point_x, double zero_point_y)
        public static List<Yw.Geometry.Point2d> AmendByZeroPointY(this List<Yw.Geometry.Point2d> pt_list, double fix_point_x, double zero_point_y,Yw.Ahart.eFeatType feat_type= Ahart.eFeatType.Cubic)
        {
            if (pt_list == null || pt_list.Count < 3)
                return null;
@@ -71,9 +71,26 @@
                fit_pt_list.Add(new Yw.Geometry.Point2d(pt_list[i].X, pt_list[i].Y - dis));
            }
            var cubic_spline = new Yw.Geometry.CubicSpline2d(fit_pt_list);
            return cubic_spline.GetPointList(pt_list.Count);
            var new_pt_list = new List<Yw.Geometry.Point2d>();
            switch (feat_type)
            {
                case Ahart.eFeatType.Polynomial:
                    {
                        new_pt_list=new Yw.Geometry.PolynomialSpline2d(fit_pt_list).GetPointList();
                    }
                    break;
                case Ahart.eFeatType.Cubic:
                    {
                        new_pt_list = new Yw.Geometry.CubicSpline2d(fit_pt_list).GetPointList();
                    }
                    break;
                case Ahart.eFeatType.Through2d:
                    {
                        new_pt_list = new Yw.Geometry.ThroughSpline2d(fit_pt_list).GetPointList();
                    }
                    break;
            }
            return new_pt_list;
        }
@@ -115,6 +132,22 @@
            return cubic_spline.GetPointList(point_count);
        }
        /// <summary>
        /// 计算总长度
        /// </summary>
        public static double  TotalLength(this IEnumerable<Yw.Geometry.Point2d> srcList)
        {
            if (srcList == null || srcList.Count() < 2)
                return default;
            var list = srcList.ToList();
            double length = 0;
            for (int i = 1; i < list.Count(); i++)
            {
                double len = Math.Sqrt((list[i].X - list[i - 1].X) * (list[i].X - list[i - 1].X) + (list[i].Y - list[i - 1].Y) * (list[i].Y - list[i - 1].Y));
                length += len;
            }
            return length;
        }
        //public static List<Yw.Geometry.Point2d> GetInterPointsX(this List<Yw.Geometry.Point2d> pt_list, double y)
        //{
@@ -138,5 +171,53 @@
        //    }
        //    return list;
        //}
        /// <summary>
        /// 根据周长度计算点位置,返回空表示超过总长
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static Yw.Geometry.Point2d GetPosiByLength(this List<Yw.Geometry.Point2d> srcList,double length_from_start)
        {
            if (length_from_start < 0.0001)
                return srcList.FirstOrDefault();
            double length = 0;
            for (int i = 1; i < srcList.Count; i++)
            {
                double len_line = Math.Sqrt((srcList[i].X - srcList[i - 1].X) * (srcList[i].X - srcList[i - 1].X) + (srcList[i].Y - srcList[i - 1].Y) * (srcList[i].Y - srcList[i - 1].Y));
                length += len_line;
                if (length >= length_from_start)
                {
                    Yw.Geometry.Point2d extPt = new Yw.Geometry.Point2d();
                    extPt.X = (srcList[i - 1].X - srcList[i].X) * (length - length_from_start) / len_line + srcList[i].X;
                    extPt.Y = (srcList[i - 1].Y - srcList[i].Y) * (length - length_from_start) / len_line + srcList[i].Y;
                    return extPt;
                }
            }
            if (length >= length_from_start * 0.9999)
            {
                return srcList.LastOrDefault();
            }
            return null;
        }
        /// <summary>
        /// 获取Y最小的点的序号
        /// </summary>
        /// <returns></returns>
        public static  int GetPointIndexOfMinY(this List<Yw.Geometry.Point2d> srcList )
        {
            if (srcList.Count() == 0)
                return 0;
            if (srcList.Count() == 1)
                return 0;
            var minY = (from x in srcList select x.Y).Min();
            var pt = (from x in srcList where x.Y == minY select x).FirstOrDefault();
            if (pt == null)
                return 0;
            else
                return srcList.IndexOf(pt);
        }
    }
}