BsVector2I.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. namespace BansheeEngine
  4. {
  5. /** @addtogroup Math
  6. * @{
  7. */
  8. /** A two dimensional vector with integer coordinates.*/
  9. struct BS_UTILITY_EXPORT Vector2I
  10. {
  11. INT32 x;
  12. INT32 y;
  13. Vector2I()
  14. :x(0), y(0)
  15. { }
  16. inline Vector2I(INT32 _x, INT32 _y )
  17. :x(_x), y(_y)
  18. { }
  19. explicit Vector2I(int val)
  20. :x(val), y(val)
  21. { }
  22. /** Exchange the contents of this vector with another. */
  23. void swap(Vector2I& other)
  24. {
  25. std::swap(x, other.x);
  26. std::swap(y, other.y);
  27. }
  28. /** Returns the manhattan distance between this and another point. */
  29. UINT32 manhattanDist(const Vector2I& other) const
  30. {
  31. return (UINT32)fabs(float(other.x - x)) + (UINT32)fabs(float(other.y - y));
  32. }
  33. INT32 operator[] (size_t i) const
  34. {
  35. assert(i < 2);
  36. return *(&x+i);
  37. }
  38. INT32& operator[] (size_t i)
  39. {
  40. assert(i < 2);
  41. return *(&x+i);
  42. }
  43. Vector2I& operator= (const Vector2I& rhs)
  44. {
  45. x = rhs.x;
  46. y = rhs.y;
  47. return *this;
  48. }
  49. Vector2I& operator= (int val)
  50. {
  51. x = val;
  52. y = val;
  53. return *this;
  54. }
  55. bool operator== (const Vector2I& rhs) const
  56. {
  57. return (x == rhs.x && y == rhs.y);
  58. }
  59. bool operator!= (const Vector2I& rhs) const
  60. {
  61. return (x != rhs.x || y != rhs.y);
  62. }
  63. Vector2I operator+ (const Vector2I& rhs) const
  64. {
  65. return Vector2I(x + rhs.x, y + rhs.y);
  66. }
  67. Vector2I operator- (const Vector2I& rhs) const
  68. {
  69. return Vector2I(x - rhs.x, y - rhs.y);
  70. }
  71. Vector2I operator* (int val) const
  72. {
  73. return Vector2I(x * val, y * val);
  74. }
  75. Vector2I operator* (const Vector2I& rhs) const
  76. {
  77. return Vector2I(x * rhs.x, y * rhs.y);
  78. }
  79. Vector2I operator/ (int val) const
  80. {
  81. assert(val != 0);
  82. return Vector2I(x / val, y / val);
  83. }
  84. Vector2I operator/ (const Vector2I& rhs) const
  85. {
  86. return Vector2I(x / rhs.x, y / rhs.y);
  87. }
  88. const Vector2I& operator+ () const
  89. {
  90. return *this;
  91. }
  92. Vector2I operator- () const
  93. {
  94. return Vector2I(-x, -y);
  95. }
  96. friend Vector2I operator* (int lhs, const Vector2I& rhs)
  97. {
  98. return Vector2I(lhs * rhs.x, lhs * rhs.y);
  99. }
  100. friend Vector2I operator/ (int lhs, const Vector2I& rhs)
  101. {
  102. return Vector2I(lhs / rhs.x, lhs / rhs.y);
  103. }
  104. Vector2I& operator+= (const Vector2I& rhs)
  105. {
  106. x += rhs.x;
  107. y += rhs.y;
  108. return *this;
  109. }
  110. Vector2I& operator-= (const Vector2I& rhs)
  111. {
  112. x -= rhs.x;
  113. y -= rhs.y;
  114. return *this;
  115. }
  116. Vector2I& operator*= (INT32 val)
  117. {
  118. x *= val;
  119. y *= val;
  120. return *this;
  121. }
  122. Vector2I& operator*= (const Vector2I& rhs)
  123. {
  124. x *= rhs.x;
  125. y *= rhs.y;
  126. return *this;
  127. }
  128. Vector2I& operator/= (INT32 val)
  129. {
  130. assert(val != 0);
  131. x /= val;
  132. y /= val;
  133. return *this;
  134. }
  135. Vector2I& operator/= (const Vector2I& rhs)
  136. {
  137. x /= rhs.x;
  138. y /= rhs.y;
  139. return *this;
  140. }
  141. /** Returns the square of the length(magnitude) of the vector. */
  142. INT32 squaredLength() const
  143. {
  144. return x * x + y * y;
  145. }
  146. /** Calculates the dot (scalar) product of this vector with another. */
  147. INT32 dot(const Vector2I& vec) const
  148. {
  149. return x * vec.x + y * vec.y;
  150. }
  151. static const Vector2I ZERO;
  152. };
  153. /** @} */
  154. }