CmVector2I.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. namespace CamelotFramework
  4. {
  5. struct CM_UTILITY_EXPORT Vector2I
  6. {
  7. INT32 x;
  8. INT32 y;
  9. Vector2I()
  10. :x(0), y(0)
  11. { }
  12. inline Vector2I(INT32 _x, INT32 _y )
  13. :x(_x), y(_y)
  14. { }
  15. explicit Vector2I(int val)
  16. :x(val), y(val)
  17. { }
  18. void swap(Vector2I& other)
  19. {
  20. std::swap(x, other.x);
  21. std::swap(y, other.y);
  22. }
  23. UINT32 manhattanDist(const Vector2I& other)
  24. {
  25. return (UINT32)fabs(float(other.x - x)) + (UINT32)fabs(float(other.y - y));
  26. }
  27. INT32 operator[] (size_t i) const
  28. {
  29. assert(i < 2);
  30. return *(&x+i);
  31. }
  32. INT32& operator[] (size_t i)
  33. {
  34. assert(i < 2);
  35. return *(&x+i);
  36. }
  37. Vector2I& operator= (const Vector2I& rhs)
  38. {
  39. x = rhs.x;
  40. y = rhs.y;
  41. return *this;
  42. }
  43. Vector2I& operator= (int val)
  44. {
  45. x = val;
  46. y = val;
  47. return *this;
  48. }
  49. bool operator== (const Vector2I& rhs) const
  50. {
  51. return (x == rhs.x && y == rhs.y);
  52. }
  53. bool operator!= (const Vector2I& rhs) const
  54. {
  55. return (x != rhs.x || y != rhs.y);
  56. }
  57. Vector2I operator+ (const Vector2I& rhs) const
  58. {
  59. return Vector2I(x + rhs.x, y + rhs.y);
  60. }
  61. Vector2I operator- (const Vector2I& rhs) const
  62. {
  63. return Vector2I(x - rhs.x, y - rhs.y);
  64. }
  65. Vector2I operator* (int val) const
  66. {
  67. return Vector2I(x * val, y * val);
  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. assert(val != 0);
  76. return Vector2I(x / val, y / val);
  77. }
  78. Vector2I operator/ (const Vector2I& rhs) const
  79. {
  80. return Vector2I(x / rhs.x, y / rhs.y);
  81. }
  82. const Vector2I& operator+ () const
  83. {
  84. return *this;
  85. }
  86. Vector2I operator- () const
  87. {
  88. return Vector2I(-x, -y);
  89. }
  90. friend Vector2I operator* (int lhs, const Vector2I& rhs)
  91. {
  92. return Vector2I(lhs * rhs.x, lhs * rhs.y);
  93. }
  94. friend Vector2I operator/ (int lhs, const Vector2I& rhs)
  95. {
  96. return Vector2I(lhs / rhs.x, lhs / rhs.y);
  97. }
  98. Vector2I& operator+= (const Vector2I& rhs)
  99. {
  100. x += rhs.x;
  101. y += rhs.y;
  102. return *this;
  103. }
  104. Vector2I& operator-= (const Vector2I& rhs)
  105. {
  106. x -= rhs.x;
  107. y -= rhs.y;
  108. return *this;
  109. }
  110. Vector2I& operator*= (INT32 val)
  111. {
  112. x *= val;
  113. y *= val;
  114. return *this;
  115. }
  116. Vector2I& operator*= (const Vector2I& rhs)
  117. {
  118. x *= rhs.x;
  119. y *= rhs.y;
  120. return *this;
  121. }
  122. Vector2I& operator/= (INT32 val)
  123. {
  124. assert(val != 0);
  125. x /= val;
  126. y /= val;
  127. return *this;
  128. }
  129. Vector2I& operator/= (const Vector2I& rhs)
  130. {
  131. x /= rhs.x;
  132. y /= rhs.y;
  133. return *this;
  134. }
  135. };
  136. }