Exemplo n.º 1
0
        /// <summary>
        /// Creates a simplified version of the curve by creating lines with a maximum length defined.
        /// </summary>
        /// <param name="curve">Curve to polygonize</param>
        /// <param name="maxLength">Maximum length of subdivisions</param>
        /// <param name="asPolycurve">If true returns a Polycurve or a list of lines otherwise.</param>
        /// <returns></returns>
        public static object Polygonize(DSCurve curve, double maxLength, bool asPolycurve = false)
        {
            //TODO : Look into http://www.antigrain.com/research/adaptive_bezier/index.html
            if (curve == null)
            {
                throw new ArgumentNullException("curve");
            }
            List <DSCurve> lines      = new List <DSCurve>();
            bool           isStraight = curve.Length.AlmostEqualTo(curve.StartPoint.DistanceTo(curve.EndPoint));

            if (isStraight)
            {
                lines.Add(curve);
            }
            else
            {
                int divisions = (int)Math.Ceiling(curve.Length / maxLength);
                if (divisions > 1)
                {
                    var points = curve.PointsAtEqualSegmentLength(divisions);
                    lines.Add(Line.ByStartPointEndPoint(curve.StartPoint, points.First()));
                    for (var i = 0; i < points.Count() - 1; i++)
                    {
                        lines.Add(Line.ByStartPointEndPoint(points[i], points[i + 1]));
                    }
                    lines.Add(Line.ByStartPointEndPoint(points.Last(), curve.EndPoint));
                }
                else
                {
                    lines.Add(Line.ByStartPointEndPoint(curve.StartPoint, curve.EndPoint));
                }
            }

            if (asPolycurve)
            {
                return(PolyCurve.ByJoinedCurves(lines));
            }
            else
            {
                return(lines);
            }
        }