/
Vector2.cs
730 lines (648 loc) · 35.4 KB
/
Vector2.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
namespace System.Numerics
{
/// <summary>Represents a vector with two single-precision floating-point values.</summary>
/// <remarks><format type="text/markdown"><![CDATA[
/// The <xref:System.Numerics.Vector2> structure provides support for hardware acceleration.
/// [!INCLUDE[vectors-are-rows-paragraph](~/includes/system-numerics-vectors-are-rows.md)]
/// ]]></format></remarks>
[Intrinsic]
public partial struct Vector2 : IEquatable<Vector2>, IFormattable
{
/// <summary>The X component of the vector.</summary>
public float X;
/// <summary>The Y component of the vector.</summary>
public float Y;
internal const int Count = 2;
/// <summary>Creates a new <see cref="Vector2" /> object whose two elements have the same value.</summary>
/// <param name="value">The value to assign to both elements.</param>
[Intrinsic]
public Vector2(float value) : this(value, value)
{
}
/// <summary>Creates a vector whose elements have the specified values.</summary>
/// <param name="x">The value to assign to the <see cref="X" /> field.</param>
/// <param name="y">The value to assign to the <see cref="Y" /> field.</param>
[Intrinsic]
public Vector2(float x, float y)
{
X = x;
Y = y;
}
/// <summary>Constructs a vector from the given <see cref="ReadOnlySpan{Single}" />. The span must contain at least 2 elements.</summary>
/// <param name="values">The span of elements to assign to the vector.</param>
public Vector2(ReadOnlySpan<float> values)
{
if (values.Length < 2)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.values);
}
this = Unsafe.ReadUnaligned<Vector2>(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(values)));
}
/// <summary>Returns a vector whose 2 elements are equal to zero.</summary>
/// <value>A vector whose two elements are equal to zero (that is, it returns the vector <c>(0,0)</c>.</value>
public static Vector2 Zero
{
[Intrinsic]
get => default;
}
/// <summary>Gets a vector whose 2 elements are equal to one.</summary>
/// <value>A vector whose two elements are equal to one (that is, it returns the vector <c>(1,1)</c>.</value>
public static Vector2 One
{
[Intrinsic]
get => new Vector2(1.0f);
}
/// <summary>Gets the vector (1,0).</summary>
/// <value>The vector <c>(1,0)</c>.</value>
public static Vector2 UnitX
{
[Intrinsic]
get => new Vector2(1.0f, 0.0f);
}
/// <summary>Gets the vector (0,1).</summary>
/// <value>The vector <c>(0,1)</c>.</value>
public static Vector2 UnitY
{
[Intrinsic]
get => new Vector2(0.0f, 1.0f);
}
/// <summary>Gets or sets the element at the specified index.</summary>
/// <param name="index">The index of the element to get or set.</param>
/// <returns>The the element at <paramref name="index" />.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index" /> was less than zero or greater than the number of elements.</exception>
public float this[int index]
{
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => this.GetElement(index);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => this = this.WithElement(index, value);
}
/// <summary>Adds two vectors together.</summary>
/// <param name="left">The first vector to add.</param>
/// <param name="right">The second vector to add.</param>
/// <returns>The summed vector.</returns>
/// <remarks>The <see cref="op_Addition" /> method defines the addition operation for <see cref="Vector2" /> objects.</remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator +(Vector2 left, Vector2 right)
{
return new Vector2(
left.X + right.X,
left.Y + right.Y
);
}
/// <summary>Divides the first vector by the second.</summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The vector that results from dividing <paramref name="left" /> by <paramref name="right" />.</returns>
/// <remarks>The <see cref="Vector2.op_Division" /> method defines the division operation for <see cref="Vector2" /> objects.</remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator /(Vector2 left, Vector2 right)
{
return new Vector2(
left.X / right.X,
left.Y / right.Y
);
}
/// <summary>Divides the specified vector by a specified scalar value.</summary>
/// <param name="value1">The vector.</param>
/// <param name="value2">The scalar value.</param>
/// <returns>The result of the division.</returns>
/// <remarks>The <see cref="Vector2.op_Division" /> method defines the division operation for <see cref="Vector2" /> objects.</remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator /(Vector2 value1, float value2)
{
return value1 / new Vector2(value2);
}
/// <summary>Returns a value that indicates whether each pair of elements in two specified vectors is equal.</summary>
/// <param name="left">The first vector to compare.</param>
/// <param name="right">The second vector to compare.</param>
/// <returns><see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are equal; otherwise, <see langword="false" />.</returns>
/// <remarks>Two <see cref="Vector2" /> objects are equal if each value in <paramref name="left" /> is equal to the corresponding value in <paramref name="right" />.</remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vector2 left, Vector2 right)
{
return (left.X == right.X)
&& (left.Y == right.Y);
}
/// <summary>Returns a value that indicates whether two specified vectors are not equal.</summary>
/// <param name="left">The first vector to compare.</param>
/// <param name="right">The second vector to compare.</param>
/// <returns><see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are not equal; otherwise, <see langword="false" />.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vector2 left, Vector2 right)
{
return !(left == right);
}
/// <summary>Returns a new vector whose values are the product of each pair of elements in two specified vectors.</summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The element-wise product vector.</returns>
/// <remarks>The <see cref="Vector2.op_Multiply" /> method defines the multiplication operation for <see cref="Vector2" /> objects.</remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator *(Vector2 left, Vector2 right)
{
return new Vector2(
left.X * right.X,
left.Y * right.Y
);
}
/// <summary>Multiplies the specified vector by the specified scalar value.</summary>
/// <param name="left">The vector.</param>
/// <param name="right">The scalar value.</param>
/// <returns>The scaled vector.</returns>
/// <remarks>The <see cref="Vector2.op_Multiply" /> method defines the multiplication operation for <see cref="Vector2" /> objects.</remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator *(Vector2 left, float right)
{
return left * new Vector2(right);
}
/// <summary>Multiplies the scalar value by the specified vector.</summary>
/// <param name="left">The vector.</param>
/// <param name="right">The scalar value.</param>
/// <returns>The scaled vector.</returns>
/// <remarks>The <see cref="Vector2.op_Multiply" /> method defines the multiplication operation for <see cref="Vector2" /> objects.</remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator *(float left, Vector2 right)
{
return right * left;
}
/// <summary>Subtracts the second vector from the first.</summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The vector that results from subtracting <paramref name="right" /> from <paramref name="left" />.</returns>
/// <remarks>The <see cref="op_Subtraction" /> method defines the subtraction operation for <see cref="Vector2" /> objects.</remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator -(Vector2 left, Vector2 right)
{
return new Vector2(
left.X - right.X,
left.Y - right.Y
);
}
/// <summary>Negates the specified vector.</summary>
/// <param name="value">The vector to negate.</param>
/// <returns>The negated vector.</returns>
/// <remarks>The <see cref="op_UnaryNegation" /> method defines the unary negation operation for <see cref="Vector2" /> objects.</remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator -(Vector2 value)
{
return Zero - value;
}
/// <summary>Returns a vector whose elements are the absolute values of each of the specified vector's elements.</summary>
/// <param name="value">A vector.</param>
/// <returns>The absolute value vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Abs(Vector2 value)
{
return new Vector2(
MathF.Abs(value.X),
MathF.Abs(value.Y)
);
}
/// <summary>Adds two vectors together.</summary>
/// <param name="left">The first vector to add.</param>
/// <param name="right">The second vector to add.</param>
/// <returns>The summed vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Add(Vector2 left, Vector2 right)
{
return left + right;
}
/// <summary>Restricts a vector between a minimum and a maximum value.</summary>
/// <param name="value1">The vector to restrict.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The restricted vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max)
{
// We must follow HLSL behavior in the case user specified min value is bigger than max value.
return Min(Max(value1, min), max);
}
/// <summary>Computes the Euclidean distance between the two given points.</summary>
/// <param name="value1">The first point.</param>
/// <param name="value2">The second point.</param>
/// <returns>The distance.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Distance(Vector2 value1, Vector2 value2)
{
float distanceSquared = DistanceSquared(value1, value2);
return MathF.Sqrt(distanceSquared);
}
/// <summary>Returns the Euclidean distance squared between two specified points.</summary>
/// <param name="value1">The first point.</param>
/// <param name="value2">The second point.</param>
/// <returns>The distance squared.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DistanceSquared(Vector2 value1, Vector2 value2)
{
Vector2 difference = value1 - value2;
return Dot(difference, difference);
}
/// <summary>Divides the first vector by the second.</summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The vector resulting from the division.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Divide(Vector2 left, Vector2 right)
{
return left / right;
}
/// <summary>Divides the specified vector by a specified scalar value.</summary>
/// <param name="left">The vector.</param>
/// <param name="divisor">The scalar value.</param>
/// <returns>The vector that results from the division.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Divide(Vector2 left, float divisor)
{
return left / divisor;
}
/// <summary>Returns the dot product of two vectors.</summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The dot product.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(Vector2 value1, Vector2 value2)
{
return (value1.X * value2.X)
+ (value1.Y * value2.Y);
}
/// <summary>Performs a linear interpolation between two vectors based on the given weighting.</summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="amount">A value between 0 and 1 that indicates the weight of <paramref name="value2" />.</param>
/// <returns>The interpolated vector.</returns>
/// <remarks><format type="text/markdown"><![CDATA[
/// The behavior of this method changed in .NET 5.0. For more information, see [Behavior change for Vector2.Lerp and Vector4.Lerp](/dotnet/core/compatibility/3.1-5.0#behavior-change-for-vector2lerp-and-vector4lerp).
/// ]]></format></remarks>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Lerp(Vector2 value1, Vector2 value2, float amount)
{
return (value1 * (1.0f - amount)) + (value2 * amount);
}
/// <summary>Returns a vector whose elements are the maximum of each of the pairs of elements in two specified vectors.</summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The maximized vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Max(Vector2 value1, Vector2 value2)
{
return new Vector2(
(value1.X > value2.X) ? value1.X : value2.X,
(value1.Y > value2.Y) ? value1.Y : value2.Y
);
}
/// <summary>Returns a vector whose elements are the minimum of each of the pairs of elements in two specified vectors.</summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The minimized vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Min(Vector2 value1, Vector2 value2)
{
return new Vector2(
(value1.X < value2.X) ? value1.X : value2.X,
(value1.Y < value2.Y) ? value1.Y : value2.Y
);
}
/// <summary>Returns a new vector whose values are the product of each pair of elements in two specified vectors.</summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The element-wise product vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Multiply(Vector2 left, Vector2 right)
{
return left * right;
}
/// <summary>Multiplies a vector by a specified scalar.</summary>
/// <param name="left">The vector to multiply.</param>
/// <param name="right">The scalar value.</param>
/// <returns>The scaled vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Multiply(Vector2 left, float right)
{
return left * right;
}
/// <summary>Multiplies a scalar value by a specified vector.</summary>
/// <param name="left">The scaled value.</param>
/// <param name="right">The vector.</param>
/// <returns>The scaled vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Multiply(float left, Vector2 right)
{
return left * right;
}
/// <summary>Negates a specified vector.</summary>
/// <param name="value">The vector to negate.</param>
/// <returns>The negated vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Negate(Vector2 value)
{
return -value;
}
/// <summary>Returns a vector with the same direction as the specified vector, but with a length of one.</summary>
/// <param name="value">The vector to normalize.</param>
/// <returns>The normalized vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Normalize(Vector2 value)
{
return value / value.Length();
}
/// <summary>Returns the reflection of a vector off a surface that has the specified normal.</summary>
/// <param name="vector">The source vector.</param>
/// <param name="normal">The normal of the surface being reflected off.</param>
/// <returns>The reflected vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Reflect(Vector2 vector, Vector2 normal)
{
float dot = Dot(vector, normal);
return vector - (2.0f * (dot * normal));
}
/// <summary>Returns a vector whose elements are the square root of each of a specified vector's elements.</summary>
/// <param name="value">A vector.</param>
/// <returns>The square root vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 SquareRoot(Vector2 value)
{
return new Vector2(
MathF.Sqrt(value.X),
MathF.Sqrt(value.Y)
);
}
/// <summary>Subtracts the second vector from the first.</summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The difference vector.</returns>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Subtract(Vector2 left, Vector2 right)
{
return left - right;
}
/// <summary>Transforms a vector by a specified 3x2 matrix.</summary>
/// <param name="position">The vector to transform.</param>
/// <param name="matrix">The transformation matrix.</param>
/// <returns>The transformed vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Transform(Vector2 position, Matrix3x2 matrix)
{
return Transform(position, in matrix.AsImpl());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector2 Transform(Vector2 position, in Matrix3x2.Impl matrix)
{
Vector2 result = matrix.X * position.X;
result += matrix.Y * position.Y;
result += matrix.Z;
return result;
}
/// <summary>Transforms a vector by a specified 4x4 matrix.</summary>
/// <param name="position">The vector to transform.</param>
/// <param name="matrix">The transformation matrix.</param>
/// <returns>The transformed vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Transform(Vector2 position, Matrix4x4 matrix)
{
return Vector4.Transform(position, in matrix.AsImpl()).AsVector128().AsVector2();
}
/// <summary>Transforms a vector by the specified Quaternion rotation value.</summary>
/// <param name="value">The vector to rotate.</param>
/// <param name="rotation">The rotation to apply.</param>
/// <returns>The transformed vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Transform(Vector2 value, Quaternion rotation)
{
float x2 = rotation.X + rotation.X;
float y2 = rotation.Y + rotation.Y;
float z2 = rotation.Z + rotation.Z;
float wz2 = rotation.W * z2;
float xx2 = rotation.X * x2;
float xy2 = rotation.X * y2;
float yy2 = rotation.Y * y2;
float zz2 = rotation.Z * z2;
return new Vector2(
value.X * (1.0f - yy2 - zz2) + value.Y * (xy2 - wz2),
value.X * (xy2 + wz2) + value.Y * (1.0f - xx2 - zz2)
);
}
/// <summary>Transforms a vector normal by the given 3x2 matrix.</summary>
/// <param name="normal">The source vector.</param>
/// <param name="matrix">The matrix.</param>
/// <returns>The transformed vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 TransformNormal(Vector2 normal, Matrix3x2 matrix)
{
return TransformNormal(normal, in matrix.AsImpl());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector2 TransformNormal(Vector2 normal, in Matrix3x2.Impl matrix)
{
Vector2 result = matrix.X * normal.X;
result += matrix.Y * normal.Y;
return result;
}
/// <summary>Transforms a vector normal by the given 4x4 matrix.</summary>
/// <param name="normal">The source vector.</param>
/// <param name="matrix">The matrix.</param>
/// <returns>The transformed vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 TransformNormal(Vector2 normal, Matrix4x4 matrix)
{
return TransformNormal(normal, in matrix.AsImpl());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector2 TransformNormal(Vector2 normal, in Matrix4x4.Impl matrix)
{
Vector4 result = matrix.X * normal.X;
result += matrix.Y * normal.Y;
return result.AsVector128().AsVector2();
}
/// <summary>Copies the elements of the vector to a specified array.</summary>
/// <param name="array">The destination array.</param>
/// <remarks><paramref name="array" /> must have at least two elements. The method copies the vector's elements starting at index 0.</remarks>
/// <exception cref="NullReferenceException"><paramref name="array" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">The number of elements in the current instance is greater than in the array.</exception>
/// <exception cref="RankException"><paramref name="array" /> is multidimensional.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void CopyTo(float[] array)
{
// We explicitly don't check for `null` because historically this has thrown `NullReferenceException` for perf reasons
if (array.Length < Count)
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[0]), this);
}
/// <summary>Copies the elements of the vector to a specified array starting at a specified index position.</summary>
/// <param name="array">The destination array.</param>
/// <param name="index">The index at which to copy the first element of the vector.</param>
/// <remarks><paramref name="array" /> must have a sufficient number of elements to accommodate the two vector elements. In other words, elements <paramref name="index" /> and <paramref name="index" /> + 1 must already exist in <paramref name="array" />.</remarks>
/// <exception cref="NullReferenceException"><paramref name="array" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">The number of elements in the current instance is greater than in the array.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index" /> is less than zero.
/// -or-
/// <paramref name="index" /> is greater than or equal to the array length.</exception>
/// <exception cref="RankException"><paramref name="array" /> is multidimensional.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void CopyTo(float[] array, int index)
{
// We explicitly don't check for `null` because historically this has thrown `NullReferenceException` for perf reasons
if ((uint)index >= (uint)array.Length)
{
ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_IndexMustBeLess();
}
if ((array.Length - index) < Count)
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[index]), this);
}
/// <summary>Copies the vector to the given <see cref="Span{T}" />.The length of the destination span must be at least 2.</summary>
/// <param name="destination">The destination span which the values are copied into.</param>
/// <exception cref="ArgumentException">If number of elements in source vector is greater than those available in destination span.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void CopyTo(Span<float> destination)
{
if (destination.Length < Count)
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(destination)), this);
}
/// <summary>Attempts to copy the vector to the given <see cref="Span{Single}" />. The length of the destination span must be at least 2.</summary>
/// <param name="destination">The destination span which the values are copied into.</param>
/// <returns><see langword="true" /> if the source vector was successfully copied to <paramref name="destination" />. <see langword="false" /> if <paramref name="destination" /> is not large enough to hold the source vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryCopyTo(Span<float> destination)
{
if (destination.Length < Count)
{
return false;
}
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(destination)), this);
return true;
}
/// <summary>Returns a value that indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns><see langword="true" /> if the current instance and <paramref name="obj" /> are equal; otherwise, <see langword="false" />. If <paramref name="obj" /> is <see langword="null" />, the method returns <see langword="false" />.</returns>
/// <remarks>The current instance and <paramref name="obj" /> are equal if <paramref name="obj" /> is a <see cref="Vector2" /> object and their <see cref="X" /> and <see cref="Y" /> elements are equal.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override readonly bool Equals([NotNullWhen(true)] object? obj)
{
return (obj is Vector2 other) && Equals(other);
}
/// <summary>Returns a value that indicates whether this instance and another vector are equal.</summary>
/// <param name="other">The other vector.</param>
/// <returns><see langword="true" /> if the two vectors are equal; otherwise, <see langword="false" />.</returns>
/// <remarks>Two vectors are equal if their <see cref="X" /> and <see cref="Y" /> elements are equal.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool Equals(Vector2 other)
{
// This function needs to account for floating-point equality around NaN
// and so must behave equivalently to the underlying float/double.Equals
if (Vector64.IsHardwareAccelerated)
{
return Vector64.LoadUnsafe(ref Unsafe.AsRef(in X)).Equals(Vector64.LoadUnsafe(ref other.X));
}
else if (Vector128.IsHardwareAccelerated)
{
return this.AsVector128().Equals(other.AsVector128());
}
return SoftwareFallback(in this, other);
static bool SoftwareFallback(in Vector2 self, Vector2 other)
{
return self.X.Equals(other.X)
&& self.Y.Equals(other.Y);
}
}
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>The hash code.</returns>
public override readonly int GetHashCode()
{
return HashCode.Combine(X, Y);
}
/// <summary>Returns the length of the vector.</summary>
/// <returns>The vector's length.</returns>
/// <altmember cref="LengthSquared"/>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float Length()
{
float lengthSquared = LengthSquared();
return MathF.Sqrt(lengthSquared);
}
/// <summary>Returns the length of the vector squared.</summary>
/// <returns>The vector's length squared.</returns>
/// <remarks>This operation offers better performance than a call to the <see cref="Length" /> method.</remarks>
/// <altmember cref="Length"/>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float LengthSquared()
{
return Dot(this, this);
}
/// <summary>Returns the string representation of the current instance using default formatting.</summary>
/// <returns>The string representation of the current instance.</returns>
/// <remarks>This method returns a string in which each element of the vector is formatted using the "G" (general) format string and the formatting conventions of the current thread culture. The "<" and ">" characters are used to begin and end the string, and the current culture's <see cref="NumberFormatInfo.NumberGroupSeparator" /> property followed by a space is used to separate each element.</remarks>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
}
/// <summary>Returns the string representation of the current instance using the specified format string to format individual elements.</summary>
/// <param name="format">A standard or custom numeric format string that defines the format of individual elements.</param>
/// <returns>The string representation of the current instance.</returns>
/// <remarks>This method returns a string in which each element of the vector is formatted using <paramref name="format" /> and the current culture's formatting conventions. The "<" and ">" characters are used to begin and end the string, and the current culture's <see cref="NumberFormatInfo.NumberGroupSeparator" /> property followed by a space is used to separate each element.</remarks>
/// <related type="Article" href="/dotnet/standard/base-types/standard-numeric-format-strings">Standard Numeric Format Strings</related>
/// <related type="Article" href="/dotnet/standard/base-types/custom-numeric-format-strings">Custom Numeric Format Strings</related>
public readonly string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string? format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
/// <summary>Returns the string representation of the current instance using the specified format string to format individual elements and the specified format provider to define culture-specific formatting.</summary>
/// <param name="format">A standard or custom numeric format string that defines the format of individual elements.</param>
/// <param name="formatProvider">A format provider that supplies culture-specific formatting information.</param>
/// <returns>The string representation of the current instance.</returns>
/// <remarks>This method returns a string in which each element of the vector is formatted using <paramref name="format" /> and <paramref name="formatProvider" />. The "<" and ">" characters are used to begin and end the string, and the format provider's <see cref="NumberFormatInfo.NumberGroupSeparator" /> property followed by a space is used to separate each element.</remarks>
/// <related type="Article" href="/dotnet/standard/base-types/custom-numeric-format-strings">Custom Numeric Format Strings</related>
/// <related type="Article" href="/dotnet/standard/base-types/standard-numeric-format-strings">Standard Numeric Format Strings</related>
public readonly string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string? format, IFormatProvider? formatProvider)
{
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
return $"<{X.ToString(format, formatProvider)}{separator} {Y.ToString(format, formatProvider)}>";
}
}
}