Vector2I.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 with integer coordinates.
  12. /// </summary>
  13. [StructLayout(LayoutKind.Sequential)]
  14. public struct Vector2I // Note: Must match C++ class Vector2I
  15. {
  16. public int x;
  17. public int y;
  18. /// <summary>
  19. /// Accesses a specific component of the vector.
  20. /// </summary>
  21. /// <param name="index">Index of the component.</param>
  22. /// <returns>Value of the specific component.</returns>
  23. public int this[int index]
  24. {
  25. get
  26. {
  27. switch (index)
  28. {
  29. case 0:
  30. return x;
  31. case 1:
  32. return y;
  33. default:
  34. throw new IndexOutOfRangeException("Invalid Vector2I index.");
  35. }
  36. }
  37. set
  38. {
  39. switch (index)
  40. {
  41. case 0:
  42. x = value;
  43. break;
  44. case 1:
  45. y = value;
  46. break;
  47. default:
  48. throw new IndexOutOfRangeException("Invalid Vector2I index.");
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// Creates a new two dimensional vector.
  54. /// </summary>
  55. /// <param name="x">X coordinate.</param>
  56. /// <param name="y">Y coordinate.</param>
  57. public Vector2I(int x, int y)
  58. {
  59. this.x = x;
  60. this.y = y;
  61. }
  62. /// <summary>
  63. /// Returns length of the vector.
  64. /// </summary>
  65. public float Length
  66. {
  67. get
  68. {
  69. return (float)MathEx.Sqrt(x * x + y * y);
  70. }
  71. }
  72. /// <summary>
  73. /// Returns the squared length of the vector.
  74. /// </summary>
  75. public float SqrdLength
  76. {
  77. get
  78. {
  79. return (x * x + y * y);
  80. }
  81. }
  82. /// <summary>
  83. /// Returns the manhattan distance between two points.
  84. /// </summary>
  85. /// <param name="a">First two dimensional point.</param>
  86. /// <param name="b">Second two dimensional point.</param>
  87. /// <returns>Manhattan distance between the two points.</returns>
  88. public static int Distance(Vector2I a, Vector2I b)
  89. {
  90. return Math.Abs(b.x - a.x) + Math.Abs(b.y - a.y);
  91. }
  92. public static Vector2I operator +(Vector2I a, Vector2I b)
  93. {
  94. return new Vector2I(a.x + b.x, a.y + b.y);
  95. }
  96. public static Vector2I operator -(Vector2I a, Vector2I b)
  97. {
  98. return new Vector2I(a.x - b.x, a.y - b.y);
  99. }
  100. public static Vector2I operator -(Vector2I v)
  101. {
  102. return new Vector2I(-v.x, -v.y);
  103. }
  104. public static Vector2I operator *(Vector2I v, int d)
  105. {
  106. return new Vector2I(v.x * d, v.y * d);
  107. }
  108. public static Vector2I operator *(int d, Vector2I v)
  109. {
  110. return new Vector2I(v.x * d, v.y * d);
  111. }
  112. public static Vector2I operator /(Vector2I v, int d)
  113. {
  114. return new Vector2I(v.x / d, v.y / d);
  115. }
  116. public static bool operator ==(Vector2I lhs, Vector2I rhs)
  117. {
  118. return lhs.x == rhs.x && lhs.y == rhs.y;
  119. }
  120. public static bool operator !=(Vector2I lhs, Vector2I rhs)
  121. {
  122. return !(lhs == rhs);
  123. }
  124. /// <inheritdoc/>
  125. public override int GetHashCode()
  126. {
  127. return x.GetHashCode() ^ y.GetHashCode() << 2;
  128. }
  129. /// <inheritdoc/>
  130. public override bool Equals(object other)
  131. {
  132. if (!(other is Vector2I))
  133. return false;
  134. Vector2I vec = (Vector2I)other;
  135. if (x.Equals(vec.x) && y.Equals(vec.y))
  136. return true;
  137. return false;
  138. }
  139. /// <inheritdoc/>
  140. public override string ToString()
  141. {
  142. return "(" + x + ", " + y + ")";
  143. }
  144. }
  145. /** @} */
  146. }