forked from nkast/MonoGame
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Vector2.cs
1282 lines (1165 loc) · 58.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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.Serialization;
namespace Microsoft.Xna.Framework
{
/// <summary>
/// Describes a 2D-vector.
/// </summary>
[DebuggerDisplay("{DebugDisplayString,nq}")]
[DataContract]
[TypeConverter("Microsoft.Xna.Framework.Design.Vector2Converter, Xna.Framework.Design")]
public struct Vector2 : IEquatable<Vector2>
{
#region Private Fields
private static readonly Vector2 zeroVector = new Vector2(0f, 0f);
private static readonly Vector2 unitVector = new Vector2(1f, 1f);
private static readonly Vector2 unitXVector = new Vector2(1f, 0f);
private static readonly Vector2 unitYVector = new Vector2(0f, 1f);
#endregion
#region Public Fields
/// <summary>
/// The x coordinate of this <see cref="Vector2"/>.
/// </summary>
[DataMember]
public float X;
/// <summary>
/// The y coordinate of this <see cref="Vector2"/>.
/// </summary>
[DataMember]
public float Y;
#endregion
#region Properties
/// <summary>
/// Returns a <see cref="Vector2"/> with components 0, 0.
/// </summary>
public static Vector2 Zero
{
get { return zeroVector; }
}
/// <summary>
/// Returns a <see cref="Vector2"/> with components 1, 1.
/// </summary>
public static Vector2 One
{
get { return unitVector; }
}
/// <summary>
/// Returns a <see cref="Vector2"/> with components 1, 0.
/// </summary>
public static Vector2 UnitX
{
get { return unitXVector; }
}
/// <summary>
/// Returns a <see cref="Vector2"/> with components 0, 1.
/// </summary>
public static Vector2 UnitY
{
get { return unitYVector; }
}
#endregion
#region Internal Properties
internal string DebugDisplayString
{
get
{
return string.Concat(
this.X.ToString(), " ",
this.Y.ToString()
);
}
}
#endregion
#region Constructors
/// <summary>
/// Constructs a 2d vector with X and Y from two values.
/// </summary>
/// <param name="x">The x coordinate in 2d-space.</param>
/// <param name="y">The y coordinate in 2d-space.</param>
public Vector2(float x, float y)
{
this.X = x;
this.Y = y;
}
/// <summary>
/// Constructs a 2d vector with X and Y set to the same value.
/// </summary>
/// <param name="value">The x and y coordinates in 2d-space.</param>
public Vector2(float value)
{
this.X = value;
this.Y = value;
}
#endregion
#region Operators
/// <summary>
/// Inverts values in the specified <see cref="Vector2"/>.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/> on the right of the sub sign.</param>
/// <returns>Result of the inversion.</returns>
public static Vector2 operator -(Vector2 value)
{
value.X = -value.X;
value.Y = -value.Y;
return value;
}
/// <summary>
/// Adds two vectors.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/> on the left of the add sign.</param>
/// <param name="right">Source <see cref="Vector2"/> on the right of the add sign.</param>
/// <returns>Sum of the vectors.</returns>
public static Vector2 operator +(Vector2 left, Vector2 right)
{
left.X += right.X;
left.Y += right.Y;
return left;
}
/// <summary>
/// Subtracts a <see cref="Vector2"/> from a <see cref="Vector2"/>.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/> on the left of the sub sign.</param>
/// <param name="right">Source <see cref="Vector2"/> on the right of the sub sign.</param>
/// <returns>Result of the vector subtraction.</returns>
public static Vector2 operator -(Vector2 left, Vector2 right)
{
left.X -= right.X;
left.Y -= right.Y;
return left;
}
/// <summary>
/// Multiplies the components of two vectors by each other.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/> on the left of the mul sign.</param>
/// <param name="right">Source <see cref="Vector2"/> on the right of the mul sign.</param>
/// <returns>Result of the vector multiplication.</returns>
public static Vector2 operator *(Vector2 left, Vector2 right)
{
left.X *= right.X;
left.Y *= right.Y;
return left;
}
/// <summary>
/// Multiplies the components of vector by a scalar.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/> on the left of the mul sign.</param>
/// <param name="right">Scalar value on the right of the mul sign.</param>
/// <returns>Result of the vector multiplication with a scalar.</returns>
public static Vector2 operator *(Vector2 left, float right)
{
left.X *= right;
left.Y *= right;
return left;
}
/// <summary>
/// Multiplies the components of vector by a scalar.
/// </summary>
/// <param name="left">Scalar value on the left of the mul sign.</param>
/// <param name="right">Source <see cref="Vector2"/> on the right of the mul sign.</param>
/// <returns>Result of the vector multiplication with a scalar.</returns>
public static Vector2 operator *(float left, Vector2 right)
{
right.X *= left;
right.Y *= left;
return right;
}
/// <summary>
/// Divides the components of a <see cref="Vector2"/> by the components of another <see cref="Vector2"/>.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/> on the left of the div sign.</param>
/// <param name="right">Divisor <see cref="Vector2"/> on the right of the div sign.</param>
/// <returns>The result of dividing the vectors.</returns>
public static Vector2 operator /(Vector2 left, Vector2 right)
{
left.X /= right.X;
left.Y /= right.Y;
return left;
}
/// <summary>
/// Divides the components of a <see cref="Vector2"/> by a scalar.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/> on the left of the div sign.</param>
/// <param name="right">Divisor scalar on the right of the div sign.</param>
/// <returns>The result of dividing a vector by a scalar.</returns>
public static Vector2 operator /(Vector2 left, float right)
{
float factor = 1 / right;
left.X *= factor;
left.Y *= factor;
return left;
}
/// <summary>
/// Compares whether two <see cref="Vector2"/> instances are equal.
/// </summary>
/// <param name="left"><see cref="Vector2"/> instance on the left of the equal sign.</param>
/// <param name="right"><see cref="Vector2"/> instance on the right of the equal sign.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public static bool operator ==(Vector2 left, Vector2 right)
{
return left.X == right.X && left.Y == right.Y;
}
/// <summary>
/// Compares whether two <see cref="Vector2"/> instances are not equal.
/// </summary>
/// <param name="left"><see cref="Vector2"/> instance on the left of the not equal sign.</param>
/// <param name="right"><see cref="Vector2"/> instance on the right of the not equal sign.</param>
/// <returns><c>true</c> if the instances are not equal; <c>false</c> otherwise.</returns>
public static bool operator !=(Vector2 left, Vector2 right)
{
return left.X != right.X || left.Y != right.Y;
}
#endregion
#region Public Methods
/// <summary>
/// Performs vector addition on <paramref name="left"/> and <paramref name="right"/>.
/// </summary>
/// <param name="left">The first vector to add.</param>
/// <param name="right">The second vector to add.</param>
/// <returns>The result of the vector addition.</returns>
public static Vector2 Add(Vector2 left, Vector2 right)
{
left.X += right.X;
left.Y += right.Y;
return left;
}
/// <summary>
/// Performs vector addition on <paramref name="left"/> and
/// <paramref name="right"/>, storing the result of the
/// addition in <paramref name="result"/>.
/// </summary>
/// <param name="left">The first vector to add.</param>
/// <param name="right">The second vector to add.</param>
/// <param name="result">The result of the vector addition.</param>
public static void Add(ref Vector2 left, ref Vector2 right, out Vector2 result)
{
result.X = left.X + right.X;
result.Y = left.Y + right.Y;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 2d-triangle.
/// </summary>
/// <param name="value1">The first vector of 2d-triangle.</param>
/// <param name="value2">The second vector of 2d-triangle.</param>
/// <param name="value3">The third vector of 2d-triangle.</param>
/// <param name="amount1">Barycentric scalar <c>b2</c> which represents a weighting factor towards second vector of 2d-triangle.</param>
/// <param name="amount2">Barycentric scalar <c>b3</c> which represents a weighting factor towards third vector of 2d-triangle.</param>
/// <returns>The cartesian translation of barycentric coordinates.</returns>
public static Vector2 Barycentric(Vector2 value1, Vector2 value2, Vector2 value3, float amount1, float amount2)
{
return new Vector2(
MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2),
MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2));
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 2d-triangle.
/// </summary>
/// <param name="value1">The first vector of 2d-triangle.</param>
/// <param name="value2">The second vector of 2d-triangle.</param>
/// <param name="value3">The third vector of 2d-triangle.</param>
/// <param name="amount1">Barycentric scalar <c>b2</c> which represents a weighting factor towards second vector of 2d-triangle.</param>
/// <param name="amount2">Barycentric scalar <c>b3</c> which represents a weighting factor towards third vector of 2d-triangle.</param>
/// <param name="result">The cartesian translation of barycentric coordinates as an output parameter.</param>
public static void Barycentric(ref Vector2 value1, ref Vector2 value2, ref Vector2 value3, float amount1, float amount2, out Vector2 result)
{
result.X = MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2);
result.Y = MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains CatmullRom interpolation of the specified vectors.
/// </summary>
/// <param name="value1">The first vector in interpolation.</param>
/// <param name="value2">The second vector in interpolation.</param>
/// <param name="value3">The third vector in interpolation.</param>
/// <param name="value4">The fourth vector in interpolation.</param>
/// <param name="amount">Weighting factor.</param>
/// <returns>The result of CatmullRom interpolation.</returns>
public static Vector2 CatmullRom(Vector2 value1, Vector2 value2, Vector2 value3, Vector2 value4, float amount)
{
return new Vector2(
MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount),
MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount));
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains CatmullRom interpolation of the specified vectors.
/// </summary>
/// <param name="value1">The first vector in interpolation.</param>
/// <param name="value2">The second vector in interpolation.</param>
/// <param name="value3">The third vector in interpolation.</param>
/// <param name="value4">The fourth vector in interpolation.</param>
/// <param name="amount">Weighting factor.</param>
/// <param name="result">The result of CatmullRom interpolation as an output parameter.</param>
public static void CatmullRom(ref Vector2 value1, ref Vector2 value2, ref Vector2 value3, ref Vector2 value4, float amount, out Vector2 result)
{
result.X = MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount);
result.Y = MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount);
}
/// <summary>
/// Round the members of this <see cref="Vector2"/> towards positive infinity.
/// </summary>
public void Ceiling()
{
X = (float)Math.Ceiling(X);
Y = (float)Math.Ceiling(Y);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains members from another vector rounded towards positive infinity.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <returns>The rounded <see cref="Vector2"/>.</returns>
public static Vector2 Ceiling(Vector2 value)
{
value.X = (float)Math.Ceiling(value.X);
value.Y = (float)Math.Ceiling(value.Y);
return value;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains members from another vector rounded towards positive infinity.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <param name="result">The rounded <see cref="Vector2"/>.</param>
public static void Ceiling(ref Vector2 value, out Vector2 result)
{
result.X = (float)Math.Ceiling(value.X);
result.Y = (float)Math.Ceiling(value.Y);
}
/// <summary>
/// Clamps the specified value within a range.
/// </summary>
/// <param name="value1">The value to clamp.</param>
/// <param name="min">The min value.</param>
/// <param name="max">The max value.</param>
/// <returns>The clamped value.</returns>
public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max)
{
return new Vector2(
MathHelper.Clamp(value1.X, min.X, max.X),
MathHelper.Clamp(value1.Y, min.Y, max.Y));
}
/// <summary>
/// Clamps the specified value within a range.
/// </summary>
/// <param name="value1">The value to clamp.</param>
/// <param name="min">The min value.</param>
/// <param name="max">The max value.</param>
/// <param name="result">The clamped value as an output parameter.</param>
public static void Clamp(ref Vector2 value1, ref Vector2 min, ref Vector2 max, out Vector2 result)
{
result.X = MathHelper.Clamp(value1.X, min.X, max.X);
result.Y = MathHelper.Clamp(value1.Y, min.Y, max.Y);
}
/// <summary>
/// Returns the distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The distance between two vectors.</returns>
public static float Distance(Vector2 value1, Vector2 value2)
{
float dX = value1.X - value2.X;
float dY = value1.Y - value2.Y;
return (float)Math.Sqrt(dX * dX + dY * dY);
}
/// <summary>
/// Returns the distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">The distance between two vectors as an output parameter.</param>
public static void Distance(ref Vector2 value1, ref Vector2 value2, out float result)
{
float v1 = value1.X - value2.X, v2 = value1.Y - value2.Y;
result = (float)Math.Sqrt((v1 * v1) + (v2 * v2));
}
/// <summary>
/// Returns the squared distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The squared distance between two vectors.</returns>
public static float DistanceSquared(Vector2 value1, Vector2 value2)
{
float dX = value1.X - value2.X;
float dY = value1.Y - value2.Y;
return (dX * dX + dY * dY);
}
/// <summary>
/// Returns the squared distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">The squared distance between two vectors as an output parameter.</param>
public static void DistanceSquared(ref Vector2 value1, ref Vector2 value2, out float result)
{
float v1 = value1.X - value2.X, v2 = value1.Y - value2.Y;
result = (v1 * v1) + (v2 * v2);
}
/// <summary>
/// Divides the components of a <see cref="Vector2"/> by the components of another <see cref="Vector2"/>.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Divisor <see cref="Vector2"/>.</param>
/// <returns>The result of dividing the vectors.</returns>
public static Vector2 Divide(Vector2 left, Vector2 right)
{
left.X /= right.X;
left.Y /= right.Y;
return left;
}
/// <summary>
/// Divides the components of a <see cref="Vector2"/> by the components of another <see cref="Vector2"/>.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Divisor <see cref="Vector2"/>.</param>
/// <param name="result">The result of dividing the vectors as an output parameter.</param>
public static void Divide(ref Vector2 left, ref Vector2 right, out Vector2 result)
{
result.X = left.X / right.X;
result.Y = left.Y / right.Y;
}
/// <summary>
/// Divides the components of a <see cref="Vector2"/> by a scalar.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Divisor scalar.</param>
/// <returns>The result of dividing a vector by a scalar.</returns>
public static Vector2 Divide(Vector2 left, float right)
{
float factor = 1 / right;
left.X *= factor;
left.Y *= factor;
return left;
}
/// <summary>
/// Divides the components of a <see cref="Vector2"/> by a scalar.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Divisor scalar.</param>
/// <param name="result">The result of dividing a vector by a scalar as an output parameter.</param>
public static void Divide(ref Vector2 left, float right, out Vector2 result)
{
float factor = 1 / right;
result.X = left.X * factor;
result.Y = left.Y * factor;
}
/// <summary>
/// Returns a dot product of two vectors.
/// </summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <returns>The dot product of two vectors.</returns>
public static float Dot(Vector2 left, Vector2 right)
{
return left.X * right.X + left.Y * right.Y;
}
/// <summary>
/// Returns a dot product of two vectors.
/// </summary>
/// <param name="left">The first vector.</param>
/// <param name="right">The second vector.</param>
/// <param name="result">The dot product of two vectors as an output parameter.</param>
public static void Dot(ref Vector2 left, ref Vector2 right, out float result)
{
result = left.X * right.X + left.Y * right.Y;
}
/// <summary>
/// Compares whether current instance is equal to specified <see cref="Object"/>.
/// </summary>
/// <param name="obj">The <see cref="Object"/> to compare.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public override bool Equals(object obj)
{
if (obj is Vector2)
{
return Equals((Vector2)obj);
}
return false;
}
/// <summary>
/// Compares whether current instance is equal to specified <see cref="Vector2"/>.
/// </summary>
/// <param name="other">The <see cref="Vector2"/> to compare.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public bool Equals(Vector2 other)
{
return (X == other.X) && (Y == other.Y);
}
/// <summary>
/// Round the members of this <see cref="Vector2"/> towards negative infinity.
/// </summary>
public void Floor()
{
X = (float)Math.Floor(X);
Y = (float)Math.Floor(Y);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains members from another vector rounded towards negative infinity.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <returns>The rounded <see cref="Vector2"/>.</returns>
public static Vector2 Floor(Vector2 value)
{
value.X = (float)Math.Floor(value.X);
value.Y = (float)Math.Floor(value.Y);
return value;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains members from another vector rounded towards negative infinity.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <param name="result">The rounded <see cref="Vector2"/>.</param>
public static void Floor(ref Vector2 value, out Vector2 result)
{
result.X = (float)Math.Floor(value.X);
result.Y = (float)Math.Floor(value.Y);
}
/// <summary>
/// Gets the hash code of this <see cref="Vector2"/>.
/// </summary>
/// <returns>Hash code of this <see cref="Vector2"/>.</returns>
public override int GetHashCode()
{
unchecked
{
return (X.GetHashCode() * 397) ^ Y.GetHashCode();
}
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains hermite spline interpolation.
/// </summary>
/// <param name="value1">The first position vector.</param>
/// <param name="tangent1">The first tangent vector.</param>
/// <param name="value2">The second position vector.</param>
/// <param name="tangent2">The second tangent vector.</param>
/// <param name="amount">Weighting factor.</param>
/// <returns>The hermite spline interpolation vector.</returns>
public static Vector2 Hermite(Vector2 value1, Vector2 tangent1, Vector2 value2, Vector2 tangent2, float amount)
{
return new Vector2(MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount), MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount));
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains hermite spline interpolation.
/// </summary>
/// <param name="value1">The first position vector.</param>
/// <param name="tangent1">The first tangent vector.</param>
/// <param name="value2">The second position vector.</param>
/// <param name="tangent2">The second tangent vector.</param>
/// <param name="amount">Weighting factor.</param>
/// <param name="result">The hermite spline interpolation vector as an output parameter.</param>
public static void Hermite(ref Vector2 value1, ref Vector2 tangent1, ref Vector2 value2, ref Vector2 tangent2, float amount, out Vector2 result)
{
result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount);
result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount);
}
/// <summary>
/// Returns the length of this <see cref="Vector2"/>.
/// </summary>
/// <returns>The length of this <see cref="Vector2"/>.</returns>
public float Length()
{
return (float)Math.Sqrt((X * X) + (Y * Y));
}
/// <summary>
/// Returns the squared length of this <see cref="Vector2"/>.
/// </summary>
/// <returns>The squared length of this <see cref="Vector2"/>.</returns>
public float LengthSquared()
{
return (X * X) + (Y * Y);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains linear interpolation of the specified vectors.
/// </summary>
/// <param name="start">The first vector.</param>
/// <param name="end">The second vector.</param>
/// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
/// <returns>The result of linear interpolation of the specified vectors.</returns>
public static Vector2 Lerp(Vector2 start, Vector2 end, float amount)
{
Vector2 result;
result.X = MathHelper.Lerp(start.X, end.X, amount);
result.Y = MathHelper.Lerp(start.Y, end.Y, amount);
return result;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains linear interpolation of the specified vectors.
/// </summary>
/// <param name="start">The first vector.</param>
/// <param name="end">The second vector.</param>
/// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
/// <param name="result">The result of linear interpolation of the specified vectors as an output parameter.</param>
public static void Lerp(ref Vector2 start, ref Vector2 end, float amount, out Vector2 result)
{
result.X = MathHelper.Lerp(start.X, end.X, amount);
result.Y = MathHelper.Lerp(start.Y, end.Y, amount);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains linear interpolation of the specified vectors.
/// Uses <see cref="MathHelper.LerpPrecise"/> on MathHelper for the interpolation.
/// Less efficient but more precise compared to <see cref="Vector2.Lerp(Vector2, Vector2, float)"/>.
/// See remarks section of <see cref="MathHelper.LerpPrecise"/> on MathHelper for more info.
/// </summary>
/// <param name="start">The first vector.</param>
/// <param name="end">The second vector.</param>
/// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
/// <returns>The result of linear interpolation of the specified vectors.</returns>
public static Vector2 LerpPrecise(Vector2 start, Vector2 end, float amount)
{
Vector2 result;
result.X = MathHelper.LerpPrecise(start.X, end.X, amount);
result.Y = MathHelper.LerpPrecise(start.Y, end.Y, amount);
return result;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains linear interpolation of the specified vectors.
/// Uses <see cref="MathHelper.LerpPrecise"/> on MathHelper for the interpolation.
/// Less efficient but more precise compared to <see cref="Vector2.Lerp(ref Vector2, ref Vector2, float, out Vector2)"/>.
/// See remarks section of <see cref="MathHelper.LerpPrecise"/> on MathHelper for more info.
/// </summary>
/// <param name="start">The first vector.</param>
/// <param name="end">The second vector.</param>
/// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
/// <param name="result">The result of linear interpolation of the specified vectors as an output parameter.</param>
public static void LerpPrecise(ref Vector2 start, ref Vector2 end, float amount, out Vector2 result)
{
result.X = MathHelper.LerpPrecise(start.X, end.X, amount);
result.Y = MathHelper.LerpPrecise(start.Y, end.Y, amount);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains a maximal values from the two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The <see cref="Vector2"/> with maximal values from the two vectors.</returns>
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>
/// Creates a new <see cref="Vector2"/> that contains a maximal values from the two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">The <see cref="Vector2"/> with maximal values from the two vectors as an output parameter.</param>
public static void Max(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
{
result.X = value1.X > value2.X ? value1.X : value2.X;
result.Y = value1.Y > value2.Y ? value1.Y : value2.Y;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains a minimal values from the two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The <see cref="Vector2"/> with minimal values from the two vectors.</returns>
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>
/// Creates a new <see cref="Vector2"/> that contains a minimal values from the two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">The <see cref="Vector2"/> with minimal values from the two vectors as an output parameter.</param>
public static void Min(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
{
result.X = value1.X < value2.X ? value1.X : value2.X;
result.Y = value1.Y < value2.Y ? value1.Y : value2.Y;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains a multiplication of two vectors.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Source <see cref="Vector2"/>.</param>
/// <returns>The result of the vector multiplication.</returns>
public static Vector2 Multiply(Vector2 left, Vector2 right)
{
left.X *= right.X;
left.Y *= right.Y;
return left;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains a multiplication of two vectors.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Source <see cref="Vector2"/>.</param>
/// <param name="result">The result of the vector multiplication as an output parameter.</param>
public static void Multiply(ref Vector2 left, ref Vector2 right, out Vector2 result)
{
result.X = left.X * right.X;
result.Y = left.Y * right.Y;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains a multiplication of <see cref="Vector2"/> and a scalar.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Scalar value.</param>
/// <returns>The result of the vector multiplication with a scalar.</returns>
public static Vector2 Multiply(Vector2 left, float right)
{
left.X *= right;
left.Y *= right;
return left;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains a multiplication of <see cref="Vector2"/> and a scalar.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Scalar value.</param>
/// <param name="result">The result of the multiplication with a scalar as an output parameter.</param>
public static void Multiply(ref Vector2 left, float right, out Vector2 result)
{
result.X = left.X * right;
result.Y = left.Y * right;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains the specified vector inversion.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <returns>The result of the vector inversion.</returns>
public static Vector2 Negate(Vector2 value)
{
value.X = -value.X;
value.Y = -value.Y;
return value;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains the specified vector inversion.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <param name="result">The result of the vector inversion as an output parameter.</param>
public static void Negate(ref Vector2 value, out Vector2 result)
{
result.X = -value.X;
result.Y = -value.Y;
}
/// <summary>
/// Turns this <see cref="Vector2"/> to a unit vector with the same direction.
/// </summary>
public void Normalize()
{
float val = 1.0f / (float)Math.Sqrt((X * X) + (Y * Y));
X *= val;
Y *= val;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains a normalized values from another vector.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <returns>Unit vector.</returns>
public static Vector2 Normalize(Vector2 value)
{
float val = 1.0f / (float)Math.Sqrt((value.X * value.X) + (value.Y * value.Y));
value.X *= val;
value.Y *= val;
return value;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains a normalized values from another vector.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <param name="result">Unit vector as an output parameter.</param>
public static void Normalize(ref Vector2 value, out Vector2 result)
{
float val = 1.0f / (float)Math.Sqrt((value.X * value.X) + (value.Y * value.Y));
result.X = value.X * val;
result.Y = value.Y * val;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains reflect vector of the given vector and normal.
/// </summary>
/// <param name="vector">Source <see cref="Vector2"/>.</param>
/// <param name="normal">Reflection normal.</param>
/// <returns>Reflected vector.</returns>
public static Vector2 Reflect(Vector2 vector, Vector2 normal)
{
Vector2 result;
float val = 2.0f * ((vector.X * normal.X) + (vector.Y * normal.Y));
result.X = vector.X - (normal.X * val);
result.Y = vector.Y - (normal.Y * val);
return result;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains reflect vector of the given vector and normal.
/// </summary>
/// <param name="vector">Source <see cref="Vector2"/>.</param>
/// <param name="normal">Reflection normal.</param>
/// <param name="result">Reflected vector as an output parameter.</param>
public static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result)
{
float val = 2.0f * ((vector.X * normal.X) + (vector.Y * normal.Y));
result.X = vector.X - (normal.X * val);
result.Y = vector.Y - (normal.Y * val);
}
/// <summary>
/// Round the members of this <see cref="Vector2"/> to the nearest integer value.
/// </summary>
public void Round()
{
X = (float)Math.Round(X);
Y = (float)Math.Round(Y);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains members from another vector rounded to the nearest integer value.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <returns>The rounded <see cref="Vector2"/>.</returns>
public static Vector2 Round(Vector2 value)
{
value.X = (float)Math.Round(value.X);
value.Y = (float)Math.Round(value.Y);
return value;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains members from another vector rounded to the nearest integer value.
/// </summary>
/// <param name="value">Source <see cref="Vector2"/>.</param>
/// <param name="result">The rounded <see cref="Vector2"/>.</param>
public static void Round(ref Vector2 value, out Vector2 result)
{
result.X = (float)Math.Round(value.X);
result.Y = (float)Math.Round(value.Y);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains cubic interpolation of the specified vectors.
/// </summary>
/// <param name="value1">Source <see cref="Vector2"/>.</param>
/// <param name="value2">Source <see cref="Vector2"/>.</param>
/// <param name="amount">Weighting value.</param>
/// <returns>Cubic interpolation of the specified vectors.</returns>
public static Vector2 SmoothStep(Vector2 value1, Vector2 value2, float amount)
{
return new Vector2(
MathHelper.SmoothStep(value1.X, value2.X, amount),
MathHelper.SmoothStep(value1.Y, value2.Y, amount));
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains cubic interpolation of the specified vectors.
/// </summary>
/// <param name="value1">Source <see cref="Vector2"/>.</param>
/// <param name="value2">Source <see cref="Vector2"/>.</param>
/// <param name="amount">Weighting value.</param>
/// <param name="result">Cubic interpolation of the specified vectors as an output parameter.</param>
public static void SmoothStep(ref Vector2 value1, ref Vector2 value2, float amount, out Vector2 result)
{
result.X = MathHelper.SmoothStep(value1.X, value2.X, amount);
result.Y = MathHelper.SmoothStep(value1.Y, value2.Y, amount);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains subtraction of on <see cref="Vector2"/> from a another.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Source <see cref="Vector2"/>.</param>
/// <returns>The result of the vector subtraction.</returns>
public static Vector2 Subtract(Vector2 left, Vector2 right)
{
left.X -= right.X;
left.Y -= right.Y;
return left;
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains subtraction of on <see cref="Vector2"/> from a another.
/// </summary>
/// <param name="left">Source <see cref="Vector2"/>.</param>
/// <param name="right">Source <see cref="Vector2"/>.</param>
/// <param name="result">The result of the vector subtraction as an output parameter.</param>
public static void Subtract(ref Vector2 left, ref Vector2 right, out Vector2 result)
{
result.X = left.X - right.X;
result.Y = left.Y - right.Y;
}
/// <summary>
/// Returns a <see cref="String"/> representation of this <see cref="Vector2"/> in the format:
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>]}
/// </summary>
/// <returns>A <see cref="String"/> representation of this <see cref="Vector2"/>.</returns>
public override string ToString()
{
return String.Format("{{X: {0}, Y: {1} }}", X, Y);
}
/// <summary>
/// Gets a <see cref="Point"/> representation for this object.
/// </summary>
/// <returns>A <see cref="Point"/> representation for this object.</returns>
public Point ToPoint()
{
return new Point((int) X,(int) Y);
}
/// <summary>
/// Creates a new <see cref="Vector2"/> that contains a transformation of 2d-vector by the specified <see cref="Matrix"/>.
/// </summary>
/// <param name="position">Source <see cref="Vector2"/>.</param>
/// <param name="matrix">The transformation <see cref="Matrix"/>.</param>
/// <returns>Transformed <see cref="Vector2"/>.</returns>
public static Vector2 Transform(Vector2 position, Matrix matrix)
{
return new Vector2((position.X * matrix.M11) + (position.Y * matrix.M21) + matrix.M41, (position.X * matrix.M12) + (position.Y * matrix.M22) + matrix.M42);
}