Vector2.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// A two dimensional vector.
  7. /// </summary>
  8. [StructLayout(LayoutKind.Sequential), SerializeObject]
  9. public struct Vector2 // Note: Must match C++ struct Vector2
  10. {
  11. public float x;
  12. public float y;
  13. public static readonly Vector2 Zero = new Vector2(0.0f, 0.0f);
  14. public static readonly Vector2 One = new Vector2(1.0f, 1.0f);
  15. public static readonly Vector2 XAxis = new Vector2(1.0f, 0.0f);
  16. public static readonly Vector2 YAxis = new Vector2(0.0f, 1.0f);
  17. /// <summary>
  18. /// Accesses a specific component of the vector.
  19. /// </summary>
  20. /// <param name="index">Index of the component.</param>
  21. /// <returns>Value of the specific component.</returns>
  22. public float this[int index]
  23. {
  24. get
  25. {
  26. switch (index)
  27. {
  28. case 0:
  29. return x;
  30. case 1:
  31. return y;
  32. default:
  33. throw new IndexOutOfRangeException("Invalid Vector2 index.");
  34. }
  35. }
  36. set
  37. {
  38. switch (index)
  39. {
  40. case 0:
  41. x = value;
  42. break;
  43. case 1:
  44. y = value;
  45. break;
  46. default:
  47. throw new IndexOutOfRangeException("Invalid Vector2 index.");
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// Returns a normalized copy of the vector.
  53. /// </summary>
  54. public Vector2 Normalized
  55. {
  56. get
  57. {
  58. return Normalize(this);
  59. }
  60. }
  61. /// <summary>
  62. /// Returns the length of the vector.
  63. /// </summary>
  64. public float Length
  65. {
  66. get
  67. {
  68. return MathEx.Sqrt(x * x + y * y);
  69. }
  70. }
  71. /// <summary>
  72. /// Returns the squared length of the vector.
  73. /// </summary>
  74. public float SqrdLength
  75. {
  76. get
  77. {
  78. return (x * x + y * y);
  79. }
  80. }
  81. /// <summary>
  82. /// Creates a new two dimensional vector.
  83. /// </summary>
  84. /// <param name="x">X coordinate.</param>
  85. /// <param name="y">Y coordinate.</param>
  86. public Vector2(float x, float y)
  87. {
  88. this.x = x;
  89. this.y = y;
  90. }
  91. public static Vector2 operator+ (Vector2 a, Vector2 b)
  92. {
  93. return new Vector2(a.x + b.x, a.y + b.y);
  94. }
  95. public static Vector2 operator- (Vector2 a, Vector2 b)
  96. {
  97. return new Vector2(a.x - b.x, a.y - b.y);
  98. }
  99. public static Vector2 operator- (Vector2 v)
  100. {
  101. return new Vector2(-v.x, -v.y);
  102. }
  103. public static Vector2 operator* (Vector2 v, float d)
  104. {
  105. return new Vector2(v.x * d, v.y * d);
  106. }
  107. public static Vector2 operator* (float d, Vector2 v)
  108. {
  109. return new Vector2(v.x * d, v.y * d);
  110. }
  111. public static Vector2 operator/ (Vector2 v, float d)
  112. {
  113. return new Vector2(v.x / d, v.y / d);
  114. }
  115. public static bool operator== (Vector2 lhs, Vector2 rhs)
  116. {
  117. return lhs.x == rhs.x && lhs.y == rhs.y;
  118. }
  119. public static bool operator!= (Vector2 lhs, Vector2 rhs)
  120. {
  121. return !(lhs == rhs);
  122. }
  123. /// <summary>
  124. /// Scales one vector by another.
  125. /// </summary>
  126. /// <param name="a">First two dimensional vector.</param>
  127. /// <param name="b">Second two dimensional vector.</param>
  128. /// <returns>One vector scaled by another.</returns>
  129. public static Vector2 Scale(Vector2 a, Vector2 b)
  130. {
  131. return new Vector2(a.x * b.x, a.y * b.y);
  132. }
  133. /// <summary>
  134. /// Normalizes the provided vector and returns the normalized copy.
  135. /// </summary>
  136. /// <param name="value">Vector to normalize.</param>
  137. /// <returns>Normalized copy of the vector.</returns>
  138. public static Vector2 Normalize(Vector2 value)
  139. {
  140. float num = Magnitude(value);
  141. if (num > 9.999999E-06f)
  142. return value / num;
  143. return Zero;
  144. }
  145. /// <summary>
  146. /// Calculates the inner product of the two vectors.
  147. /// </summary>
  148. /// <param name="lhs">First two dimensional vector.</param>
  149. /// <param name="rhs">Second two dimensional vector.</param>
  150. /// <returns>Inner product between the two vectors.</returns>
  151. public static float Dot(Vector2 lhs, Vector2 rhs)
  152. {
  153. return lhs.x * rhs.x + lhs.y * rhs.y;
  154. }
  155. /// <summary>
  156. /// Calculates the distance between two points.
  157. /// </summary>
  158. /// <param name="a">First two dimensional point.</param>
  159. /// <param name="b">Second two dimensional point.</param>
  160. /// <returns>Distance between the two points.</returns>
  161. public static float Distance(Vector2 a, Vector2 b)
  162. {
  163. Vector2 vector2 = new Vector2(a.x - b.x, a.y - b.y);
  164. return MathEx.Sqrt(vector2.x * vector2.x + vector2.y * vector2.y);
  165. }
  166. /// <summary>
  167. /// Calculates the magnitude of the provided vector.
  168. /// </summary>
  169. /// <param name="v">Vector to calculate the magnitude for.</param>
  170. /// <returns>Magnitude of the vector.</returns>
  171. public static float Magnitude(Vector2 v)
  172. {
  173. return MathEx.Sqrt(v.x * v.x + v.y * v.y);
  174. }
  175. /// <summary>
  176. /// Calculates the squared magnitude of the provided vector.
  177. /// </summary>
  178. /// <param name="v">Vector to calculate the magnitude for.</param>
  179. /// <returns>Squared magnitude of the vector.</returns>
  180. public static float SqrMagnitude(Vector2 v)
  181. {
  182. return (v.x * v.x + v.y * v.y);
  183. }
  184. /// <summary>
  185. /// Scales the components of the vector by specified scale factors.
  186. /// </summary>
  187. /// <param name="scale">Scale factors to multiply components by.</param>
  188. public void Scale(Vector2 scale)
  189. {
  190. x *= scale.x;
  191. y *= scale.y;
  192. }
  193. /// <summary>
  194. /// Normalizes the vector.
  195. /// </summary>
  196. public void Normalize()
  197. {
  198. float num = Magnitude(this);
  199. if (num > 9.999999E-06f)
  200. this /= num;
  201. else
  202. this = Zero;
  203. }
  204. /// <inheritdoc/>
  205. public override int GetHashCode()
  206. {
  207. return x.GetHashCode() ^ y.GetHashCode() << 2;
  208. }
  209. /// <inheritdoc/>
  210. public override bool Equals(object other)
  211. {
  212. if (!(other is Vector2))
  213. return false;
  214. Vector2 vec = (Vector2)other;
  215. if (x.Equals(vec.x) && y.Equals(vec.y))
  216. return true;
  217. return false;
  218. }
  219. /// <inheritdoc/>
  220. public override string ToString()
  221. {
  222. return "(" + x + ", " + y + ")";
  223. }
  224. }
  225. }