/// <summary> /// Generates a solid outline of the path. /// </summary> /// <param name="path">the path to outline</param> /// <param name="width">The final width outline</param> /// <param name="jointStyle">The style to render the joints.</param> /// <param name="endCapStyle">The style to render the end caps of open paths (ignored on closed paths).</param> /// <returns>A new path representing the outline.</returns> public static IPath GenerateOutline(this IPath path, float width, JointStyle jointStyle = JointStyle.Square, EndCapStyle endCapStyle = EndCapStyle.Square) { var offset = new ClipperOffset() { MiterLimit = MiterOffsetDelta }; JoinType style = Convert(jointStyle); EndType openEndCapStyle = Convert(endCapStyle); // Pattern can be applied to the path by cutting it into segments IEnumerable <ISimplePath> paths = path.Flatten(); foreach (ISimplePath p in paths) { IReadOnlyList <PointF> vectors = p.Points; var points = new List <IntPoint>(vectors.Count); foreach (Vector2 v in vectors) { points.Add(new IntPoint(v.X * ScalingFactor, v.Y * ScalingFactor)); } EndType type = p.IsClosed ? EndType.etClosedLine : openEndCapStyle; offset.AddPath(points, style, type); } return(ExecuteOutliner(width, offset)); }
/// <summary> /// Generates an outline of the path. /// </summary> /// <param name="path">The path to outline</param> /// <param name="width">The outline width.</param> /// <param name="jointStyle">The style to apply to the joints.</param> /// <param name="endCapStyle">The style to apply to the end caps.</param> /// <returns>A new <see cref="IPath"/> representing the outline.</returns> /// <exception cref="ClipperException">Thrown when an offset cannot be calculated.</exception> public static IPath GenerateOutline(this IPath path, float width, JointStyle jointStyle, EndCapStyle endCapStyle) { ClipperOffset offset = new(MiterOffsetDelta); offset.AddPath(path, jointStyle, endCapStyle); return(offset.Execute(width)); }
private static JoinType Convert(JointStyle style) { switch (style) { case JointStyle.Round: return(JoinType.jtRound); case JointStyle.Miter: return(JoinType.jtMiter); case JointStyle.Square: default: return(JoinType.jtSquare); } }
private static void OutputStarOutlineDashed(int points, float inner = 10, float outer = 20, float width = 5, JointStyle jointStyle = JointStyle.Miter, EndCapStyle cap = EndCapStyle.Butt) { // center the shape outerRadii + 10 px away from edges float offset = outer + 10; Star star = new Star(offset, offset, points, inner, outer); var outline = Outliner.GenerateOutline(star, width, new float[] { 3, 3 }, false, jointStyle, cap); outline.SaveImage("Stars", $"StarOutlineDashed_{points}_{jointStyle}_{cap}.png"); }
private static void OutputStarOutline(int points, float inner = 10, float outer = 20, float width = 5, JointStyle jointStyle = JointStyle.Miter) { // center the shape outerRadii + 10 px away from edges float offset = outer + 10; Star star = new Star(offset, offset, points, inner, outer); var outline = Outliner.GenerateOutline(star, width, jointStyle); outline.SaveImage("Stars", $"StarOutline_{points}_{jointStyle}.png"); }
/// <summary> /// Generates a outline of the path with alternating on and off segments based on the pattern. /// </summary> /// <param name="path">the path to outline</param> /// <param name="width">The final width outline</param> /// <param name="pattern">The pattern made of multiples of the width.</param> /// <param name="startOff">Weather the first item in the pattern is on or off.</param> /// <param name="jointStyle">The style to render the joints.</param> /// <param name="patternSectionCapStyle">The style to render between sections of the specified pattern.</param> /// <returns>A new path representing the outline.</returns> public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan <float> pattern, bool startOff, JointStyle jointStyle = JointStyle.Square, EndCapStyle patternSectionCapStyle = EndCapStyle.Butt) { if (pattern.Length < 2) { return(path.GenerateOutline(width, jointStyle: jointStyle)); } JoinType style = Convert(jointStyle); EndType patternSectionCap = Convert(patternSectionCapStyle); IEnumerable <ISimplePath> paths = path.Flatten(); var offset = new ClipperOffset() { MiterLimit = MiterOffsetDelta }; var buffer = new List <IntPoint>(3); foreach (ISimplePath p in paths) { bool online = !startOff; float targetLength = pattern[0] * width; int patternPos = 0; // Create a new list of points representing the new outline int pCount = p.Points.Count; if (!p.IsClosed) { pCount--; } int i = 0; Vector2 currentPoint = p.Points[0]; while (i < pCount) { int next = (i + 1) % p.Points.Count; Vector2 targetPoint = p.Points[next]; float distToNext = Vector2.Distance(currentPoint, targetPoint); if (distToNext > targetLength) { // find a point between the 2 float t = targetLength / distToNext; Vector2 point = (currentPoint * (1 - t)) + (targetPoint * t); buffer.Add(currentPoint.ToPoint()); buffer.Add(point.ToPoint()); // we now inset a line joining if (online) { offset.AddPath(buffer, style, patternSectionCap); } online = !online; buffer.Clear(); currentPoint = point; // next length patternPos = (patternPos + 1) % pattern.Length; targetLength = pattern[patternPos] * width; } else if (distToNext <= targetLength) { buffer.Add(currentPoint.ToPoint()); currentPoint = targetPoint; i++; targetLength -= distToNext; } } if (buffer.Count > 0) { if (p.IsClosed) { buffer.Add(p.Points.First().ToPoint()); } else { buffer.Add(p.Points.Last().ToPoint()); } if (online) { offset.AddPath(buffer, style, patternSectionCap); } online = !online; buffer.Clear(); patternPos = (patternPos + 1) % pattern.Length; targetLength = pattern[patternPos] * width; } } return(ExecuteOutliner(width, offset)); }
/// <summary> /// Generates an outline of the path with alternating on and off segments based on the pattern. /// </summary> /// <param name="path">The path to outline</param> /// <param name="width">The outline width.</param> /// <param name="pattern">The pattern made of multiples of the width.</param> /// <param name="startOff">Whether the first item in the pattern is on or off.</param> /// <param name="jointStyle">The style to apply to the joints.</param> /// <param name="endCapStyle">The style to apply to the end caps.</param> /// <returns>A new <see cref="IPath"/> representing the outline.</returns> /// <exception cref="ClipperException">Thrown when an offset cannot be calculated.</exception> public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan <float> pattern, bool startOff, JointStyle jointStyle, EndCapStyle endCapStyle) { if (pattern.Length < 2) { return(path.GenerateOutline(width, jointStyle, endCapStyle)); } IEnumerable <ISimplePath> paths = path.Flatten(); ClipperOffset offset = new(MiterOffsetDelta); List <PointF> buffer = new(); foreach (ISimplePath p in paths) { bool online = !startOff; float targetLength = pattern[0] * width; int patternPos = 0; ReadOnlySpan <PointF> points = p.Points.Span; // Create a new list of points representing the new outline int pCount = points.Length; if (!p.IsClosed) { pCount--; } int i = 0; Vector2 currentPoint = points[0]; while (i < pCount) { int next = (i + 1) % points.Length; Vector2 targetPoint = points[next]; float distToNext = Vector2.Distance(currentPoint, targetPoint); if (distToNext > targetLength) { // find a point between the 2 float t = targetLength / distToNext; Vector2 point = (currentPoint * (1 - t)) + (targetPoint * t); buffer.Add(currentPoint); buffer.Add(point); // we now inset a line joining if (online) { offset.AddPath(new ReadOnlySpan <PointF>(buffer.ToArray()), jointStyle, endCapStyle); } online = !online; buffer.Clear(); currentPoint = point; // next length patternPos = (patternPos + 1) % pattern.Length; targetLength = pattern[patternPos] * width; } else if (distToNext <= targetLength) { buffer.Add(currentPoint); currentPoint = targetPoint; i++; targetLength -= distToNext; } } if (buffer.Count > 0) { if (p.IsClosed) { buffer.Add(points[0]); } else { buffer.Add(points[points.Length - 1]); } if (online) { offset.AddPath(new ReadOnlySpan <PointF>(buffer.ToArray()), jointStyle, endCapStyle); } online = !online; buffer.Clear(); patternPos = (patternPos + 1) % pattern.Length; targetLength = pattern[patternPos] * width; } } return(offset.Execute(width)); }
/// <summary> /// Generates an outline of the path with alternating on and off segments based on the pattern. /// </summary> /// <param name="path">The path to outline</param> /// <param name="width">The outline width.</param> /// <param name="pattern">The pattern made of multiples of the width.</param> /// <param name="jointStyle">The style to apply to the joints.</param> /// <param name="endCapStyle">The style to apply to the end caps.</param> /// <returns>A new <see cref="IPath"/> representing the outline.</returns> /// <exception cref="ClipperException">Thrown when an offset cannot be calculated.</exception> public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan <float> pattern, JointStyle jointStyle, EndCapStyle endCapStyle) => GenerateOutline(path, width, pattern, false, jointStyle, endCapStyle);