Skip to content

Commit 7b2a1ce

Browse files
Merge pull request #284 from SixLabors/js/shape-constructors
Add Convenience Constructors to shapes
2 parents f7f0f4d + 67e546e commit 7b2a1ce

File tree

9 files changed

+35
-17
lines changed

9 files changed

+35
-17
lines changed

samples/DrawShapesWithImageSharp/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static void DrawSerializedOPenSansLetterShape_a()
111111
PointF[] points = pl.Select(p => p.Split('x'))
112112
.Select(p => new PointF(float.Parse(p[0]), float.Parse(p[1])))
113113
.ToArray();
114-
return new Polygon(new LinearLineSegment(points));
114+
return new Polygon(points);
115115
}).ToArray();
116116

117117
var complex = new ComplexPolygon(polys);
@@ -131,7 +131,7 @@ private static void DrawSerializedOPenSansLetterShape_o()
131131
.Select(p => new PointF(float.Parse(p[0]), float.Parse(p[1])))
132132
.ToArray();
133133

134-
return new Polygon(new LinearLineSegment(points));
134+
return new Polygon(points);
135135
}).ToArray();
136136

137137
var complex = new ComplexPolygon(polys);

src/ImageSharp.Drawing/Processing/Extensions/DrawLineExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static IImageProcessingContext DrawLine(
2525
Brush brush,
2626
float thickness,
2727
params PointF[] points) =>
28-
source.Draw(options, new SolidPen(brush, thickness), new Path(new LinearLineSegment(points)));
28+
source.Draw(options, new SolidPen(brush, thickness), new Path(points));
2929

3030
/// <summary>
3131
/// Draws the provided points as an open linear path at the provided thickness with the supplied brush.
@@ -40,7 +40,7 @@ public static IImageProcessingContext DrawLine(
4040
Brush brush,
4141
float thickness,
4242
params PointF[] points) =>
43-
source.Draw(new SolidPen(brush, thickness), new Path(new LinearLineSegment(points)));
43+
source.Draw(new SolidPen(brush, thickness), new Path(points));
4444

4545
/// <summary>
4646
/// Draws the provided points as an open linear path at the provided thickness with the supplied brush.
@@ -87,7 +87,7 @@ public static IImageProcessingContext DrawLine(
8787
DrawingOptions options,
8888
Pen pen,
8989
params PointF[] points) =>
90-
source.Draw(options, pen, new Path(new LinearLineSegment(points)));
90+
source.Draw(options, pen, new Path(points));
9191

9292
/// <summary>
9393
/// Draws the provided points as an open linear path with the supplied pen.
@@ -100,6 +100,6 @@ public static IImageProcessingContext DrawLine(
100100
this IImageProcessingContext source,
101101
Pen pen,
102102
params PointF[] points) =>
103-
source.Draw(pen, new Path(new LinearLineSegment(points)));
103+
source.Draw(pen, new Path(points));
104104
}
105105
}

src/ImageSharp.Drawing/Processing/Extensions/DrawPolygonExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static IImageProcessingContext DrawPolygon(
2121
this IImageProcessingContext source,
2222
Pen pen,
2323
params PointF[] points) =>
24-
source.Draw(source.GetDrawingOptions(), pen, new Polygon(new LinearLineSegment(points)));
24+
source.Draw(source.GetDrawingOptions(), pen, new Polygon(points));
2525

2626
/// <summary>
2727
/// Draws the provided points as a closed linear polygon with the provided pen.
@@ -36,7 +36,7 @@ public static IImageProcessingContext DrawPolygon(
3636
DrawingOptions options,
3737
Pen pen,
3838
params PointF[] points) =>
39-
source.Draw(options, pen, new Polygon(new LinearLineSegment(points)));
39+
source.Draw(options, pen, new Polygon(points));
4040

4141
/// <summary>
4242
/// Draws the provided points as a closed linear polygon with the provided brush at the provided thickness.

src/ImageSharp.Drawing/Processing/Extensions/FillPolygonExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static IImageProcessingContext FillPolygon(
2323
DrawingOptions options,
2424
Brush brush,
2525
params PointF[] points) =>
26-
source.Fill(options, brush, new Polygon(new LinearLineSegment(points)));
26+
source.Fill(options, brush, new Polygon(points));
2727

