Vector2.cs 7.7 KB

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