Jypeli 10
The simple game programming library
Vector2D.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26#if UseDouble
27using Scalar = System.Double;
28#else
29using Scalar = System.Single;
30#endif
31using System;
32using System.Runtime.InteropServices;
34using System.Xml.Serialization;
35
36namespace AdvanceMath
37{
38
43 [StructLayout(LayoutKind.Sequential, Size = Vector2D.Size)]
44 [AdvBrowsableOrder("X,Y")]
45#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE
46 [Serializable]
47 [System.ComponentModel.TypeConverter(typeof(AdvTypeConverter<Vector2D>))]
48#endif
49 public struct Vector2D : IVector<Vector2D>
50 {
51 #region const fields
55 public const int Count = 2;
59 public const int Size = sizeof(Scalar) * Count;
60 #endregion
61 #region readonly fields
65 public static readonly Vector2D One = new Vector2D(1,1);
69 public static readonly Vector2D Zero = new Vector2D();
73 public static readonly Vector2D XAxis = new Vector2D(1, 0);
77 public static readonly Vector2D YAxis = new Vector2D(0, 1);
81 public static readonly Vector2D XYAxis = new Vector2D((Scalar)0.7071067811865475244008443621052, (Scalar)0.7071067811865475244008443621052);
82
83
84 private static readonly string FormatString = MatrixHelper.CreateVectorFormatString(Count);
86 #endregion
87 #region static methods
88 public static void Copy(ref Vector2D vector, Scalar[] destArray)
89 {
90 Copy(ref vector, destArray, 0);
91 }
92 public static void Copy(ref Vector2D vector, Scalar[] destArray, int index)
93 {
94 ThrowHelper.CheckCopy(destArray, index, Count);
95
96 destArray[index] = vector.X;
97 destArray[++index] = vector.Y;
98 }
99 public static void Copy(Scalar[] sourceArray, out Vector2D result)
100 {
101 Copy(sourceArray, 0, out result);
102 }
103 public static void Copy(Scalar[] sourceArray, int index, out Vector2D result)
104 {
105 ThrowHelper.CheckCopy(sourceArray, index, Count);
106
107 result.X = sourceArray[index];
108 result.Y = sourceArray[++index];
109 }
110 public static void Copy(ref Vector4D source, out Vector2D dest)
111 {
112 dest.X = source.X;
113 dest.Y = source.Y;
114 }
115 public static void Copy(ref Vector3D source, out Vector2D dest)
116 {
117 dest.X = source.X;
118 dest.Y = source.Y;
119 }
120
128 public static Vector2D Clamp(Vector2D value, Vector2D min, Vector2D max)
129 {
130 Vector2D result;
131 MathHelper.Clamp(ref value.X, ref min.X, ref max.X, out result.X);
132 MathHelper.Clamp(ref value.Y, ref min.Y, ref max.Y, out result.Y);
133 return result;
134 }
135 public static void Clamp(ref Vector2D value, ref Vector2D min, ref Vector2D max, out Vector2D result)
136 {
137 MathHelper.Clamp(ref value.X, ref min.X, ref max.X, out result.X);
138 MathHelper.Clamp(ref value.Y, ref min.Y, ref max.Y, out result.Y);
139 }
140
141 public static Vector2D Lerp(Vector2D left, Vector2D right, Scalar amount)
142 {
143 Vector2D result;
144 Lerp(ref left, ref right, ref amount, out result);
145 return result;
146 }
147 public static void Lerp(ref Vector2D left, ref Vector2D right, ref Scalar amount, out Vector2D result)
148 {
149 result.X = (right.X - left.X) * amount + left.X;
150 result.Y = (right.Y - left.Y) * amount + left.Y;
151 }
152 public static Vector2D Lerp(Vector2D left, Vector2D right, Vector2D amount)
153 {
154 Vector2D result;
155 Lerp(ref left, ref right, ref amount, out result);
156 return result;
157 }
158 public static void Lerp(ref Vector2D left, ref Vector2D right, ref Vector2D amount, out Vector2D result)
159 {
160 result.X = (right.X - left.X) * amount.X + left.X;
161 result.Y = (right.Y - left.Y) * amount.Y + left.Y;
162 }
163
164 public static Scalar Distance(Vector2D left, Vector2D right)
165 {
166 Scalar x, y;
167 x = left.X - right.X;
168 y = left.Y - right.Y;
169 return MathHelper.Sqrt(x * x + y * y);
170 }
171 public static void Distance(ref Vector2D left, ref Vector2D right, out Scalar result)
172 {
173 Scalar x, y;
174 x = left.X - right.X;
175 y = left.Y - right.Y;
176 result = MathHelper.Sqrt(x * x + y * y);
177 }
178 public static Scalar DistanceSq(Vector2D left, Vector2D right)
179 {
180 Scalar x, y;
181 x = left.X - right.X;
182 y = left.Y - right.Y;
183 return x * x + y * y;
184 }
185 public static void DistanceSq(ref Vector2D left, ref Vector2D right, out Scalar result)
186 {
187 Scalar x, y;
188 x = left.X - right.X;
189 y = left.Y - right.Y;
190 result = x * x + y * y;
191 }
206 public static Vector2D FromLengthAndAngle(Scalar length, Scalar radianAngle)
207 {
208 Vector2D result;
209 result.X = length * MathHelper.Cos(radianAngle);
210 result.Y = length * MathHelper.Sin(radianAngle);
211 return result;
212 }
213 public static void FromLengthAndAngle(ref Scalar length, ref Scalar radianAngle, out Vector2D result)
214 {
215 result.X = length * MathHelper.Cos(radianAngle);
216 result.Y = length * MathHelper.Sin(radianAngle);
217 }
224 public static Vector2D Rotate(Scalar radianAngle, Vector2D source)
225 {
226 Scalar negradianAngle = -radianAngle;
227 Scalar cos = MathHelper.Cos(negradianAngle);
228 Scalar sin = MathHelper.Sin(negradianAngle);
229 Vector2D result;
230 result.X = source.X * cos + source.Y * sin;
231 result.Y = source.Y * cos - source.X * sin;
232 return result;
233 }
234 public static void Rotate(ref Scalar radianAngle, ref Vector2D source, out Vector2D result)
235 {
236 Scalar negradianAngle = -radianAngle;
237 Scalar cos = MathHelper.Cos(negradianAngle);
238 Scalar sin = MathHelper.Sin(negradianAngle);
239 result.X = source.X * cos + source.Y * sin;
240 result.Y = source.Y * cos - source.X * sin;
241 }
248 public static Vector2D SetAngle(Vector2D source, Scalar radianAngle)
249 {
250 Scalar magnitude;
251 GetMagnitude(ref source, out magnitude);
252 Vector2D result;
253 result.X = magnitude * MathHelper.Cos(radianAngle);
254 result.Y = magnitude * MathHelper.Sin(radianAngle);
255 return result;
256 }
257 public static void SetAngle(ref Vector2D source, ref Scalar radianAngle, out Vector2D result)
258 {
259 Scalar magnitude;
260 GetMagnitude(ref source, out magnitude);
261 result.X = magnitude * MathHelper.Cos(radianAngle);
262 result.Y = magnitude * MathHelper.Sin(radianAngle);
263 }
269 public static Scalar GetAngle(Vector2D source)
270 {
271 Scalar result = MathHelper.Atan2(source.Y, source.X);
272 if (result < 0) { result += MathHelper.TwoPi; }
273 return result;
274 }
275 public static void GetAngle(ref Vector2D source, out Scalar result)
276 {
277 result = MathHelper.Atan2(source.Y, source.X);
278 if (result < 0) { result += MathHelper.TwoPi; }
279 }
287 public static Vector2D Add(Vector2D left, Vector2D right)
288 {
289 Vector2D result;
290 result.X = left.X + right.X;
291 result.Y = left.Y + right.Y;
292 return result;
293 }
294 public static void Add(ref Vector2D left, ref Vector2D right, out Vector2D result)
295 {
296 result.X = left.X + right.X;
297 result.Y = left.Y + right.Y;
298 }
306 public static Vector2D Subtract(Vector2D left, Vector2D right)
307 {
308 Vector2D result;
309 result.X = left.X - right.X;
310 result.Y = left.Y - right.Y;
311 return result;
312 }
313 public static void Subtract(ref Vector2D left, ref Vector2D right, out Vector2D result)
314 {
315 result.X = left.X - right.X;
316 result.Y = left.Y - right.Y;
317 }
325 public static Vector2D Transform(Matrix3x3 matrix, Vector2D source)
326 {
327 Scalar inverseZ = 1 / (source.X * matrix.m20 + source.Y * matrix.m21 + matrix.m22);
328 Vector2D result;
329 result.X = (source.X * matrix.m00 + source.Y * matrix.m01 + matrix.m02) * inverseZ;
330 result.Y = (source.X * matrix.m10 + source.Y * matrix.m11 + matrix.m12) * inverseZ;
331 return result;
332 }
333 public static void Transform(ref Matrix3x3 matrix, ref Vector2D source, out Vector2D result)
334 {
335 Scalar X = source.X;
336 Scalar inverseZ = 1 / (X * matrix.m20 + source.Y * matrix.m21 + matrix.m22);
337 result.X = (X * matrix.m00 + source.Y * matrix.m01 + matrix.m02) * inverseZ;
338 result.Y = (X * matrix.m10 + source.Y * matrix.m11 + matrix.m12) * inverseZ;
339 }
340 public static Vector2D TransformNormal(Matrix3x3 matrix, Vector2D source)
341 {
342 Vector2D result;
343 result.X = (source.X * matrix.m00 + source.Y * matrix.m01);
344 result.Y = (source.X * matrix.m10 + source.Y * matrix.m11);
345 return result;
346 }
347 public static void TransformNormal(ref Matrix3x3 matrix, ref Vector2D source, out Vector2D result)
348 {
349 Scalar X = source.X;
350 result.X = (X * matrix.m00 + source.Y * matrix.m01);
351 result.Y = (X * matrix.m10 + source.Y * matrix.m11);
352 }
360 public static Vector2D Transform(Matrix2x3 matrix, Vector2D source)
361 {
362 Vector2D result;
363 result.X = (source.X * matrix.m00 + source.Y * matrix.m01 + matrix.m02);
364 result.Y = (source.X * matrix.m10 + source.Y * matrix.m11 + matrix.m12);
365 return result;
366 }
367 public static void Transform(ref Matrix2x3 matrix, ref Vector2D source, out Vector2D result)
368 {
369 Scalar X = source.X;
370 result.X = (X * matrix.m00 + source.Y * matrix.m01 + matrix.m02);
371 result.Y = (X * matrix.m10 + source.Y * matrix.m11 + matrix.m12);
372 }
373 public static Vector2D TransformNormal(Matrix2x3 matrix, Vector2D source)
374 {
375 Vector2D result;
376 result.X = (source.X * matrix.m00 + source.Y * matrix.m01);
377 result.Y = (source.X * matrix.m10 + source.Y * matrix.m11);
378 return result;
379 }
380 public static void TransformNormal(ref Matrix2x3 matrix, ref Vector2D source, out Vector2D result)
381 {
382 Scalar X = source.X;
383 result.X = (X * matrix.m00 + source.Y * matrix.m01);
384 result.Y = (X * matrix.m10 + source.Y * matrix.m11);
385 }
393 public static Vector2D Transform(Matrix2x2 matrix, Vector2D source)
394 {
395 Vector2D result;
396 result.X = (source.X * matrix.m00 + source.Y * matrix.m01);
397 result.Y = (source.X * matrix.m10 + source.Y * matrix.m11);
398 return result;
399 }
400 public static void Transform(ref Matrix2x2 matrix, ref Vector2D source, out Vector2D result)
401 {
402 Scalar X = source.X;
403 result.X = (X * matrix.m00 + source.Y * matrix.m01);
404 result.Y = (X * matrix.m10 + source.Y * matrix.m11);
405 }
413 public static Vector2D Multiply(Vector2D source, Scalar scalar)
414 {
415 Vector2D result;
416 result.X = source.X * scalar;
417 result.Y = source.Y * scalar;
418 return result;
419 }
420 public static void Multiply(ref Vector2D source, ref Scalar scalar, out Vector2D result)
421 {
422 result.X = source.X * scalar;
423 result.Y = source.Y * scalar;
424 }
425
426 public static Vector2D Multiply(Scalar scalar, Vector2D source)
427 {
428 Vector2D result;
429 result.X = scalar * source.X;
430 result.Y = scalar * source.Y;
431 return result;
432 }
433 public static void Multiply(ref Scalar scalar, ref Vector2D source, out Vector2D result)
434 {
435 result.X = scalar * source.X;
436 result.Y = scalar * source.Y;
437 }
445 public static Scalar Dot(Vector2D left, Vector2D right)
446 {
447 return left.Y * right.Y + left.X * right.X;
448 }
449 public static void Dot(ref Vector2D left, ref Vector2D right, out Scalar result)
450 {
451 result = left.X * right.X + left.Y * right.Y;
452 }
471 public static Scalar ZCross(Vector2D left, Vector2D right)
472 {
473 return left.X * right.Y - left.Y * right.X;
474 }
475 public static void ZCross(ref Vector2D left, ref Vector2D right, out Scalar result)
476 {
477 result = left.X * right.Y - left.Y * right.X;
478 }
497 public static Vector2D ZCross(Scalar leftZ, Vector2D right)
498 {
499 Vector2D result;
500 result.X = -leftZ * right.Y;
501 result.Y = leftZ * right.X;
502 return result;
503 }
504 public static void ZCross(ref Scalar leftZ, ref Vector2D right, out Vector2D result)
505 {
506 Scalar rightX = right.X;
507 result.X = -leftZ * right.Y;
508 result.Y = leftZ * rightX;
509 }
528 public static Vector2D ZCross(Vector2D left, Scalar rightZ)
529 {
530 Vector2D result;
531 result.X = left.Y * rightZ;
532 result.Y = -left.X * rightZ;
533 return result;
534 }
535 public static void ZCross(ref Vector2D left, ref Scalar rightZ, out Vector2D result)
536 {
537 Scalar leftX = left.X;
538 result.X = left.Y * rightZ;
539 result.Y = -leftX * rightZ;
540 }
546 public static Scalar GetMagnitudeSq(Vector2D source)
547 {
548 return source.X * source.X + source.Y * source.Y;
549 }
550 public static void GetMagnitudeSq(ref Vector2D source, out Scalar result)
551 {
552 result = source.X * source.X + source.Y * source.Y;
553 }
559 public static Scalar GetMagnitude(Vector2D source)
560 {
561 return MathHelper.Sqrt(source.X * source.X + source.Y * source.Y);
562 }
563 public static void GetMagnitude(ref Vector2D source, out Scalar result)
564 {
565 result = MathHelper.Sqrt(source.X * source.X + source.Y * source.Y);
566 }
573 public static Vector2D SetMagnitude(Vector2D source, Scalar magnitude)
574 {
575 Vector2D result;
576 SetMagnitude(ref source, ref magnitude, out result);
577 return result;
578 }
579 public static void SetMagnitude(ref Vector2D source, ref Scalar magnitude, out Vector2D result)
580 {
581 if (magnitude == 0) { result = Zero; return; }
582 Scalar oldmagnitude = MathHelper.Sqrt(source.X * source.X + source.Y * source.Y);
583 if (oldmagnitude == 0) { result = Zero; return; }
584 oldmagnitude = (magnitude / oldmagnitude);
585 result.X = source.X * oldmagnitude;
586 result.Y = source.Y * oldmagnitude;
587 }
593 public static Vector2D Negate(Vector2D source)
594 {
595 Vector2D result;
596 result.X = -source.X;
597 result.Y = -source.Y;
598 return result;
599 }
600 public static void Negate(ref Vector2D source)
601 {
602 Negate(ref source, out source);
603 }
604 public static void Negate(ref Vector2D source, out Vector2D result)
605 {
606 result.X = -source.X;
607 result.Y = -source.Y;
608 }
615 public static Vector2D Normalize(Vector2D source)
616 {
617 Scalar oldmagnitude;
618 GetMagnitude(ref source, out oldmagnitude);
619 if (oldmagnitude == 0) { return Vector2D.Zero; }
620 oldmagnitude = (1 / oldmagnitude);
621 Vector2D result;
622 result.X = source.X * oldmagnitude;
623 result.Y = source.Y * oldmagnitude;
624 return result;
625 }
626 public static void Normalize(ref Vector2D source, out Vector2D result)
627 {
628 Scalar oldmagnitude;
629 GetMagnitude(ref source, out oldmagnitude);
630 if (oldmagnitude == 0) { result = Zero; return; }
631 oldmagnitude = (1 / oldmagnitude);
632 result.X = source.X * oldmagnitude;
633 result.Y = source.Y * oldmagnitude;
634 }
635 public static void Normalize(ref Vector2D source)
636 {
637 Normalize(ref source, out source);
638 }
646 public static Vector2D Normalize(Vector2D source, out Scalar magnitude)
647 {
648 Vector2D result;
649 Normalize(ref source, out magnitude, out result);
650 return result;
651 }
652 public static void Normalize(ref Vector2D source, out Scalar magnitude, out Vector2D result)
653 {
654 GetMagnitude(ref source, out magnitude);
655 if (magnitude > 0)
656 {
657 Scalar magnitudeInv = (1 / magnitude);
658 result.X = source.X * magnitudeInv;
659 result.Y = source.Y * magnitudeInv;
660 }
661 else
662 {
663 result = Zero;
664 }
665 }
673 public static Vector2D Project(Vector2D left, Vector2D right)
674 {
675 Vector2D result;
676 Project(ref left, ref right, out result);
677 return result;
678 }
679 public static void Project(ref Vector2D left, ref Vector2D right, out Vector2D result)
680 {
681 Scalar tmp, magsq;
682 Dot(ref left, ref right, out tmp);
683 GetMagnitudeSq(ref right, out magsq);
684 tmp /= magsq;
685 Multiply(ref right, ref tmp, out result);
686 }
694 {
695 Vector2D result;
696 result.X = -source.Y;
697 result.Y = source.X;
698 return result;
699 }
700 public static void GetRightHandNormal(ref Vector2D source, out Vector2D result)
701 {
702 Scalar sourceX = source.X;
703 result.X = -source.Y;
704 result.Y = sourceX;
705 }
712 public static Vector2D GetLeftHandNormal(Vector2D source)
713 {
714 Vector2D result;
715 result.X = source.Y;
716 result.Y = -source.X;
717 return result;
718 }
719 public static void GetLeftHandNormal(ref Vector2D source, out Vector2D result)
720 {
721 Scalar sourceX = source.X;
722 result.X = source.Y;
723 result.Y = -sourceX;
724 }
725
726 public static Vector2D Hermite(Vector2D value1, Vector2D tangent1, Vector2D value2, Vector2D tangent2, Scalar amount)
727 {
728 Vector2D result;
729 Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
730 return result;
731 }
732 public static void Hermite(ref Vector2D value1, ref Vector2D tangent1, ref Vector2D value2, ref Vector2D tangent2, Scalar amount, out Vector2D result)
733 {
734 Scalar h1, h2, h3, h4;
735 MathHelper.HermiteHelper(amount, out h1, out h2, out h3, out h4);
736 result.X = h1 * value1.X + h2 * value2.X + h3 * tangent1.X + h4 * tangent2.X;
737 result.Y = h1 * value1.Y + h2 * value2.Y + h3 * tangent1.Y + h4 * tangent2.Y;
738 }
739
740 public static Vector2D CatmullRom(Vector2D value1, Vector2D value2, Vector2D value3, Vector2D value4, Scalar amount)
741 {
742 Vector2D result;
743 CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out result);
744 return result;
745 }
746 public static void CatmullRom(ref Vector2D value1, ref Vector2D value2, ref Vector2D value3, ref Vector2D value4, Scalar amount, out Vector2D result)
747 {
748 Scalar amountSq = amount * amount;
749 Scalar amountCu = amountSq * amount;
750 result.X =
751 0.5f * ((2 * value2.X) +
752 (-value1.X + value3.X) * amount +
753 (2 * value1.X - 5 * value2.X + 4 * value3.X - value4.X) * amountSq +
754 (-value1.X + 3 * value2.X - 3 * value3.X + value4.X) * amountCu);
755 result.Y =
756 0.5f * ((2 * value2.Y) +
757 (-value1.Y + value3.Y) * amount +
758 (2 * value1.Y - 5 * value2.Y + 4 * value3.Y - value4.Y) * amountSq +
759 (-value1.Y + 3 * value2.Y - 3 * value3.Y + value4.Y) * amountCu);
760 }
761
762 public static Vector2D Max(Vector2D value1, Vector2D value2)
763 {
764 Vector2D result;
765 Max(ref value1, ref value2, out result);
766 return result;
767 }
768 public static void Max(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
769 {
770 result.X = (value1.X < value2.X) ? (value2.X) : (value1.X);
771 result.Y = (value1.Y < value2.Y) ? (value2.Y) : (value1.Y);
772 }
773
774 public static Vector2D Min(Vector2D value1, Vector2D value2)
775 {
776 Vector2D result;
777 Min(ref value1, ref value2, out result);
778 return result;
779 }
780 public static void Min(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
781 {
782 result.X = (value1.X > value2.X) ? (value2.X) : (value1.X);
783 result.Y = (value1.Y > value2.Y) ? (value2.Y) : (value1.Y);
784 }
785
786 #endregion
787 #region fields
791 [AdvBrowsable]
792 [XmlAttribute]
793#if !WINDOWS_STOREAPP
794 [System.ComponentModel.Description("The Magnitude on the X-Axis")]
795#endif
796 public Scalar X;
800 [AdvBrowsable]
801 [XmlAttribute]
802#if !WINDOWS_STOREAPP
803 [System.ComponentModel.Description("The Magnitude on the Y-Axis")]
804#endif
805 public Scalar Y;
806 #endregion
807 #region constructors
813 [InstanceConstructor("X,Y")]
815 {
816 this.X = X;
817 this.Y = Y;
818 }
819 public Vector2D(Scalar[] vals) : this(vals, 0) { }
820 public Vector2D(Scalar[] vals, int index)
821 {
822 Copy(vals, index, out this);
823 }
824 #endregion
825 #region indexers
826#if UNSAFE
834 public Scalar this[int index]
835 {
836 get
837 {
838 ThrowHelper.CheckIndex("index", index, Count);
839 unsafe
840 {
841 fixed (Scalar* ptr = &this.X)
842 {
843 return ptr[index];
844 }
845 }
846 }
847 set
848 {
849 ThrowHelper.CheckIndex("index", index, Count);
850 unsafe
851 {
852 fixed (Scalar* ptr = &this.X)
853 {
854 ptr[index] = value;
855 }
856 }
857 }
858 }
859#endif
860 #endregion
861 #region public properties
867 {
868 get
869 {
870 return GetRightHandNormal(this);
871 }
872 }
878 {
879 get
880 {
881 return GetLeftHandNormal(this);
882 }
883 }
888 [XmlIgnore]
890 {
891 get
892 {
893 return MathHelper.Sqrt(this.X * this.X + this.Y * this.Y);
894 }
895 set
896 {
897 SetMagnitude(ref this, ref value, out this);
898 }
899 }
904 {
905 get
906 {
907 return this.X * this.X + this.Y * this.Y;
908 }
909 }
917 [XmlIgnore]
919 {
920 get
921 {
922 Scalar result;
923 GetAngle(ref this, out result);
924 return result;
925 }
926 set
927 {
928 SetAngle(ref this, ref value, out this);
929 }
930 }
936 {
937 get
938 {
939 return Normalize(this);
940 }
941 }
945 int IAdvanceValueType.Count { get { return Count; } }
946 #endregion
947 #region public methods
948 public Scalar[] ToArray()
949 {
950 Scalar[] array = new Scalar[Count];
951 Copy(ref this, array, 0);
952 return array;
953 }
954 public void CopyFrom(Scalar[] array, int index)
955 {
956 Copy(array, index, out this);
957 }
958 public void CopyTo(Scalar[] array, int index)
959 {
960 Copy(ref this, array, index);
961 }
963 {
964 Vector3D rv;
965 rv.X = this.X;
966 rv.Y = this.Y;
967 rv.Z = Z;
968 return rv;
969 }
970 #endregion
971 #region operators
979 public static Vector2D operator +(Vector2D left, Vector2D right)
980 {
981 Vector2D result;
982 result.X = left.X + right.X;
983 result.Y = left.Y + right.Y;
984 return result;
985 }
993 public static Vector2D operator -(Vector2D left, Vector2D right)
994 {
995 Vector2D result;
996 result.X = left.X - right.X;
997 result.Y = left.Y - right.Y;
998 return result;
999 }
1007 public static Vector2D operator *(Vector2D source, Scalar scalar)
1008 {
1009 Vector2D result;
1010 result.X = source.X * scalar;
1011 result.Y = source.Y * scalar;
1012 return result;
1013 }
1021 public static Vector2D operator *(Scalar scalar, Vector2D source)
1022 {
1023 Vector2D result;
1024 result.X = scalar * source.X;
1025 result.Y = scalar * source.Y;
1026 return result;
1027 }
1035 public static Scalar operator *(Vector2D left, Vector2D right)
1036 {
1037 return left.Y * right.Y + left.X * right.X;
1038 }
1039 public static Vector2D operator *(Matrix2x3 matrix, Vector2D source)
1040 {
1041 Vector2D result;
1042 result.X = (source.X * matrix.m00 + source.Y * matrix.m01 + matrix.m02);
1043 result.Y = (source.X * matrix.m10 + source.Y * matrix.m11 + matrix.m12);
1044 return result;
1045 }
1046 public static Vector2D operator *(Matrix3x3 matrix, Vector2D source)
1047 {
1048 Scalar inverseZ = 1 / (source.X * matrix.m20 + source.Y * matrix.m21 + matrix.m22);
1049 Vector2D result;
1050 result.X = (source.X * matrix.m00 + source.Y * matrix.m01 + matrix.m02) * inverseZ;
1051 result.Y = (source.X * matrix.m10 + source.Y * matrix.m11 + matrix.m12) * inverseZ;
1052 return result;
1053 }
1054 public static Vector2D operator *(Matrix2x2 matrix, Vector2D source)
1055 {
1056 Vector2D result;
1057 result.X = (source.X * matrix.m00 + source.Y * matrix.m01);
1058 result.Y = (source.X * matrix.m10 + source.Y * matrix.m11);
1059 return result;
1060 }
1066 public static Vector2D operator -(Vector2D source)
1067 {
1068 Vector2D result;
1069 result.X = -source.X;
1070 result.Y = -source.Y;
1071 return result;
1072 }
1091 public static Scalar operator ^(Vector2D left, Vector2D right)
1092 {
1093 return left.X * right.Y - left.Y * right.X;
1094 }
1113 public static Vector2D operator ^(Scalar leftZ, Vector2D right)
1114 {
1115 Vector2D result;
1116 result.X = -leftZ * right.Y;
1117 result.Y = leftZ * right.X;
1118 return result;
1119 }
1138 public static Vector2D operator ^(Vector2D left, Scalar rightZ)
1139 {
1140 Vector2D result;
1141 result.X = left.Y * rightZ;
1142 result.Y = -left.X * rightZ;
1143 return result;
1144 }
1151 public static bool operator ==(Vector2D left, Vector2D right)
1152 {
1153 return left.X == right.X && left.Y == right.Y;
1154 }
1161 public static bool operator !=(Vector2D left, Vector2D right)
1162 {
1163 return !(left.X == right.X && left.Y == right.Y);
1164 }
1165
1166
1167
1168 public static explicit operator Vector2D(Vector3D source)
1169 {
1170 Vector2D result;
1171 result.X = source.X;
1172 result.Y = source.Y;
1173 return result;
1174 }
1175
1176 #endregion
1177 #region overrides
1178 private string ToStringInternal(string FormatString)
1179 {
1180 return string.Format(FormatString, X, Y);
1181 }
1187 public string ToString(string format)
1188 {
1189 return ToStringInternal(string.Format(FormatableString, format));
1190 }
1191 public override string ToString()
1192 {
1194 }
1195
1196#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT
1197 public static bool TryParse(string s, out Vector2D result)
1198 {
1199 if (s == null)
1200 {
1201 result = Zero;
1202 return false;
1203 }
1204 string[] vals = ParseHelper.SplitStringVector(s);
1205 if (vals.Length != Count)
1206 {
1207 result = Zero;
1208 return false;
1209 }
1210 if (Scalar.TryParse(vals[0], out result.X) &&
1211 Scalar.TryParse(vals[1], out result.Y))
1212 {
1213 return true;
1214 }
1215 else
1216 {
1217 result = Zero;
1218 return false;
1219 }
1220 }
1221#endif
1222 [ParseMethod]
1223 public static Vector2D Parse(string s)
1224 {
1225 if (s == null)
1226 {
1227 throw new ArgumentNullException("s");
1228 }
1229 string[] vals = ParseHelper.SplitStringVector(s);
1230 if (vals.Length != Count)
1231 {
1233 }
1234 Vector2D value;
1235 value.X = Scalar.Parse(vals[0]);
1236 value.Y = Scalar.Parse(vals[1]);
1237 return value;
1238 }
1239
1249 public override int GetHashCode()
1250 {
1251 return X.GetHashCode() ^ Y.GetHashCode();
1252 }
1259 public override bool Equals(object obj)
1260 {
1261 return (obj is Vector2D) && Equals((Vector2D)obj);
1262 }
1263 public bool Equals(Vector2D other)
1264 {
1265 return Equals(ref this, ref other);
1266 }
1267 public static bool Equals(Vector2D left, Vector2D right)
1268 {
1269 return
1270 left.X == right.X &&
1271 left.Y == right.Y;
1272 }
1273 public static bool Equals(ref Vector2D left, ref Vector2D right)
1274 {
1275 return
1276 left.X == right.X &&
1277 left.Y == right.Y;
1278 }
1279 #endregion
1280 }
1281}
System.Single Scalar
Definition: Clamped.cs:29
System.Single Scalar
Definition: Vector2D.cs:29
static Scalar Clamp(Scalar value, Scalar min, Scalar max)
Definition: MathHelper.cs:111
static Scalar Cos(Scalar d)
Definition: MathHelper.cs:302
static Scalar Atan2(Scalar y, Scalar x)
Definition: MathHelper.cs:300
static Scalar Sin(Scalar a)
Definition: MathHelper.cs:312
static Scalar Sqrt(Scalar d)
Definition: MathHelper.cs:314
const Scalar TwoPi
Definition: MathHelper.cs:42
static void HermiteHelper(Scalar amount, out Scalar h1, out Scalar h2, out Scalar h3, out Scalar h4)
Definition: MathHelper.cs:86
static string CreateVectorFormatableString(int Count)
Definition: MatrixHelper.cs:51
static string CreateVectorFormatString(int Count)
Definition: MatrixHelper.cs:47
static string[] SplitStringVector(string s)
Definition: ParseHelper.cs:39
static void ThrowVectorFormatException(string value, int count, string format)
Definition: ThrowHelper.cs:54
static void CheckCopy(Scalar[] array, int index, int count)
Definition: ThrowHelper.cs:35
int Count
Gets a 32-bit integer that represents the total number of elements in all the dimensions of IAdvanceV...
Definition: Interfaces.cs:41
A 2x2 matrix which can represent rotations for 2D vectors.
Definition: Matrix2x2.cs:58
A 2x3 matrix which can represent rotations around axes.
Definition: Matrix2x3.cs:62
A 3x3 matrix which can represent rotations around axes.
Definition: Matrix3x3.cs:62
This is the Vector Class.
Definition: Vector2D.cs:50
static readonly Vector2D Zero
Vector2D(0,0)
Definition: Vector2D.cs:69
static void Project(ref Vector2D left, ref Vector2D right, out Vector2D result)
Definition: Vector2D.cs:679
static void Normalize(ref Vector2D source)
Definition: Vector2D.cs:635
static Vector2D operator+(Vector2D left, Vector2D right)
Adds 2 Vectors2Ds.
Definition: Vector2D.cs:979
static void TransformNormal(ref Matrix3x3 matrix, ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:347
static void Lerp(ref Vector2D left, ref Vector2D right, ref Vector2D amount, out Vector2D result)
Definition: Vector2D.cs:158
static void Clamp(ref Vector2D value, ref Vector2D min, ref Vector2D max, out Vector2D result)
Definition: Vector2D.cs:135
static void CatmullRom(ref Vector2D value1, ref Vector2D value2, ref Vector2D value3, ref Vector2D value4, Scalar amount, out Vector2D result)
Definition: Vector2D.cs:746
override string ToString()
Definition: Vector2D.cs:1191
static Vector2D Add(Vector2D left, Vector2D right)
Adds 2 Vectors2Ds.
Definition: Vector2D.cs:287
static Vector2D Negate(Vector2D source)
Negates a Vector2D.
Definition: Vector2D.cs:593
static void Max(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
Definition: Vector2D.cs:768
int IAdvanceValueType. Count
The Number of Variables accesable though the indexer.
Definition: Vector2D.cs:945
static Vector2D Hermite(Vector2D value1, Vector2D tangent1, Vector2D value2, Vector2D tangent2, Scalar amount)
Definition: Vector2D.cs:726
static Vector2D Normalize(Vector2D source, out Scalar magnitude)
This returns the Normalized Vector2D that is passed. This is also known as a Unit Vector.
Definition: Vector2D.cs:646
static Vector2D GetRightHandNormal(Vector2D source)
Gets a Vector2D that is perpendicular(orthogonal) to the passed Vector2D while staying on the XY Plan...
Definition: Vector2D.cs:693
static Vector2D Multiply(Vector2D source, Scalar scalar)
Does Scaler Multiplication on a Vector2D.
Definition: Vector2D.cs:413
static void Copy(ref Vector2D vector, Scalar[] destArray)
Definition: Vector2D.cs:88
const int Size
The Size of the class in bytes;
Definition: Vector2D.cs:59
static void ZCross(ref Scalar leftZ, ref Vector2D right, out Vector2D result)
Definition: Vector2D.cs:504
static Scalar Distance(Vector2D left, Vector2D right)
Definition: Vector2D.cs:164
Scalar Magnitude
Gets or Sets the Magnitude (Length) of the Vector2D.
Definition: Vector2D.cs:890
Scalar X
This is the X value. (Usually represents a horizontal position or direction.)
Definition: Vector2D.cs:796
static Vector2D Transform(Matrix3x3 matrix, Vector2D source)
Uses a matrix multiplication to Transform the vector.
Definition: Vector2D.cs:325
void CopyTo(Scalar[] array, int index)
Definition: Vector2D.cs:958
Scalar[] ToArray()
Definition: Vector2D.cs:948
static void SetMagnitude(ref Vector2D source, ref Scalar magnitude, out Vector2D result)
Definition: Vector2D.cs:579
string ToStringInternal(string FormatString)
Definition: Vector2D.cs:1178
static Vector2D Multiply(Scalar scalar, Vector2D source)
Definition: Vector2D.cs:426
static void SetAngle(ref Vector2D source, ref Scalar radianAngle, out Vector2D result)
Definition: Vector2D.cs:257
static Scalar Dot(Vector2D left, Vector2D right)
Does a Dot Operation Also know as an Inner Product.
Definition: Vector2D.cs:445
static Vector2D Transform(Matrix2x3 matrix, Vector2D source)
Uses a matrix multiplication to Transform the vector.
Definition: Vector2D.cs:360
static Vector2D ZCross(Vector2D left, Scalar rightZ)
Does a "2D" Cross Product also know as an Outer Product.
Definition: Vector2D.cs:528
static Vector2D Clamp(Vector2D value, Vector2D min, Vector2D max)
Binds a value to
Definition: Vector2D.cs:128
static void Lerp(ref Vector2D left, ref Vector2D right, ref Scalar amount, out Vector2D result)
Definition: Vector2D.cs:147
const int Count
The number of Scalar values in the class.
Definition: Vector2D.cs:55
static Scalar operator^(Vector2D left, Vector2D right)
Does a "2D" Cross Product also know as an Outer Product.
Definition: Vector2D.cs:1091
static readonly string FormatableString
Definition: Vector2D.cs:85
string ToString(string format)
Converts the numeric value of this instance to its equivalent string representation,...
Definition: Vector2D.cs:1187
static Vector2D Normalize(Vector2D source)
This returns the Normalized Vector2D that is passed. This is also known as a Unit Vector.
Definition: Vector2D.cs:615
void CopyFrom(Scalar[] array, int index)
Definition: Vector2D.cs:954
bool Equals(Vector2D other)
Definition: Vector2D.cs:1263
static readonly Vector2D XAxis
Vector2D(1,0)
Definition: Vector2D.cs:73
static Vector2D operator-(Vector2D left, Vector2D right)
Subtracts 2 Vector2Ds.
Definition: Vector2D.cs:993
static Vector2D Project(Vector2D left, Vector2D right)
Thie Projects the left Vector2D onto the Right Vector2D.
Definition: Vector2D.cs:673
Vector2D Normalized
Gets the Normalized Vector2D. (Unit Vector)
Definition: Vector2D.cs:936
Vector2D(Scalar[] vals)
Definition: Vector2D.cs:819
static Vector2D FromLengthAndAngle(Scalar length, Scalar radianAngle)
Creates a Vector2D With the given length (Magnitude) and the given Angle.
Definition: Vector2D.cs:206
static Vector2D SetAngle(Vector2D source, Scalar radianAngle)
Sets the Angle of a Vector2D without changing the Magnitude.
Definition: Vector2D.cs:248
static void FromLengthAndAngle(ref Scalar length, ref Scalar radianAngle, out Vector2D result)
Definition: Vector2D.cs:213
static readonly string FormatString
Definition: Vector2D.cs:84
static void Copy(Scalar[] sourceArray, int index, out Vector2D result)
Definition: Vector2D.cs:103
static void Subtract(ref Vector2D left, ref Vector2D right, out Vector2D result)
Definition: Vector2D.cs:313
static void Copy(Scalar[] sourceArray, out Vector2D result)
Definition: Vector2D.cs:99
static Vector2D TransformNormal(Matrix3x3 matrix, Vector2D source)
Definition: Vector2D.cs:340
Vector2D RightHandNormal
Gets A perpendicular(orthogonal) Vector2D using the Right Hand Rule.
Definition: Vector2D.cs:867
static Scalar DistanceSq(Vector2D left, Vector2D right)
Definition: Vector2D.cs:178
static Vector2D Lerp(Vector2D left, Vector2D right, Vector2D amount)
Definition: Vector2D.cs:152
static bool Equals(ref Vector2D left, ref Vector2D right)
Definition: Vector2D.cs:1273
static void Distance(ref Vector2D left, ref Vector2D right, out Scalar result)
Definition: Vector2D.cs:171
Scalar MagnitudeSq
Gets the Squared Magnitude of the Vector2D.
Definition: Vector2D.cs:904
static void Transform(ref Matrix3x3 matrix, ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:333
static bool Equals(Vector2D left, Vector2D right)
Definition: Vector2D.cs:1267
static Vector2D GetLeftHandNormal(Vector2D source)
Gets a Vector2D that is perpendicular(orthogonal) to the passed Vector2D while staying on the XY Plan...
Definition: Vector2D.cs:712
override int GetHashCode()
Provides a unique hash code based on the member variables of this class. This should be done because ...
Definition: Vector2D.cs:1249
static Vector2D Subtract(Vector2D left, Vector2D right)
Subtracts 2 Vector2Ds.
Definition: Vector2D.cs:306
static void GetMagnitude(ref Vector2D source, out Scalar result)
Definition: Vector2D.cs:563
static void Rotate(ref Scalar radianAngle, ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:234
Scalar Y
This is the Y value. (Usually represents a vertical position or direction.)
Definition: Vector2D.cs:805
override bool Equals(object obj)
Compares this Vector to another object. This should be done because the equality operators (==,...
Definition: Vector2D.cs:1259
static void Min(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
Definition: Vector2D.cs:780
static void GetMagnitudeSq(ref Vector2D source, out Scalar result)
Definition: Vector2D.cs:550
static void Multiply(ref Scalar scalar, ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:433
static Scalar GetAngle(Vector2D source)
Determines the current Angle in radians of the Vector2D and Returns it.
Definition: Vector2D.cs:269
static void Transform(ref Matrix2x2 matrix, ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:400
Scalar Angle
Gets or Sets the Angle in radians of the Vector2D.
Definition: Vector2D.cs:919
static Scalar GetMagnitudeSq(Vector2D source)
Gets the Squared Magnitude of the Vector2D that is passed.
Definition: Vector2D.cs:546
static Vector2D ZCross(Scalar leftZ, Vector2D right)
Does a "2D" Cross Product also know as an Outer Product.
Definition: Vector2D.cs:497
static bool TryParse(string s, out Vector2D result)
Definition: Vector2D.cs:1197
static Vector2D Transform(Matrix2x2 matrix, Vector2D source)
Uses a matrix multiplication to Transform the vector.
Definition: Vector2D.cs:393
static Vector2D TransformNormal(Matrix2x3 matrix, Vector2D source)
Definition: Vector2D.cs:373
static Vector2D CatmullRom(Vector2D value1, Vector2D value2, Vector2D value3, Vector2D value4, Scalar amount)
Definition: Vector2D.cs:740
static void Copy(ref Vector2D vector, Scalar[] destArray, int index)
Definition: Vector2D.cs:92
static Vector2D operator*(Vector2D source, Scalar scalar)
Does Scaler Multiplication on a Vector2D.
Definition: Vector2D.cs:1007
Vector2D(Scalar[] vals, int index)
Definition: Vector2D.cs:820
Vector3D ToVector3D(Scalar Z)
Definition: Vector2D.cs:962
static void GetRightHandNormal(ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:700
static void Add(ref Vector2D left, ref Vector2D right, out Vector2D result)
Definition: Vector2D.cs:294
static void TransformNormal(ref Matrix2x3 matrix, ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:380
static void Transform(ref Matrix2x3 matrix, ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:367
static readonly Vector2D One
Vector2D(1,1)
Definition: Vector2D.cs:65
static readonly Vector2D XYAxis
Vector2D(0.707...,0.707...)
Definition: Vector2D.cs:81
static Vector2D Max(Vector2D value1, Vector2D value2)
Definition: Vector2D.cs:762
static Vector2D Min(Vector2D value1, Vector2D value2)
Definition: Vector2D.cs:774
static void Negate(ref Vector2D source)
Definition: Vector2D.cs:600
static void ZCross(ref Vector2D left, ref Scalar rightZ, out Vector2D result)
Definition: Vector2D.cs:535
static void Hermite(ref Vector2D value1, ref Vector2D tangent1, ref Vector2D value2, ref Vector2D tangent2, Scalar amount, out Vector2D result)
Definition: Vector2D.cs:732
Vector2D LeftHandNormal
Gets A perpendicular(orthogonal) Vector2D using the Left Hand Rule.
Definition: Vector2D.cs:878
static Vector2D Parse(string s)
Definition: Vector2D.cs:1223
static void Copy(ref Vector4D source, out Vector2D dest)
Definition: Vector2D.cs:110
static Scalar GetMagnitude(Vector2D source)
Gets the Magnitude of the Vector2D that is passed.
Definition: Vector2D.cs:559
static Vector2D Rotate(Scalar radianAngle, Vector2D source)
Rotates a Vector2D.
Definition: Vector2D.cs:224
static readonly Vector2D YAxis
Vector2D(0,1)
Definition: Vector2D.cs:77
static void Multiply(ref Vector2D source, ref Scalar scalar, out Vector2D result)
Definition: Vector2D.cs:420
static void GetAngle(ref Vector2D source, out Scalar result)
Definition: Vector2D.cs:275
static bool operator!=(Vector2D left, Vector2D right)
Specifies whether the Vector2Ds do not contain the same coordinates.
Definition: Vector2D.cs:1161
static Scalar ZCross(Vector2D left, Vector2D right)
Does a "2D" Cross Product also know as an Outer Product.
Definition: Vector2D.cs:471
static bool operator==(Vector2D left, Vector2D right)
Specifies whether the Vector2Ds contain the same coordinates.
Definition: Vector2D.cs:1151
static void GetLeftHandNormal(ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:719
static void Negate(ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:604
static void Normalize(ref Vector2D source, out Vector2D result)
Definition: Vector2D.cs:626
static void DistanceSq(ref Vector2D left, ref Vector2D right, out Scalar result)
Definition: Vector2D.cs:185
static void Normalize(ref Vector2D source, out Scalar magnitude, out Vector2D result)
Definition: Vector2D.cs:652
static Vector2D SetMagnitude(Vector2D source, Scalar magnitude)
Sets the Magnitude of a Vector2D without changing the Angle.
Definition: Vector2D.cs:573
static void ZCross(ref Vector2D left, ref Vector2D right, out Scalar result)
Definition: Vector2D.cs:475
Vector2D(Scalar X, Scalar Y)
Creates a New Vector2D Instance on the Stack.
Definition: Vector2D.cs:814
static void Dot(ref Vector2D left, ref Vector2D right, out Scalar result)
Definition: Vector2D.cs:449
static void Copy(ref Vector3D source, out Vector2D dest)
Definition: Vector2D.cs:115
static Vector2D Lerp(Vector2D left, Vector2D right, Scalar amount)
Definition: Vector2D.cs:141
A Vector with 3 dimensions.
Definition: Vector3D.cs:47
Scalar X
This is the X value.
Definition: Vector3D.cs:692
Scalar Y
This is the Y value.
Definition: Vector3D.cs:701
Scalar Z
This is the Z value.
Definition: Vector3D.cs:710
A Vector with 4 dimensions.
Definition: Vector4D.cs:48