CmVector2I.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. namespace CamelotFramework
  4. {
  5. /**
  6. * @brief A two dimensional vector with integer
  7. * coordinates.
  8. */
  9. struct CM_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. /**
  23. * @brief Exchange the contents of this vector with another.
  24. */
  25. void swap(Vector2I& other)
  26. {
  27. std::swap(x, other.x);
  28. std::swap(y, other.y);
  29. }
  30. /**
  31. * @brief Returns the manhattan distance between this and another vector.
  32. */
  33. UINT32 manhattanDist(const Vector2I& other)
  34. {
  35. return (UINT32)fabs(float(other.x - x)) + (UINT32)fabs(float(other.y - y));
  36. }
  37. INT32 operator[] (size_t i) const
  38. {
  39. assert(i < 2);
  40. return *(&x+i);
  41. }
  42. INT32& operator[] (size_t i)
  43. {
  44. assert(i < 2);
  45. return *(&x+i);
  46. }
  47. Vector2I& operator= (const Vector2I& rhs)
  48. {
  49. x = rhs.x;
  50. y = rhs.y;
  51. return *this;
  52. }
  53. Vector2I& operator= (int val)
  54. {
  55. x = val;
  56. y = val;
  57. return *this;
  58. }
  59. bool operator== (const Vector2I& rhs) const
  60. {
  61. return (x == rhs.x && y == rhs.y);
  62. }
  63. bool operator!= (const Vector2I& rhs) const
  64. {
  65. return (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- (const Vector2I& rhs) const
  72. {
  73. return Vector2I(x - rhs.x, y - rhs.y);
  74. }
  75. Vector2I operator* (int val) const
  76. {
  77. return Vector2I(x * val, y * val);
  78. }
  79. Vector2I operator* (const Vector2I& rhs) const
  80. {
  81. return Vector2I(x * rhs.x, y * rhs.y);
  82. }
  83. Vector2I operator/ (int val) const
  84. {
  85. assert(val != 0);
  86. return Vector2I(x / val, y / val);
  87. }
  88. Vector2I operator/ (const Vector2I& rhs) const
  89. {
  90. return Vector2I(x / rhs.x, y / rhs.y);
  91. }
  92. const Vector2I& operator+ () const
  93. {
  94. return *this;
  95. }
  96. Vector2I operator- () const
  97. {
  98. return Vector2I(-x, -y);
  99. }
  100. friend Vector2I operator* (int lhs, const Vector2I& rhs)
  101. {
  102. return Vector2I(lhs * rhs.x, lhs * rhs.y);
  103. }
  104. friend Vector2I operator/ (int lhs, const Vector2I& rhs)
  105. {
  106. return Vector2I(lhs / rhs.x, lhs / rhs.y);
  107. }
  108. Vector2I& operator+= (const Vector2I& rhs)
  109. {
  110. x += rhs.x;
  111. y += rhs.y;
  112. return *this;
  113. }
  114. Vector2I& operator-= (const Vector2I& rhs)
  115. {
  116. x -= rhs.x;
  117. y -= rhs.y;
  118. return *this;
  119. }
  120. Vector2I& operator*= (INT32 val)
  121. {
  122. x *= val;
  123. y *= val;
  124. return *this;
  125. }
  126. Vector2I& operator*= (const Vector2I& rhs)
  127. {
  128. x *= rhs.x;
  129. y *= rhs.y;
  130. return *this;
  131. }
  132. Vector2I& operator/= (INT32 val)
  133. {
  134. assert(val != 0);
  135. x /= val;
  136. y /= val;
  137. return *this;
  138. }
  139. Vector2I& operator/= (const Vector2I& rhs)
  140. {
  141. x /= rhs.x;
  142. y /= rhs.y;
  143. return *this;
  144. }
  145. };
  146. }