// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System; using System.Collections.Generic; using System.Linq; namespace SixLabors.ImageSharp.Drawing { /// /// Convenience methods that can be applied to shapes and paths. /// public static partial class PathExtensions { /// /// Create a path with the segment order reversed. /// /// The path to reverse. /// The reversed . internal static IPath Reverse(this IPath path) { // TODO. Make this a void. We can reverse the segments in place and then reverse the points in place as well. IEnumerable segments = path.Flatten().Select(static p => new LinearLineSegment(ReversePoints(p.Points.Span))); bool closed = false; if (path is ISimplePath sp) { closed = sp.IsClosed; } return closed ? new Polygon(segments) : new Path(segments); } private static PointF[] ReversePoints(ReadOnlySpan points) { PointF[] reversed = new PointF[points.Length]; for (int i = 0; i < reversed.Length; i++) { reversed[i] = points[points.Length - 1 - i]; } return reversed; } } }