C# 根据路线点集合动态分段

/// <summary>
        /// 将数据根据起、止点分段,返回结果点集合
        /// </summary>
        public ObservableCollection<Point> BuildDrawLineData(ObservableCollection<Point> MapPointList, string StartPeg, string StopPeg)
        {
            ObservableCollection<Point> Result = new ObservableCollection<Point>();
            double StartValue = this.MathPegNo(StartPeg);
            double StopValue = this.MathPegNo(StopPeg);
            if (StartValue > 1)
                MapPointList = this.SplitLineByStartLength(MapPointList, StartValue);
            //共截取的距离
            double ResultLength = StopValue - StartValue;
            //分段计算中的合计距离
            double TotalLength = 0.0;
            //分段计算中的段距离
            double FtrLength = 0.0;
            //计算中的点对象
            Point pt1,pt2;
            //循环点集合,并计算
            for (int i = 0;i< MapPointList.Count;i++)
            {
                if (i + 1 < MapPointList.Count)
                {
                    pt1 = MapPointList[i];
                    pt2 = MapPointList[i +1];
                    FtrLength = this.GetDistance(pt1.Y, pt1.X, pt2.Y, pt2.X);
                    TotalLength += FtrLength;
                    if (TotalLength < ResultLength)
                    {
                        //加入集合中点
                        Result.Add(pt1);
                    }
                    else
                    {
                        //超出后在当前直线上取点
                        double RemainderLength = 0;
                        //最后一段直线上的截取
                        RemainderLength = ResultLength - (TotalLength - FtrLength);
                        //由于一条街道上的计算 用三角比例
                        double x = Math.Abs(pt2.X - pt1.X);
                        double y = Math.Abs(pt2.Y - pt1.Y);
                        double x_x = x * (RemainderLength / FtrLength);
                        double y_y = y * (RemainderLength / FtrLength);
                        //判断方向并计算X
                        if (pt1.X > pt2.X)
                            x_x = pt1.X - x_x;
                        else
                            x_x = pt1.X + x_x;
                        //判断方向并计算Y
                        if (pt1.Y > pt2.Y)
                            y_y = pt1.Y - y_y;
                        else
                            y_y = pt1.Y + y_y;
                        //将最后的点加入结果
                        Result.Add(new Point(x_x, y_y));
                        break;
                    }
                }
                else//加入最后一个点
                    Result.Add(MapPointList.Last());
            }
            return Result;
        }

        //截取超点不为零的点集合
        private ObservableCollection<Point> SplitLineByStartLength(ObservableCollection<Point> MapPointList, double StartLength)
        {
            ObservableCollection<Point> Result = new ObservableCollection<Point>();
            //分段计算中的合计距离
            double TotalLength = 0.0;
            //分段计算中的段距离
            double FtrLength = 0.0;
            //计算中的点对象
            Point pt1, pt2;
            //提前量,是指超过起点距离的第一次检查
            bool IsFirst = true;
            //循环点集合,并计算
            for (int i = 0; i < MapPointList.Count; i++)
            {
                if (i + 1 < MapPointList.Count)
                {
                    pt1 = MapPointList[i];
                    pt2 = MapPointList[i + 1];
                    FtrLength = this.GetDistance(pt1.Y, pt1.X, pt2.Y, pt2.X);
                    TotalLength += FtrLength;
                    if (TotalLength > StartLength)
                    {
                        if (IsFirst)
                        {
                            IsFirst = false;
                            //超出后在当前直线上取点
                            double RemainderLength = 0;
                            //最后一段直线上的截取
                            RemainderLength = StartLength - (TotalLength - FtrLength);
                            //由于一条街道上的计算 用三角比例
                            double x = Math.Abs(pt2.X - pt1.X);
                            double y = Math.Abs(pt2.Y - pt1.Y);
                            double x_x = x * (RemainderLength / FtrLength);
                            double y_y = y * (RemainderLength / FtrLength);
                            //判断方向并计算X
                            if (pt1.X > pt2.X)
                                x_x = pt1.X - x_x;
                            else
                                x_x = pt1.X + x_x;
                            //判断方向并计算Y
                            if (pt1.Y > pt2.Y)
                                y_y = pt1.Y - y_y;
                            else
                                y_y = pt1.Y + y_y;
                            //将最后的点加入结果
                            Result.Add(new Point(x_x, y_y));
                        }
                        else//将起点后面的点集合进行整理
                            Result.Add(pt1);
                    }                       
                }
                else//加入最后一个点
                    Result.Add(MapPointList.Last());
            }
            return Result;
        }

原文地址:https://www.cnblogs.com/jiangu66/p/3181729.html