Vector2.cs 8.7 KB

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