Vector2I.cs 4.2 KB

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