Skip to content

Commit 7527241

Browse files
Close the polygons on stroking.
1 parent 558de97 commit 7527241

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

src/ImageSharp.Drawing/Shapes/OutlinePathExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static IPath GenerateOutline(this IPath path, float width, JointStyle joi
4949
foreach (ISimplePath simplePath in path.Flatten())
5050
{
5151
PolygonStroker stroker = new() { Width = width };
52-
Polygon polygon = stroker.ProcessPath(simplePath.Points.Span);
52+
Polygon polygon = stroker.ProcessPath(simplePath.Points.Span, simplePath.IsClosed);
5353
polygons.Add(polygon);
5454
}
5555

src/ImageSharp.Drawing/Shapes/PolygonClipper/PolygonStroker.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,45 @@ public double MiterLimitTheta
6060

6161
public double MiterLimit { get; set; } = 4;
6262

63-
public Polygon ProcessPath(ReadOnlySpan<PointF> pathPoints)
63+
public Polygon ProcessPath(ReadOnlySpan<PointF> pathPoints, bool isClosed)
6464
{
6565
for (int i = 0; i < pathPoints.Length; i++)
6666
{
6767
PointF point = pathPoints[i];
6868
this.AddVertex(point.X, point.Y, PathCommand.LineTo);
6969
}
7070

71-
this.AddVertex(pathPoints[^1].X, pathPoints[^1].Y, PathCommand.LineTo);
72-
this.closed = 1;
73-
this.Rewind();
71+
if (isClosed)
72+
{
73+
this.AddVertex(0, 0, PathCommand.EndPoly | (PathCommand)PathFlags.Close);
74+
}
7475

7576
double x = 0;
7677
double y = 0;
77-
List<PointF> results = new(pathPoints.Length);
78-
while (!(_ = this.Vertex(ref x, ref y)).Stop())
78+
List<PointF> results = new(pathPoints.Length * 3);
79+
80+
int startIndex = 0;
81+
PointF? lastPoint = null;
82+
PathCommand command;
83+
while (!(command = this.Vertex(ref x, ref y)).Stop())
7984
{
80-
results.Add(new PointF((float)x, (float)y));
85+
PointF currentPoint;
86+
if (command.EndPoly() && results.Count > 0)
87+
{
88+
PointF initial = results[startIndex];
89+
currentPoint = new(initial.X, initial.Y);
90+
results.Add(currentPoint);
91+
startIndex = results.Count;
92+
}
93+
else
94+
{
95+
currentPoint = new((float)x, (float)y);
96+
if (currentPoint != lastPoint)
97+
{
98+
results.Add(currentPoint);
99+
lastPoint = currentPoint;
100+
}
101+
}
81102
}
82103

83104
return new Polygon(results.ToArray());

0 commit comments

Comments
 (0)