-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PolygonF.cs
300 lines (266 loc) · 9.09 KB
/
PolygonF.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Numerics;
using X10D.Numerics;
namespace X10D.Drawing;
/// <summary>
/// Represents a 2D polygon composed of single-precision floating-vertex vertices.
/// </summary>
public class PolygonF
{
private readonly List<PointF> _vertices = new();
/// <summary>
/// Initializes a new instance of the <see cref="PolygonF" /> class.
/// </summary>
public PolygonF()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PolygonF" /> class by copying the specified polygon.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="polygon" /> is <see langword="null" />.</exception>
public PolygonF(PolygonF polygon)
: this(polygon?._vertices ?? throw new ArgumentNullException(nameof(polygon)))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PolygonF" /> class by constructing it from the specified vertices.
/// </summary>
/// <param name="vertices">An enumerable collection of vertices from which the polygon should be constructed.</param>
/// <exception cref="ArgumentNullException"><paramref name="vertices" /> is <see langword="null" />.</exception>
public PolygonF(IEnumerable<Vector2> vertices)
: this(vertices.Select(p => p.ToPointF()))
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(vertices);
#else
if (vertices is null)
{
throw new ArgumentNullException(nameof(vertices));
}
#endif
}
/// <summary>
/// Initializes a new instance of the <see cref="PolygonF" /> class by constructing it from the specified vertices.
/// </summary>
/// <param name="vertices">An enumerable collection of vertices from which the polygon should be constructed.</param>
/// <exception cref="ArgumentNullException"><paramref name="vertices" /> is <see langword="null" />.</exception>
public PolygonF(IEnumerable<PointF> vertices)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(vertices);
#else
if (vertices is null)
{
throw new ArgumentNullException(nameof(vertices));
}
#endif
_vertices = new List<PointF>(vertices);
}
/// <summary>
/// Gets an empty polygon. That is, a polygon with no vertices.
/// </summary>
/// <value>An empty polygon.</value>
public static PolygonF Empty
{
get => new();
}
/// <summary>
/// Returns a value indicating whether this polygon is convex.
/// </summary>
/// <value><see langword="true" /> if this polygon is convex; otherwise, <see langword="false" />.</value>
public bool IsConvex
{
get
{
if (_vertices.Count < 3)
{
return false;
}
var positive = false;
var negative = false;
PointF p0 = _vertices[0];
for (var index = 1; index < _vertices.Count; index++)
{
PointF p1 = _vertices[index];
float d = (p1.X - p0.X) * (p1.Y + p0.Y);
if (d > 0)
{
positive = true;
}
else if (d < 0)
{
negative = true;
}
if (positive && negative)
{
return false;
}
p0 = p1;
}
return true;
}
}
/// <summary>
/// Gets the number of vertices in this polygon.
/// </summary>
/// <value>An <see cref="int" /> value, representing the number of vertices in this polygon.</value>
public int VertexCount
{
get => _vertices.Count;
}
/// <summary>
/// Gets a read-only view of the vertices in this polygon.
/// </summary>
/// <value>
/// A <see cref="IReadOnlyList{T}" /> of <see cref="PointF" /> values, representing the vertices of this polygon.
/// </value>
public IReadOnlyList<PointF> Vertices
{
get => _vertices.AsReadOnly();
}
/// <summary>
/// Returns a value indicating whether two instances of <see cref="PolygonF" /> are equal.
/// </summary>
/// <param name="left">The first instance.</param>
/// <param name="right">The second instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are considered equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator ==(PolygonF? left, PolygonF? right)
{
return Equals(left, right);
}
/// <summary>
/// Returns a value indicating whether two instances of <see cref="PolygonF" /> are not equal.
/// </summary>
/// <param name="left">The first instance.</param>
/// <param name="right">The second instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are considered not equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator !=(PolygonF? left, PolygonF? right)
{
return !(left == right);
}
/// <summary>
/// Implicitly converts a <see cref="Polygon" /> to a <see cref="PolygonF" />.
/// </summary>
/// <param name="polygon">The polygon to convert.</param>
/// <returns>The converted polygon.</returns>
[return: NotNullIfNotNull("polygon")]
public static implicit operator PolygonF?(Polygon? polygon)
{
return polygon is null ? null : FromPolygon(polygon);
}
/// <summary>
/// Implicitly converts a <see cref="Polygon" /> to a <see cref="PolygonF" />.
/// </summary>
/// <param name="polygon">The polygon to convert.</param>
/// <returns>The converted polygon.</returns>
/// <exception cref="ArgumentNullException"><paramref name="polygon" /> is <see langword="null" />.</exception>
public static PolygonF FromPolygon(Polygon polygon)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(polygon);
#else
if (polygon is null)
{
throw new ArgumentNullException(nameof(polygon));
}
#endif
var vertices = new List<PointF>();
foreach (Point vertex in polygon.Vertices)
{
vertices.Add(vertex);
}
return new PolygonF(vertices);
}
/// <summary>
/// Adds a vertex to this polygon.
/// </summary>
/// <param name="vertex">The vertex to add.</param>
public void AddVertex(PointF vertex)
{
_vertices.Add(vertex);
}
/// <summary>
/// Adds a vertex to this polygon.
/// </summary>
/// <param name="vertex">The vertex to add.</param>
public void AddVertex(Vector2 vertex)
{
AddVertex(vertex.ToPointF());
}
/// <summary>
/// Adds a collection of vertices to this polygon.
/// </summary>
/// <param name="vertices">An enumerable collection of vertices to add.</param>
/// <exception cref="ArgumentNullException"><paramref name="vertices" /> is <see langword="null" />.</exception>
public void AddVertices(IEnumerable<PointF> vertices)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(vertices);
#else
if (vertices is null)
{
throw new ArgumentNullException(nameof(vertices));
}
#endif
foreach (PointF vertex in vertices)
{
AddVertex(vertex);
}
}
/// <summary>
/// Adds a collection of vertices to this polygon.
/// </summary>
/// <param name="vertices">An enumerable collection of vertices to add.</param>
/// <exception cref="ArgumentNullException"><paramref name="vertices" /> is <see langword="null" />.</exception>
public void AddVertices(IEnumerable<Vector2> vertices)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(vertices);
#else
if (vertices is null)
{
throw new ArgumentNullException(nameof(vertices));
}
#endif
foreach (Vector2 vertex in vertices)
{
AddVertex(vertex);
}
}
/// <summary>
/// Clears all vertices from this polygon.
/// </summary>
public void ClearVertices()
{
_vertices.Clear();
}
/// <inheritdoc />
public override bool Equals(object? obj)
{
return obj is PolygonF polygon && Equals(polygon);
}
/// <summary>
/// Returns a value indicating whether this instance and another instance are equal.
/// </summary>
/// <param name="other">The instance with which to compare.</param>
/// <returns>
/// <see langword="true" /> if this instance and <paramref name="other" /> are considered equal; otherwise,
/// <see langword="false" />.
/// </returns>
public bool Equals(PolygonF? other)
{
return other is not null && _vertices.SequenceEqual(other._vertices);
}
/// <inheritdoc />
public override int GetHashCode()
{
return _vertices.Aggregate(0, HashCode.Combine);
}
}