Skip to content

Commit 0dbfc38

Browse files
committed
Lazy-init bezier drawing points
1 parent 4bfa88b commit 0dbfc38

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

src/ImageSharp.Drawing/Shapes/ArcLineSegment.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public ArcLineSegment(PointF from, PointF to, SizeF radius, float rotation, bool
4444
{
4545
this.linePoints = EllipticArcFromEndParams(from, to, radius, rotation, largeArc, sweep);
4646
}
47-
48-
this.EndPoint = this.linePoints[^1];
4947
}
5048

5149
/// <summary>
@@ -80,18 +78,15 @@ public ArcLineSegment(PointF center, SizeF radius, float rotation, float startAn
8078
{
8179
this.linePoints = EllipticArcFromEndParams(from, to, radius, rotation, largeArc, sweep);
8280
}
83-
84-
this.EndPoint = this.linePoints[^1];
8581
}
8682

8783
private ArcLineSegment(PointF[] linePoints)
8884
{
8985
this.linePoints = linePoints;
90-
this.EndPoint = this.linePoints[^1];
9186
}
9287

9388
/// <inheritdoc/>
94-
public PointF EndPoint { get; }
89+
public PointF EndPoint => this.linePoints[^1];
9590

9691
/// <inheritdoc/>
9792
public ReadOnlyMemory<PointF> Flatten() => this.linePoints;

src/ImageSharp.Drawing/Shapes/CubicBezierLineSegment.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public sealed class CubicBezierLineSegment : ILineSegment
1818
/// <summary>
1919
/// The line points.
2020
/// </summary>
21-
private readonly PointF[] linePoints;
21+
private PointF[]? linePoints;
22+
2223
private readonly PointF[] controlPoints;
2324

2425
/// <summary>
@@ -36,10 +37,6 @@ public CubicBezierLineSegment(PointF[] points)
3637
{
3738
throw new ArgumentOutOfRangeException(nameof(points), "points must be a multiple of 3 plus 1 long.");
3839
}
39-
40-
this.linePoints = GetDrawingPoints(this.controlPoints);
41-
42-
this.EndPoint = this.controlPoints[this.controlPoints.Length - 1];
4340
}
4441

4542
/// <summary>
@@ -55,19 +52,31 @@ public CubicBezierLineSegment(PointF start, PointF controlPoint1, PointF control
5552
{
5653
}
5754

55+
/// <inheritdoc cref="CubicBezierLineSegment(PointF, PointF, PointF, PointF, PointF[])" />
56+
public CubicBezierLineSegment(PointF start, PointF controlPoint1, PointF controlPoint2, PointF end)
57+
: this(new[] { start, controlPoint1, controlPoint2, end })
58+
{
59+
}
60+
5861
/// <summary>
5962
/// Gets the control points.
6063
/// </summary>
6164
public IReadOnlyList<PointF> ControlPoints => this.controlPoints;
6265

6366
/// <inheritdoc/>
64-
public PointF EndPoint { get; }
67+
public PointF EndPoint => this.controlPoints[^1];
6568

6669
/// <inheritdoc/>
67-
public ReadOnlyMemory<PointF> Flatten() => this.linePoints;
70+
public ReadOnlyMemory<PointF> Flatten() => this.linePoints ??= GetDrawingPoints(this.controlPoints);
71+
72+
/// <summary>
73+
/// Gets the control points of this curve.
74+
/// </summary>
75+
/// <returns>The control points of this curve.</returns>
76+
public ReadOnlyMemory<PointF> GetControlPoints() => this.controlPoints;
6877

6978
/// <summary>
70-
/// Transforms the current LineSegment using specified matrix.
79+
/// Transforms this line segment using the specified matrix.
7180
/// </summary>
7281
/// <param name="matrix">The matrix.</param>
7382
/// <returns>A line segment with the matrix applied to it.</returns>

src/ImageSharp.Drawing/Shapes/Helpers/ArrayExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal static class ArrayExtensions
1717
/// <returns>the Merged arrays</returns>
1818
public static T[] Merge<T>(this T[] source1, T[] source2)
1919
{
20-
if (source2 is null)
20+
if (source2 is null || source2.Length == 0)
2121
{
2222
return source1;
2323
}

src/ImageSharp.Drawing/Shapes/LinearLineSegment.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ public LinearLineSegment(PointF[] points)
4646
this.points = points ?? throw new ArgumentNullException(nameof(points));
4747

4848
Guard.MustBeGreaterThanOrEqualTo(this.points.Length, 2, nameof(points));
49-
50-
this.EndPoint = this.points[this.points.Length - 1];
5149
}
5250

5351
/// <summary>
@@ -56,7 +54,7 @@ public LinearLineSegment(PointF[] points)
5654
/// <value>
5755
/// The end point.
5856
/// </value>
59-
public PointF EndPoint { get; }
57+
public PointF EndPoint => this.points[^1];
6058

6159
/// <summary>
6260
/// Converts the <see cref="ILineSegment" /> into a simple linear path..

0 commit comments

Comments
 (0)