2828
/// <summary>
2929
/// Flood fills the image in the shape of a linear polygon described by the points
@@ -36,7 +36,7 @@ public static IImageProcessingContext FillPolygon(
3636
this IImageProcessingContext source,
3737
Brush brush,
3838
params PointF[] points) =>
39-
source.Fill(brush, new Polygon(new LinearLineSegment(points)));
39+
source.Fill(brush, new Polygon(points));
4040

4141
/// <summary>
4242
/// Flood fills the image in the shape of a linear polygon described by the points
@@ -51,7 +51,7 @@ public static IImageProcessingContext FillPolygon(
5151
DrawingOptions options,
5252
Color color,
5353
params PointF[] points) =>
54-
source.Fill(options, new SolidBrush(color), new Polygon(new LinearLineSegment(points)));
54+
source.Fill(options, new SolidBrush(color), new Polygon(points));
5555

5656
/// <summary>
5757
/// Flood fills the image in the shape of a linear polygon described by the points
@@ -64,6 +64,6 @@ public static IImageProcessingContext FillPolygon(
6464
this IImageProcessingContext source,
6565
Color color,
6666
params PointF[] points) =>
67-
source.Fill(new SolidBrush(color), new Polygon(new LinearLineSegment(points)));
67+
source.Fill(new SolidBrush(color), new Polygon(points));
6868
}
6969
}

src/ImageSharp.Drawing/Shapes/Path.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public class Path : IPath, ISimplePath, IPathInternals, IInternalPathOwner
1717
private readonly ILineSegment[] lineSegments;
1818
private InternalPath innerPath;
1919

20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="Path"/> class.
22+
/// </summary>
23+
/// <param name="points">The collection of points; processed as a series of linear line segments.</param>
24+
public Path(PointF[] points)
25+
: this(new LinearLineSegment(points))
26+
{
27+
}
28+
2029
/// <summary>
2130
/// Initializes a new instance of the <see cref="Path"/> class.
2231
/// </summary>

src/ImageSharp.Drawing/Shapes/Polygon.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ namespace SixLabors.ImageSharp.Drawing
1111
/// </summary>
1212
public class Polygon : Path
1313
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="Polygon"/> class.
16+
/// </summary>
17+
/// <param name="points">The collection of points; processed as a series of linear line segments.</param>
18+
public Polygon(PointF[] points)
19+
: this(new LinearLineSegment(points))
20+
{
21+
}
22+
1423
/// <summary>
1524
/// Initializes a new instance of the <see cref="Polygon"/> class.
1625
/// </summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public IPath[] GenerateClippedShapes(ClippingOperation operation, IntersectionRu
5454
#endif
5555
}
5656

57-
shapes[index++] = new Polygon(new LinearLineSegment(points));
57+
shapes[index++] = new Polygon(points);
5858
}
5959

6060
for (int i = 0; i < openPaths.Count; i++)
@@ -72,7 +72,7 @@ public IPath[] GenerateClippedShapes(ClippingOperation operation, IntersectionRu
7272
#endif
7373
}
7474

75-
shapes[index++] = new Polygon(new LinearLineSegment(points));
75+
shapes[index++] = new Polygon(points);
7676
}
7777

7878
return shapes;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ComplexPolygon Execute(float width)
4848
#endif
4949
}
5050

51-
polygons[i] = new Polygon(new LinearLineSegment(points));
51+
polygons[i] = new Polygon(points);
5252
}
5353

5454
return new ComplexPolygon(polygons);

tests/ImageSharp.Drawing.Tests/Shapes/Scan/TessellatedMultipolygonTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void Create_FromPolygon_Case1(bool reverseOriginal)
4747
points.AsSpan().Reverse();
4848
}
4949

50-
var polygon = new Polygon(new LinearLineSegment(points));
50+
var polygon = new Polygon(points);
5151

5252
using var multipolygon = TessellatedMultipolygon.Create(polygon, MemoryAllocator);
5353
VerifyRing(multipolygon[0], points, reverseOriginal, false);
@@ -65,7 +65,7 @@ public void Create_FromPolygon_Case2(bool reverseOriginal)
6565
points.AsSpan().Reverse();
6666
}
6767

68-
var polygon = new Polygon(new LinearLineSegment(points));
68+
var polygon = new Polygon(points);
6969

7070
using var multipolygon = TessellatedMultipolygon.Create(polygon, MemoryAllocator);
7171

0 commit comments

Comments
 (0)