Пример #1
0
        public IList <Bezier2> ToBeziers()
        {
            var expectedIdenticalKnots = Degree + 1;

            if (expectedIdenticalKnots != 4)
            {
                throw new NotSupportedException("Only cubic Bezier curves of 4 points are supported.");
            }

            var result = new Spline2(Degree, ControlPoints, KnotValues);

            for (int offset = 0; ; offset++)
            {
                // get next set of values
                var values = result.KnotValues.Skip(offset * expectedIdenticalKnots).Take(expectedIdenticalKnots).ToList();

                if (values.Count == 0 && result.KnotValues.Count() % expectedIdenticalKnots == 0)
                {
                    // done
                    break;
                }

                var expectedValue = values[0];
                int missingValueCount;
                if (values.Count < expectedIdenticalKnots)
                {
                    // not enough values
                    missingValueCount = expectedIdenticalKnots - values.Count;
                }
                else if (values.Count < expectedIdenticalKnots || values.Any(v => v != expectedValue))
                {
                    // not all the same
                    missingValueCount = expectedIdenticalKnots - values.Count(v => v == expectedValue);
                }
                else
                {
                    missingValueCount = 0;
                }

                for (int i = 0; i < missingValueCount; i++)
                {
                    result.InsertKnot(expectedValue);
                }
            }

            var points = result.ControlPoints.ToList();
            var curves = new List <Bezier2>();

            for (int startIndex = 0; startIndex < points.Count; startIndex += expectedIdenticalKnots)
            {
                var curve = new Bezier2(points[startIndex], points[startIndex + 1], points[startIndex + 2], points[startIndex + 3]);
                curves.Add(curve);
            }

            return(curves);
        }
Пример #2
0
        public static XElement ToXElement(this DxfSpline spline)
        {
            var spline2 = new Spline2(
                spline.DegreeOfCurve,
                spline.ControlPoints.Select(p => new SplinePoint2(p.Point.X, p.Point.Y)),
                spline.KnotValues);
            var beziers = spline2.ToBeziers();
            var path    = beziers.GetSvgPath();

            return(new XElement(DxfToSvgConverter.Xmlns + "path",
                                new XAttribute("d", path.ToString()),
                                new XAttribute("fill-opacity", 0))
                   .AddStroke(spline.Color)
                   .AddStrokeWidth(1.0)
                   .AddVectorEffect());
        }