/
LinearLineSegment.cs
102 lines (88 loc) · 3.4 KB
/
LinearLineSegment.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
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
namespace SixLabors.ImageSharp.Drawing
{
/// <summary>
/// Represents a series of control points that will be joined by straight lines
/// </summary>
/// <seealso cref="ILineSegment" />
public sealed class LinearLineSegment : ILineSegment
{
/// <summary>
/// The collection of points.
/// </summary>
private readonly PointF[] points;
/// <summary>
/// Initializes a new instance of the <see cref="LinearLineSegment"/> class.
/// </summary>
/// <param name="start">The start.</param>
/// <param name="end">The end.</param>
public LinearLineSegment(PointF start, PointF end)
: this(new[] { start, end })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinearLineSegment" /> class.
/// </summary>
/// <param name="point1">The point1.</param>
/// <param name="point2">The point2.</param>
/// <param name="additionalPoints">Additional points</param>
public LinearLineSegment(PointF point1, PointF point2, params PointF[] additionalPoints)
: this(new[] { point1, point2 }.Merge(additionalPoints))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinearLineSegment"/> class.
/// </summary>
/// <param name="points">The points.</param>
public LinearLineSegment(PointF[] points)
{
this.points = points ?? throw new ArgumentNullException(nameof(points));
Guard.MustBeGreaterThanOrEqualTo(this.points.Length, 2, nameof(points));
this.EndPoint = this.points[this.points.Length - 1];
}
/// <summary>
/// Gets the end point.
/// </summary>
/// <value>
/// The end point.
/// </value>
public PointF EndPoint { get; }
/// <summary>
/// Converts the <see cref="ILineSegment" /> into a simple linear path..
/// </summary>
/// <returns>
/// Returns the current <see cref="ILineSegment" /> as simple linear path.
/// </returns>
public ReadOnlyMemory<PointF> Flatten() => this.points;
/// <summary>
/// Transforms the current LineSegment using specified matrix.
/// </summary>
/// <param name="matrix">The matrix.</param>
/// <returns>
/// A line segment with the matrix applied to it.
/// </returns>
public LinearLineSegment Transform(Matrix3x2 matrix)
{
if (matrix.IsIdentity)
{
// no transform to apply skip it
return this;
}
var transformedPoints = new PointF[this.points.Length];
for (int i = 0; i < this.points.Length; i++)
{
transformedPoints[i] = PointF.Transform(this.points[i], matrix);
}
return new LinearLineSegment(transformedPoints);
}
/// <summary>
/// Transforms the current LineSegment using specified matrix.
/// </summary>
/// <param name="matrix">The matrix.</param>
/// <returns>A line segment with the matrix applied to it.</returns>
ILineSegment ILineSegment.Transform(Matrix3x2 matrix) => this.Transform(matrix);
}
}