程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 根據路線點集合動態分段

C# 根據路線點集合動態分段

編輯: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;
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved