Vector2I.cs 4.4 KB

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