Vector2.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 v, float d)
  106. {
  107. return new Vector2(v.x * d, v.y * d);
  108. }
  109. public static Vector2 operator* (float d, Vector2 v)
  110. {
  111. return new Vector2(v.x * d, v.y * d);
  112. }
  113. public static Vector2 operator/ (Vector2 v, float d)
  114. {
  115. return new Vector2(v.x / d, v.y / d);
  116. }
  117. public static bool operator== (Vector2 lhs, Vector2 rhs)
  118. {
  119. return lhs.x == rhs.x && lhs.y == rhs.y;
  120. }
  121. public static bool operator!= (Vector2 lhs, Vector2 rhs)
  122. {
  123. return !(lhs == rhs);
  124. }
  125. /// <summary>
  126. /// Scales one vector by another.
  127. /// </summary>
  128. /// <param name="a">First two dimensional vector.</param>
  129. /// <param name="b">Second two dimensional vector.</param>
  130. /// <returns>One vector scaled by another.</returns>
  131. public static Vector2 Scale(Vector2 a, Vector2 b)
  132. {
  133. return new Vector2(a.x * b.x, a.y * b.y);
  134. }
  135. /// <summary>
  136. /// Normalizes the provided vector and returns the normalized copy.
  137. /// </summary>
  138. /// <param name="value">Vector to normalize.</param>
  139. /// <returns>Normalized copy of the vector.</returns>
  140. public static Vector2 Normalize(Vector2 value)
  141. {
  142. float num = Magnitude(value);
  143. if (num > 9.999999E-06f)
  144. return value / num;
  145. return Zero;
  146. }
  147. /// <summary>
  148. /// Calculates the inner product of the two vectors.
  149. /// </summary>
  150. /// <param name="lhs">First two dimensional vector.</param>
  151. /// <param name="rhs">Second two dimensional vector.</param>
  152. /// <returns>Inner product between the two vectors.</returns>
  153. public static float Dot(Vector2 lhs, Vector2 rhs)
  154. {
  155. return lhs.x * rhs.x + lhs.y * rhs.y;
  156. }
  157. /// <summary>
  158. /// Calculates the distance between two points.
  159. /// </summary>
  160. /// <param name="a">First two dimensional point.</param>
  161. /// <param name="b">Second two dimensional point.</param>
  162. /// <returns>Distance between the two points.</returns>
  163. public static float Distance(Vector2 a, Vector2 b)
  164. {
  165. Vector2 vector2 = new Vector2(a.x - b.x, a.y - b.y);
  166. return MathEx.Sqrt(vector2.x * vector2.x + vector2.y * vector2.y);
  167. }
  168. /// <summary>
  169. /// Calculates the magnitude of the provided vector.
  170. /// </summary>
  171. /// <param name="v">Vector to calculate the magnitude for.</param>
  172. /// <returns>Magnitude of the vector.</returns>
  173. public static float Magnitude(Vector2 v)
  174. {
  175. return MathEx.Sqrt(v.x * v.x + v.y * v.y);
  176. }
  177. /// <summary>
  178. /// Calculates the squared magnitude of the provided vector.
  179. /// </summary>
  180. /// <param name="v">Vector to calculate the magnitude for.</param>
  181. /// <returns>Squared magnitude of the vector.</returns>
  182. public static float SqrMagnitude(Vector2 v)
  183. {
  184. return (v.x * v.x + v.y * v.y);
  185. }
  186. /// <summary>
  187. /// Scales the components of the vector by specified scale factors.
  188. /// </summary>
  189. /// <param name="scale">Scale factors to multiply components by.</param>
  190. public void Scale(Vector2 scale)
  191. {
  192. x *= scale.x;
  193. y *= scale.y;
  194. }
  195. /// <summary>
  196. /// Normalizes the vector.
  197. /// </summary>
  198. public void Normalize()
  199. {
  200. float num = Magnitude(this);
  201. if (num > 9.999999E-06f)
  202. this /= num;
  203. else
  204. this = Zero;
  205. }
  206. /// <inheritdoc/>
  207. public override int GetHashCode()
  208. {
  209. return x.GetHashCode() ^ y.GetHashCode() << 2;
  210. }
  211. /// <inheritdoc/>
  212. public override bool Equals(object other)
  213. {
  214. if (!(other is Vector2))
  215. return false;
  216. Vector2 vec = (Vector2)other;
  217. if (x.Equals(vec.x) && y.Equals(vec.y))
  218. return true;
  219. return false;
  220. }
  221. /// <inheritdoc/>
  222. public override string ToString()
  223. {
  224. return "(" + x + ", " + y + ")";
  225. }
  226. }
  227. }