00001 #region --- License ---
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #endregion
00024
00025 using System;
00026 using System.Runtime.InteropServices;
00027
00028 namespace OpenTK
00029 {
00031 [Serializable]
00032 [StructLayout(LayoutKind.Sequential)]
00033 public struct Vector2d : IEquatable<Vector2d>
00034 {
00035 #region Fields
00036
00038 public double X;
00039
00041 public double Y;
00042
00046 public static Vector2d UnitX = new Vector2d(1, 0);
00047
00051 public static Vector2d UnitY = new Vector2d(0, 1);
00052
00056 public static Vector2d Zero = new Vector2d(0, 0);
00057
00061 public static readonly Vector2d One = new Vector2d(1, 1);
00062
00066 public static readonly int SizeInBytes = Marshal.SizeOf(new Vector2d());
00067
00068 #endregion
00069
00070 #region Constructors
00071
00075 public Vector2d(double x, double y)
00076 {
00077 this.X = x;
00078 this.Y = y;
00079 }
00080
00081 #endregion
00082
00083 #region Public Members
00084
00085 #region Instance
00086
00087 #region public void Add()
00088
00091 [Obsolete("Use static Add() method instead.")]
00092 public void Add(Vector2d right)
00093 {
00094 this.X += right.X;
00095 this.Y += right.Y;
00096 }
00097
00100 [CLSCompliant(false)]
00101 [Obsolete("Use static Add() method instead.")]
00102 public void Add(ref Vector2d right)
00103 {
00104 this.X += right.X;
00105 this.Y += right.Y;
00106 }
00107
00108 #endregion public void Add()
00109
00110 #region public void Sub()
00111
00114 [Obsolete("Use static Subtract() method instead.")]
00115 public void Sub(Vector2d right)
00116 {
00117 this.X -= right.X;
00118 this.Y -= right.Y;
00119 }
00120
00123 [CLSCompliant(false)]
00124 [Obsolete("Use static Subtract() method instead.")]
00125 public void Sub(ref Vector2d right)
00126 {
00127 this.X -= right.X;
00128 this.Y -= right.Y;
00129 }
00130
00131 #endregion public void Sub()
00132
00133 #region public void Mult()
00134
00137 [Obsolete("Use static Multiply() method instead.")]
00138 public void Mult(double f)
00139 {
00140 this.X *= f;
00141 this.Y *= f;
00142 }
00143
00144 #endregion public void Mult()
00145
00146 #region public void Div()
00147
00150 [Obsolete("Use static Divide() method instead.")]
00151 public void Div(double f)
00152 {
00153 double mult = 1.0 / f;
00154 this.X *= mult;
00155 this.Y *= mult;
00156 }
00157
00158 #endregion public void Div()
00159
00160 #region public double Length
00161
00166 public double Length
00167 {
00168 get
00169 {
00170 return System.Math.Sqrt(X * X + Y * Y);
00171 }
00172 }
00173
00174 #endregion
00175
00176 #region public double LengthSquared
00177
00186 public double LengthSquared
00187 {
00188 get
00189 {
00190 return X * X + Y * Y;
00191 }
00192 }
00193
00194 #endregion
00195
00196 #region public Vector2d PerpendicularRight
00197
00201 public Vector2d PerpendicularRight
00202 {
00203 get
00204 {
00205 return new Vector2d(Y, -X);
00206 }
00207 }
00208
00209 #endregion
00210
00211 #region public Vector2d PerpendicularLeft
00212
00216 public Vector2d PerpendicularLeft
00217 {
00218 get
00219 {
00220 return new Vector2d(-Y, X);
00221 }
00222 }
00223
00224 #endregion
00225
00226 #region public void Normalize()
00227
00231 public void Normalize()
00232 {
00233 double scale = 1.0 / Length;
00234 X *= scale;
00235 Y *= scale;
00236 }
00237
00238 #endregion
00239
00240 #region public void Scale()
00241
00247 [Obsolete("Use static Multiply() method instead.")]
00248 public void Scale(double sx, double sy)
00249 {
00250 X *= sx;
00251 Y *= sy;
00252 }
00253
00256 [Obsolete("Use static Multiply() method instead.")]
00257 public void Scale(Vector2d scale)
00258 {
00259 this.X *= scale.X;
00260 this.Y *= scale.Y;
00261 }
00262
00265 [CLSCompliant(false)]
00266 [Obsolete("Use static Multiply() method instead.")]
00267 public void Scale(ref Vector2d scale)
00268 {
00269 this.X *= scale.X;
00270 this.Y *= scale.Y;
00271 }
00272
00273 #endregion public void Scale()
00274
00275 #endregion
00276
00277 #region Static
00278
00279 #region Obsolete
00280
00281 #region Sub
00282
00289 [Obsolete("Use static Subtract() method instead.")]
00290 public static Vector2d Sub(Vector2d a, Vector2d b)
00291 {
00292 a.X -= b.X;
00293 a.Y -= b.Y;
00294 return a;
00295 }
00296
00303 [Obsolete("Use static Subtract() method instead.")]
00304 public static void Sub(ref Vector2d a, ref Vector2d b, out Vector2d result)
00305 {
00306 result.X = a.X - b.X;
00307 result.Y = a.Y - b.Y;
00308 }
00309
00310 #endregion
00311
00312 #region Mult
00313
00320 [Obsolete("Use static Multiply() method instead.")]
00321 public static Vector2d Mult(Vector2d a, double d)
00322 {
00323 a.X *= d;
00324 a.Y *= d;
00325 return a;
00326 }
00327
00334 [Obsolete("Use static Multiply() method instead.")]
00335 public static void Mult(ref Vector2d a, double d, out Vector2d result)
00336 {
00337 result.X = a.X * d;
00338 result.Y = a.Y * d;
00339 }
00340
00341 #endregion
00342
00343 #region Div
00344
00351 [Obsolete("Use static Divide() method instead.")]
00352 public static Vector2d Div(Vector2d a, double d)
00353 {
00354 double mult = 1.0 / d;
00355 a.X *= mult;
00356 a.Y *= mult;
00357 return a;
00358 }
00359
00366 [Obsolete("Use static Divide() method instead.")]
00367 public static void Div(ref Vector2d a, double d, out Vector2d result)
00368 {
00369 double mult = 1.0 / d;
00370 result.X = a.X * mult;
00371 result.Y = a.Y * mult;
00372 }
00373
00374 #endregion
00375
00376 #endregion
00377
00378 #region Add
00379
00386 public static Vector2d Add(Vector2d a, Vector2d b)
00387 {
00388 Add(ref a, ref b, out a);
00389 return a;
00390 }
00391
00398 public static void Add(ref Vector2d a, ref Vector2d b, out Vector2d result)
00399 {
00400 result = new Vector2d(a.X + b.X, a.Y + b.Y);
00401 }
00402
00403 #endregion
00404
00405 #region Subtract
00406
00413 public static Vector2d Subtract(Vector2d a, Vector2d b)
00414 {
00415 Subtract(ref a, ref b, out a);
00416 return a;
00417 }
00418
00425 public static void Subtract(ref Vector2d a, ref Vector2d b, out Vector2d result)
00426 {
00427 result = new Vector2d(a.X - b.X, a.Y - b.Y);
00428 }
00429
00430 #endregion
00431
00432 #region Multiply
00433
00440 public static Vector2d Multiply(Vector2d vector, double scale)
00441 {
00442 Multiply(ref vector, scale, out vector);
00443 return vector;
00444 }
00445
00452 public static void Multiply(ref Vector2d vector, double scale, out Vector2d result)
00453 {
00454 result = new Vector2d(vector.X * scale, vector.Y * scale);
00455 }
00456
00463 public static Vector2d Multiply(Vector2d vector, Vector2d scale)
00464 {
00465 Multiply(ref vector, ref scale, out vector);
00466 return vector;
00467 }
00468
00475 public static void Multiply(ref Vector2d vector, ref Vector2d scale, out Vector2d result)
00476 {
00477 result = new Vector2d(vector.X * scale.X, vector.Y * scale.Y);
00478 }
00479
00480 #endregion
00481
00482 #region Divide
00483
00490 public static Vector2d Divide(Vector2d vector, double scale)
00491 {
00492 Divide(ref vector, scale, out vector);
00493 return vector;
00494 }
00495
00502 public static void Divide(ref Vector2d vector, double scale, out Vector2d result)
00503 {
00504 Multiply(ref vector, 1 / scale, out result);
00505 }
00506
00513 public static Vector2d Divide(Vector2d vector, Vector2d scale)
00514 {
00515 Divide(ref vector, ref scale, out vector);
00516 return vector;
00517 }
00518
00525 public static void Divide(ref Vector2d vector, ref Vector2d scale, out Vector2d result)
00526 {
00527 result = new Vector2d(vector.X / scale.X, vector.Y / scale.Y);
00528 }
00529
00530 #endregion
00531
00532 #region Min
00533
00540 public static Vector2d Min(Vector2d a, Vector2d b)
00541 {
00542 a.X = a.X < b.X ? a.X : b.X;
00543 a.Y = a.Y < b.Y ? a.Y : b.Y;
00544 return a;
00545 }
00546
00553 public static void Min(ref Vector2d a, ref Vector2d b, out Vector2d result)
00554 {
00555 result.X = a.X < b.X ? a.X : b.X;
00556 result.Y = a.Y < b.Y ? a.Y : b.Y;
00557 }
00558
00559 #endregion
00560
00561 #region Max
00562
00569 public static Vector2d Max(Vector2d a, Vector2d b)
00570 {
00571 a.X = a.X > b.X ? a.X : b.X;
00572 a.Y = a.Y > b.Y ? a.Y : b.Y;
00573 return a;
00574 }
00575
00582 public static void Max(ref Vector2d a, ref Vector2d b, out Vector2d result)
00583 {
00584 result.X = a.X > b.X ? a.X : b.X;
00585 result.Y = a.Y > b.Y ? a.Y : b.Y;
00586 }
00587
00588 #endregion
00589
00590 #region Clamp
00591
00599 public static Vector2d Clamp(Vector2d vec, Vector2d min, Vector2d max)
00600 {
00601 vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
00602 vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
00603 return vec;
00604 }
00605
00613 public static void Clamp(ref Vector2d vec, ref Vector2d min, ref Vector2d max, out Vector2d result)
00614 {
00615 result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
00616 result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
00617 }
00618
00619 #endregion
00620
00621 #region Normalize
00622
00628 public static Vector2d Normalize(Vector2d vec)
00629 {
00630 double scale = 1.0 / vec.Length;
00631 vec.X *= scale;
00632 vec.Y *= scale;
00633 return vec;
00634 }
00635
00641 public static void Normalize(ref Vector2d vec, out Vector2d result)
00642 {
00643 double scale = 1.0 / vec.Length;
00644 result.X = vec.X * scale;
00645 result.Y = vec.Y * scale;
00646 }
00647
00648 #endregion
00649
00650 #region NormalizeFast
00651
00657 public static Vector2d NormalizeFast(Vector2d vec)
00658 {
00659 double scale = MathHelper.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
00660 vec.X *= scale;
00661 vec.Y *= scale;
00662 return vec;
00663 }
00664
00670 public static void NormalizeFast(ref Vector2d vec, out Vector2d result)
00671 {
00672 double scale = MathHelper.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
00673 result.X = vec.X * scale;
00674 result.Y = vec.Y * scale;
00675 }
00676
00677 #endregion
00678
00679 #region Dot
00680
00687 public static double Dot(Vector2d left, Vector2d right)
00688 {
00689 return left.X * right.X + left.Y * right.Y;
00690 }
00691
00698 public static void Dot(ref Vector2d left, ref Vector2d right, out double result)
00699 {
00700 result = left.X * right.X + left.Y * right.Y;
00701 }
00702
00703 #endregion
00704
00705 #region Lerp
00706
00714 public static Vector2d Lerp(Vector2d a, Vector2d b, double blend)
00715 {
00716 a.X = blend * (b.X - a.X) + a.X;
00717 a.Y = blend * (b.Y - a.Y) + a.Y;
00718 return a;
00719 }
00720
00728 public static void Lerp(ref Vector2d a, ref Vector2d b, double blend, out Vector2d result)
00729 {
00730 result.X = blend * (b.X - a.X) + a.X;
00731 result.Y = blend * (b.Y - a.Y) + a.Y;
00732 }
00733
00734 #endregion
00735
00736 #region Barycentric
00737
00747 public static Vector2d BaryCentric(Vector2d a, Vector2d b, Vector2d c, double u, double v)
00748 {
00749 return a + u * (b - a) + v * (c - a);
00750 }
00751
00759 public static void BaryCentric(ref Vector2d a, ref Vector2d b, ref Vector2d c, double u, double v, out Vector2d result)
00760 {
00761 result = a;
00762
00763 Vector2d temp = b;
00764 Subtract(ref temp, ref a, out temp);
00765 Multiply(ref temp, u, out temp);
00766 Add(ref result, ref temp, out result);
00767
00768 temp = c;
00769 Subtract(ref temp, ref a, out temp);
00770 Multiply(ref temp, v, out temp);
00771 Add(ref result, ref temp, out result);
00772 }
00773
00774 #endregion
00775
00776 #region Transform
00777
00784 public static Vector2d Transform(Vector2d vec, Quaterniond quat)
00785 {
00786 Vector2d result;
00787 Transform(ref vec, ref quat, out result);
00788 return result;
00789 }
00790
00797 public static void Transform(ref Vector2d vec, ref Quaterniond quat, out Vector2d result)
00798 {
00799 Quaterniond v = new Quaterniond(vec.X, vec.Y, 0, 0), i, t;
00800 Quaterniond.Invert(ref quat, out i);
00801 Quaterniond.Multiply(ref quat, ref v, out t);
00802 Quaterniond.Multiply(ref t, ref i, out v);
00803
00804 result = new Vector2d(v.X, v.Y);
00805 }
00806
00807 #endregion
00808
00809 #endregion
00810
00811 #region Operators
00812
00819 public static Vector2d operator +(Vector2d left, Vector2d right)
00820 {
00821 left.X += right.X;
00822 left.Y += right.Y;
00823 return left;
00824 }
00825
00832 public static Vector2d operator -(Vector2d left, Vector2d right)
00833 {
00834 left.X -= right.X;
00835 left.Y -= right.Y;
00836 return left;
00837 }
00838
00844 public static Vector2d operator -(Vector2d vec)
00845 {
00846 vec.X = -vec.X;
00847 vec.Y = -vec.Y;
00848 return vec;
00849 }
00850
00857 public static Vector2d operator *(Vector2d vec, double f)
00858 {
00859 vec.X *= f;
00860 vec.Y *= f;
00861 return vec;
00862 }
00863
00870 public static Vector2d operator *(double f, Vector2d vec)
00871 {
00872 vec.X *= f;
00873 vec.Y *= f;
00874 return vec;
00875 }
00876
00883 public static Vector2d operator /(Vector2d vec, double f)
00884 {
00885 double mult = 1.0 / f;
00886 vec.X *= mult;
00887 vec.Y *= mult;
00888 return vec;
00889 }
00890
00897 public static bool operator ==(Vector2d left, Vector2d right)
00898 {
00899 return left.Equals(right);
00900 }
00901
00908 public static bool operator !=(Vector2d left, Vector2d right)
00909 {
00910 return !left.Equals(right);
00911 }
00912
00916 public static explicit operator Vector2d(Vector2 v2)
00917 {
00918 return new Vector2d(v2.X, v2.Y);
00919 }
00920
00924 public static explicit operator Vector2(Vector2d v2d)
00925 {
00926 return new Vector2((float)v2d.X, (float)v2d.Y);
00927 }
00928
00929 #endregion
00930
00931 #region Overrides
00932
00933 #region public override string ToString()
00934
00939 public override string ToString()
00940 {
00941 return String.Format("({0}, {1})", X, Y);
00942 }
00943
00944 #endregion
00945
00946 #region public override int GetHashCode()
00947
00952 public override int GetHashCode()
00953 {
00954 return X.GetHashCode() ^ Y.GetHashCode();
00955 }
00956
00957 #endregion
00958
00959 #region public override bool Equals(object obj)
00960
00966 public override bool Equals(object obj)
00967 {
00968 if (!(obj is Vector2d))
00969 return false;
00970
00971 return this.Equals((Vector2d)obj);
00972 }
00973
00974 #endregion
00975
00976 #endregion
00977
00978 #endregion
00979
00980 #region IEquatable<Vector2d> Members
00981
00985 public bool Equals(Vector2d other)
00986 {
00987 return
00988 X == other.X &&
00989 Y == other.Y;
00990 }
00991
00992 #endregion
00993 }
00994 }