-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
ClipperOffset.cs
91 lines (79 loc) · 2.89 KB
/
ClipperOffset.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper;
/// <summary>
/// Wrapper for clipper offset
/// </summary>
internal class ClipperOffset
{
private readonly PolygonOffsetter polygonClipperOffset;
/// <summary>
/// Initializes a new instance of the <see cref="ClipperOffset"/> class.
/// </summary>
/// <param name="meterLimit">meter limit</param>
/// <param name="arcTolerance">arc tolerance</param>
public ClipperOffset(float meterLimit = 2F, float arcTolerance = .25F)
=> this.polygonClipperOffset = new(meterLimit, arcTolerance);
/// <summary>
/// Calculates an offset polygon based on the given path and width.
/// </summary>
/// <param name="width">Width</param>
/// <returns>path offset</returns>
public ComplexPolygon Execute(float width)
{
PathsF solution = new();
this.polygonClipperOffset.Execute(width, solution);
Polygon[] polygons = new Polygon[solution.Count];
for (int i = 0; i < solution.Count; i++)
{
PathF pt = solution[i];
PointF[] points = new PointF[pt.Count];
for (int j = 0; j < pt.Count; j++)
{
points[j] = pt[j];
}
polygons[i] = new Polygon(points);
}
return new ComplexPolygon(polygons);
}
/// <summary>
/// Adds the path points
/// </summary>
/// <param name="pathPoints">The path points</param>
/// <param name="jointStyle">Joint Style</param>
/// <param name="endCapStyle">Endcap Style</param>
public void AddPath(ReadOnlySpan<PointF> pathPoints, JointStyle jointStyle, EndCapStyle endCapStyle)
{
PathF points = new(pathPoints.Length);
for (int i = 0; i < pathPoints.Length; i++)
{
points.Add(pathPoints[i]);
}
this.polygonClipperOffset.AddPath(points, jointStyle, endCapStyle);
}
/// <summary>
/// Adds the path.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="jointStyle">Joint Style</param>
/// <param name="endCapStyle">Endcap Style</param>
public void AddPath(IPath path, JointStyle jointStyle, EndCapStyle endCapStyle)
{
Guard.NotNull(path, nameof(path));
foreach (ISimplePath p in path.Flatten())
{
this.AddPath(p, jointStyle, endCapStyle);
}
}
/// <summary>
/// Adds the path.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="jointStyle">Joint Style</param>
/// <param name="endCapStyle">Endcap Style</param>
private void AddPath(ISimplePath path, JointStyle jointStyle, EndCapStyle endCapStyle)
{
ReadOnlySpan<PointF> vectors = path.Points.Span;
this.AddPath(vectors, jointStyle, path.IsClosed ? EndCapStyle.Joined : endCapStyle);
}
}