Vector2.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. [StructLayout(LayoutKind.Sequential)]
  6. public struct Vector2
  7. {
  8. public float x;
  9. public float y;
  10. public static readonly Vector2 zero = new Vector2(0.0f, 0.0f);
  11. public static readonly Vector2 one = new Vector2(1.0f, 1.0f);
  12. public static readonly Vector2 xAxis = new Vector2(1.0f, 0.0f);
  13. public static readonly Vector2 yAxis = new Vector2(0.0f, 1.0f);
  14. public float this[int index]
  15. {
  16. get
  17. {
  18. switch (index)
  19. {
  20. case 0:
  21. return x;
  22. case 1:
  23. return y;
  24. default:
  25. throw new IndexOutOfRangeException("Invalid Vector2 index.");
  26. }
  27. }
  28. set
  29. {
  30. switch (index)
  31. {
  32. case 0:
  33. x = value;
  34. break;
  35. case 1:
  36. y = value;
  37. break;
  38. default:
  39. throw new IndexOutOfRangeException("Invalid Vector2 index.");
  40. }
  41. }
  42. }
  43. public Vector2 normalized
  44. {
  45. get
  46. {
  47. return Normalize(this);
  48. }
  49. }
  50. public float magnitude
  51. {
  52. get
  53. {
  54. return MathEx.Sqrt(x * x + y * y);
  55. }
  56. }
  57. public float sqrdMagnitude
  58. {
  59. get
  60. {
  61. return (x * x + y * y);
  62. }
  63. }
  64. public Vector2(float x, float y)
  65. {
  66. this.x = x;
  67. this.y = y;
  68. }
  69. public static Vector2 operator+ (Vector2 a, Vector2 b)
  70. {
  71. return new Vector2(a.x + b.x, a.y + b.y);
  72. }
  73. public static Vector2 operator- (Vector2 a, Vector2 b)
  74. {
  75. return new Vector2(a.x - b.x, a.y - b.y);
  76. }
  77. public static Vector2 operator- (Vector2 v)
  78. {
  79. return new Vector2(-v.x, -v.y);
  80. }
  81. public static Vector2 operator* (Vector2 v, float d)
  82. {
  83. return new Vector2(v.x * d, v.y * d);
  84. }
  85. public static Vector2 operator* (float d, Vector2 v)
  86. {
  87. return new Vector2(v.x * d, v.y * d);
  88. }
  89. public static Vector2 operator/ (Vector2 v, float d)
  90. {
  91. return new Vector2(v.x / d, v.y / d);
  92. }
  93. public static bool operator== (Vector2 lhs, Vector2 rhs)
  94. {
  95. return lhs.x == rhs.x && lhs.y == rhs.y;
  96. }
  97. public static bool operator!= (Vector2 lhs, Vector2 rhs)
  98. {
  99. return !(lhs == rhs);
  100. }
  101. public static Vector2 Scale(Vector2 a, Vector2 b)
  102. {
  103. return new Vector2(a.x * b.x, a.y * b.y);
  104. }
  105. public static Vector2 Normalize(Vector2 value)
  106. {
  107. float num = Magnitude(value);
  108. if (num > 9.999999E-06f)
  109. return value / num;
  110. return zero;
  111. }
  112. public static float Dot(Vector2 lhs, Vector2 rhs)
  113. {
  114. return lhs.x * rhs.x + lhs.y * rhs.y;
  115. }
  116. public static float Distance(Vector2 a, Vector2 b)
  117. {
  118. Vector2 vector2 = new Vector2(a.x - b.x, a.y - b.y);
  119. return MathEx.Sqrt(vector2.x * vector2.x + vector2.y * vector2.y);
  120. }
  121. public static float Magnitude(Vector2 v)
  122. {
  123. return MathEx.Sqrt(v.x * v.x + v.y * v.y);
  124. }
  125. public static float SqrMagnitude(Vector2 v)
  126. {
  127. return (v.x * v.x + v.y * v.y);
  128. }
  129. public void Scale(Vector2 scale)
  130. {
  131. x *= scale.x;
  132. y *= scale.y;
  133. }
  134. public void Normalize()
  135. {
  136. float num = Magnitude(this);
  137. if (num > 9.999999E-06f)
  138. this /= num;
  139. else
  140. this = zero;
  141. }
  142. public override int GetHashCode()
  143. {
  144. return x.GetHashCode() ^ y.GetHashCode() << 2;
  145. }
  146. public override bool Equals(object other)
  147. {
  148. if (!(other is Vector2))
  149. return false;
  150. Vector2 vec = (Vector2)other;
  151. if (x.Equals(vec.x) && y.Equals(vec.y))
  152. return true;
  153. return false;
  154. }
  155. }
  156. }