Exemplo n.º 1
0
        /// <summary>
        /// Splits the polygon to a list of points at given distances.
        /// </summary>
        /// <param name="length">The length.</param>
        /// <returns>List&lt;UV&gt;.</returns>
        public List <UV> SplitToPoints(double length)
        {
            List <UV> pnts = new List <UV>();

            pnts.Add(this.Start);
            double totalLength = 0;
            double u           = 0;

            for (int i = 0; i < this.PointCount; i++)
            {
                int j = (i == this.PointCount - 1) ? 0 : i + 1;
                if (!this.Closed && j == 0)
                {
                    break;
                }
                UV     vector = this.Points[j] - this.Points[i];
                double dist   = vector.GetLength();
                vector /= dist;
                while (u < totalLength + dist)
                {
                    pnts.Add(this.Points[i] + (u - totalLength) * vector);
                    u += length;
                }
                totalLength += dist;
            }
            if (this.End != pnts[pnts.Count - 1] && !this.Closed)
            {
                pnts.Add(this.End);
            }

            UV        current = pnts[0];
            List <UV> pnts2   = new List <UV>()
            {
                current
            };

            for (int i = 0; i < pnts.Count; i++)
            {
                if (pnts[i] != current)
                {
                    current = pnts[i];
                    pnts2.Add(current);
                }
            }
            pnts.Clear();
            pnts = null;
            return(pnts2);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Splits this polyline to a list of polylines with equal lengths
        /// </summary>
        /// <param name="length">The length.</param>
        /// <param name="tolerance">The tolerance by default set to main documents absolute tolerance.</param>
        /// <returns>List&lt;PLine&gt;.</returns>
        public List <PLine> SplitToPLines(double length, double tolerance = OSMDocument.AbsoluteTolerance)
        {
            List <int> indexes = new List <int>()
            {
                0
            };
            List <UV> pnts        = new List <UV>();
            double    totalLength = 0;
            double    u           = length;

            for (int i = 0; i < this.PointCount; i++)
            {
                pnts.Add(this.Points[i]);
                //indexes.Add(pnts.Count - 1);
                int j = (i == this.PointCount - 1) ? 0 : i + 1;
                if (!this.Closed && j == 0)
                {
                    break;
                }
                UV     vector = this.Points[j] - this.Points[i];
                double dist   = vector.GetLength();
                vector /= dist;
                while (u < totalLength + dist)
                {
                    var p = this.Points[i] + (u - totalLength) * vector;
                    if (!this._pntSet.Contains(p))
                    {
                        pnts.Add(p);
                    }
                    indexes.Add(pnts.Count - 1);
                    u += length;
                }
                totalLength += dist;
            }
            if (this.Closed)
            {
                if (this.Start != pnts[pnts.Count - 1])
                {
                    pnts.Add(this.Start);
                }
                indexes.Add(pnts.Count - 1);
            }
            else
            {
                if (this.End != pnts[pnts.Count - 1])
                {
                    pnts.Add(this.End);
                }
                indexes.Add(pnts.Count - 1);
            }
            List <PLine> plines = new List <PLine>();

            if (indexes.Count == 2)
            {
                plines.Add(this);
            }
            for (int i = 0; i < indexes.Count - 1; i++)
            {
                var points = pnts.GetRange(indexes[i], indexes[i + 1] - indexes[i] + 1);
                if (points[0].DistanceTo(points[1]) < tolerance)
                {
                    points.RemoveAt(1);
                }
                if (points.Count > 1)
                {
                    if (points[points.Count - 1].DistanceTo(points[points.Count - 2]) < tolerance)
                    {
                        points.RemoveAt(points.Count - 2);
                    }
                    if (points.Count > 1)
                    {
                        PLine pline = new PLine(points, false);
                        plines.Add(pline);
                    }
                }
            }
            return(plines);
        }