BsVector2I.h 4.1 KB

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