protected override void UpdateData()
        {
            var geometry = (PathGeometry)Data;
            geometry.Figures.Clear();

            if (ParentMap != null && Locations != null && Locations.Any())
            {
                var points = Locations.Select(l => ParentMap.MapTransform.Transform(l));

                var figure = new PathFigure
                {
                    StartPoint = points.First(),
                    IsClosed = IsClosed,
                    IsFilled = IsClosed
                };

                var segment = new PolyLineSegment();

                foreach (var point in points.Skip(1))
                {
                    segment.Points.Add(point);
                }

                figure.Segments.Add(segment);
                geometry.Figures.Add(figure);
                geometry.Transform = ParentMap.ViewportTransform;
            }
            else
            {
                geometry.ClearValue(Geometry.TransformProperty);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Generate PathGeometry object with curved Path using supplied route points
        /// </summary>
        /// <param name="points">Route points</param>
        /// <param name="tension"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        public static PolyLineSegment GetCurveThroughPoints(Point[] points, Double tension, Double tolerance)
        {
            Debug.Assert(points != null);
            Debug.Assert(points.Length >= 2);
            Debug.Assert(tolerance > 0);

            var oPolyLineSegment = new PolyLineSegment();

            if (points.Length == 2)
            {
                AddPointsToPolyLineSegment(oPolyLineSegment, points[0], points[0],
                    points[1], points[1], tension, tolerance);
            }
            else
            {
                var iPoints = points.Length;

                for (var i = 0; i < iPoints; i++)
                {
                    if (i == 0)
                    {
                        AddPointsToPolyLineSegment(oPolyLineSegment, points[0],
                            points[0], points[1], points[2], tension, tolerance);
                    }

                    else if (i == iPoints - 2)
                    {
                        AddPointsToPolyLineSegment(oPolyLineSegment, points[i - 1],
                            points[i], points[i + 1], points[i + 1], tension,
                            tolerance);
                    }
                    else if (i != iPoints - 1)
                    {
                        AddPointsToPolyLineSegment(oPolyLineSegment, points[i - 1],
                            points[i], points[i + 1], points[i + 2], tension,
                            tolerance);
                    }
                }
                oPolyLineSegment.Points.Insert(0, points[0]);
            }

            return oPolyLineSegment;
        }
        protected override void UpdateData()
        {
            var geometry = (PathGeometry)Data;
            var locations = Locations;
            Location first;

            if (ParentMap != null && locations != null && (first = locations.FirstOrDefault()) != null)
            {
                var figure = new PathFigure
                {
                    StartPoint = ParentMap.MapTransform.Transform(first),
                    IsClosed = IsClosed,
                    IsFilled = IsClosed
                };

                var segment = new PolyLineSegment();

                foreach (var location in locations.Skip(1))
                {
                    segment.Points.Add(ParentMap.MapTransform.Transform(location));
                }

                if (segment.Points.Count > 0)
                {
                    figure.Segments.Add(segment);
                }

                geometry.Figures.Clear();
                geometry.Figures.Add(figure);
                geometry.Transform = ParentMap.ViewportTransform;
            }
            else
            {
                geometry.Figures.Clear();
                geometry.ClearValue(Geometry.TransformProperty);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Creates a path geometry from a polygon.
        /// </summary>
        public static PathGeometry CreatePolygonGeometry(SysPoint[] points, XFillMode fillMode, bool closed)
        {
            PolyLineSegment seg = new PolyLineSegment();
            int count = points.Length;
            // For correct drawing the start point of the segment must not be the same as the first point.
            for (int idx = 1; idx < count; idx++)
                seg.Points.Add(new SysPoint(points[idx].X, points[idx].Y));
#if !SILVERLIGHT &&  !NETFX_CORE
            seg.IsStroked = true;
#endif
            PathFigure fig = new PathFigure();
            fig.StartPoint = new SysPoint(points[0].X, points[0].Y);
            fig.Segments.Add(seg);
            fig.IsClosed = closed;
            PathGeometry geo = new PathGeometry();
            geo.FillRule = fillMode == XFillMode.Winding ? FillRule.Nonzero : FillRule.EvenOdd;
            geo.Figures.Add(fig);
            return geo;
        }
Beispiel #5
0
        /// <summary>
        /// Creates a path geometry from a polygon.
        /// </summary>
        public static PolyLineSegment CreatePolyLineSegment(SysPoint[] points, XFillMode fillMode, bool closed)
        {
            PolyLineSegment seg = new PolyLineSegment();
            int count = points.Length;
            // For correct drawing the start point of the segment must not be the same as the first point.
            for (int idx = 1; idx < count; idx++)
                seg.Points.Add(new SysPoint(points[idx].X, points[idx].Y));
#if !SILVERLIGHT && !NETFX_CORE
            seg.IsStroked = true;
#endif
            return seg;
        }
Beispiel #6
0
        /// <summary>
        /// Adds a polygon to this path.
        /// </summary>
        public void AddPolygon(XPoint[] points)
        {
#if CORE
            int count = points.Length;
            if (count == 0)
                return;

            _corePath.MoveTo(points[0].X, points[0].Y);
            for (int idx = 0; idx < count - 1; idx++)
                _corePath.LineTo(points[idx].X, points[idx].Y, false);
            _corePath.LineTo(points[count - 1].X, points[count - 1].Y, true);
            _corePath.CloseSubpath();
#endif
#if GDI
            try
            {
                Lock.EnterGdiPlus();
                _gdipPath.AddPolygon(XGraphics.MakePointFArray(points));
            }
            finally { Lock.ExitGdiPlus(); }
#endif
#if WPF || NETFX_CORE
#if !SILVERLIGHT && !NETFX_CORE
            _pathGeometry.AddGeometry(GeometryHelper.CreatePolygonGeometry(XGraphics.MakePointArray(points), XFillMode.Alternate, true));
#else
            var figure = new PathFigure();
            figure.StartPoint = new SysPoint(points[0].X, points[0].Y);
            figure.IsClosed = true;

            PolyLineSegment segment = new PolyLineSegment();
            int count = points.Length;
            // For correct drawing the start point of the segment must not be the same as the first point.
            for (int idx = 1; idx < count; idx++)
                segment.Points.Add(new SysPoint(points[idx].X, points[idx].Y));
#if !SILVERLIGHT && !NETFX_CORE
            seg.IsStroked = true;
#endif
            figure.Segments.Add(segment);
            _pathGeometry.Figures.Add(figure);
#endif
            // TODO: NOT NEEDED
            //CloseFigure(); // StartFigure() isn't needed because AddGeometry() implicitly starts a new figure, but CloseFigure() is needed for the next adding not to continue this figure.
#endif
        }
 /// <summary>
 /// Returns new PolyLineSegment by easing startValue to endValue using a time percentage 0 -> 1.
 /// </summary>
 /// <example>XAML: Points="50,100 50,150"</example>
 /// <seealso cref="http://msdn.microsoft.com/en-us/library/system.windows.media.polylinesegment.aspx"/>
 public static PolyLineSegment EaseValue(PolyLineSegment startValue, PolyLineSegment endValue, double percent)
 {
     return new PolyLineSegment
     {
         Points = EaseValue(startValue.Points, endValue.Points, percent),
     };
 }
Beispiel #8
0
        private static void AddPointsToPolyLineSegment(PolyLineSegment oPolyLineSegment, Point oPoint0, Point oPoint1, Point oPoint2, Point oPoint3, Double dTension, Double dTolerance)
        {
            Debug.Assert(oPolyLineSegment != null);
            Debug.Assert(dTolerance > 0);

            var iPoints = (Int32)((Math.Abs(oPoint1.X - oPoint2.X) +
                Math.Abs(oPoint1.Y - oPoint2.Y)) / dTolerance);

            var oPolyLineSegmentPoints = oPolyLineSegment.Points;

            if (iPoints <= 2)
            {
                oPolyLineSegmentPoints.Add(oPoint2);
            }
            else
            {
                var dSx1 = dTension * (oPoint2.X - oPoint0.X);
                var dSy1 = dTension * (oPoint2.Y - oPoint0.Y);
                var dSx2 = dTension * (oPoint3.X - oPoint1.X);
                var dSy2 = dTension * (oPoint3.Y - oPoint1.Y);

                var dAx = dSx1 + dSx2 + 2 * oPoint1.X - 2 * oPoint2.X;
                var dAy = dSy1 + dSy2 + 2 * oPoint1.Y - 2 * oPoint2.Y;
                var dBx = -2 * dSx1 - dSx2 - 3 * oPoint1.X + 3 * oPoint2.X;
                var dBy = -2 * dSy1 - dSy2 - 3 * oPoint1.Y + 3 * oPoint2.Y;

                var dCx = dSx1;
                var dCy = dSy1;
                var dDx = oPoint1.X;
                var dDy = oPoint1.Y;

                // Note that this starts at 1, not 0.

                for (var i = 1; i < iPoints; i++)
                {
                    var t = (Double)i / (iPoints - 1);

                    var oPoint = new Point(
                        dAx * t * t * t + dBx * t * t + dCx * t + dDx,
                        dAy * t * t * t + dBy * t * t + dCy * t + dDy
                        );

                    oPolyLineSegmentPoints.Add(oPoint);
                }

            }
        }
Beispiel #9
-1
        public PathGeometry CloneDeep(PathGeometry pathGeometry)
        {
            var newPathGeometry = new PathGeometry();
            foreach (var figure in pathGeometry.Figures)
            {
                var newFigure = new PathFigure();
                newFigure.StartPoint = figure.StartPoint;
                // Even figures have to be deep cloned. Assigning them directly will result in
                //  an InvalidOperationException being thrown with the message "Element is already the child of another element."
                foreach (var segment in figure.Segments)
                {
                    // I only impemented cloning the abstract PathSegments to one implementation, 
                    //  the PolyLineSegment class. If your paths use other kinds of segments, you'll need
                    //  to implement that kind of coding yourself.
                    var segmentAsPolyLineSegment = segment as PolyLineSegment;
                    if (segmentAsPolyLineSegment != null)
                    {
                        var newSegment = new PolyLineSegment();
                        foreach (var point in segmentAsPolyLineSegment.Points)
                        {
                            newSegment.Points.Add(point);
                        }
                        newFigure.Segments.Add(newSegment);
                    }

                    var segmentAsLineSegment = segment as LineSegment;
                    if (segmentAsLineSegment != null)
                    {
                        var newSegment = new LineSegment();
                        newSegment.Point = segmentAsLineSegment.Point;
                        newFigure.Segments.Add(newSegment);
                    }

                    var segmentAsArcSegment = segment as ArcSegment;
                    if (segmentAsArcSegment != null)
                    {
                        var newSegment = new ArcSegment();
                        newSegment.Point = segmentAsArcSegment.Point;
                        newSegment.SweepDirection = segmentAsArcSegment.SweepDirection;
                        newSegment.RotationAngle = segmentAsArcSegment.RotationAngle;
                        newSegment.IsLargeArc = segmentAsArcSegment.IsLargeArc;
                        newSegment.Size = segmentAsArcSegment.Size;
                        newFigure.Segments.Add(newSegment);
                    }
                }
                newPathGeometry.Figures.Add(newFigure);
            }
            return newPathGeometry;
        }