CmInt2.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. namespace CamelotFramework
  4. {
  5. struct CM_UTILITY_EXPORT Int2
  6. {
  7. int x;
  8. int y;
  9. inline Int2()
  10. : x(0), y(0)
  11. {
  12. }
  13. inline Int2(const int _x, const int _y )
  14. : x( _x ), y( _y )
  15. {
  16. }
  17. inline explicit Int2( const int scaler )
  18. : x( scaler), y( scaler )
  19. {
  20. }
  21. inline explicit Int2( const int afCoordinate[2] )
  22. {
  23. x = afCoordinate[0];
  24. y = afCoordinate[1];
  25. }
  26. inline explicit Int2( int* const r )
  27. : x( r[0] ), y( r[1] )
  28. {
  29. }
  30. /** Exchange the contents of this vector with another.
  31. */
  32. inline void swap(Int2& other)
  33. {
  34. std::swap(x, other.x);
  35. std::swap(y, other.y);
  36. }
  37. inline unsigned int manhattanDist(const Int2& other)
  38. {
  39. return (unsigned int)fabs(float(other.x - x)) + (unsigned int)fabs(float(other.y - y));
  40. }
  41. inline int operator [] ( const size_t i ) const
  42. {
  43. assert( i < 2 );
  44. return *(&x+i);
  45. }
  46. inline int& operator [] ( const size_t i )
  47. {
  48. assert( i < 2 );
  49. return *(&x+i);
  50. }
  51. /// Pointer accessor for direct copying
  52. inline int* ptr()
  53. {
  54. return &x;
  55. }
  56. /// Pointer accessor for direct copying
  57. inline const int* ptr() const
  58. {
  59. return &x;
  60. }
  61. /** Assigns the value of the other vector.
  62. @param
  63. rhs The other vector
  64. */
  65. inline Int2& operator = ( const Int2& rhs )
  66. {
  67. x = rhs.x;
  68. y = rhs.y;
  69. return *this;
  70. }
  71. inline Int2& operator = ( const int fScalar)
  72. {
  73. x = fScalar;
  74. y = fScalar;
  75. return *this;
  76. }
  77. inline bool operator == ( const Int2& rhs ) const
  78. {
  79. return ( x == rhs.x && y == rhs.y );
  80. }
  81. inline bool operator != ( const Int2& rhs ) const
  82. {
  83. return ( x != rhs.x || y != rhs.y );
  84. }
  85. // arithmetic operations
  86. inline Int2 operator + ( const Int2& rhs ) const
  87. {
  88. return Int2(
  89. x + rhs.x,
  90. y + rhs.y);
  91. }
  92. inline Int2 operator - ( const Int2& rhs ) const
  93. {
  94. return Int2(
  95. x - rhs.x,
  96. y - rhs.y);
  97. }
  98. inline Int2 operator * ( const int fScalar ) const
  99. {
  100. return Int2(
  101. x * fScalar,
  102. y * fScalar);
  103. }
  104. inline Int2 operator * ( const Int2& rhs) const
  105. {
  106. return Int2(
  107. x * rhs.x,
  108. y * rhs.y);
  109. }
  110. inline Int2 operator / ( const int fScalar ) const
  111. {
  112. assert( fScalar != 0 );
  113. return Int2(
  114. x / fScalar,
  115. y / fScalar);
  116. }
  117. inline Int2 operator / ( const Int2& rhs) const
  118. {
  119. return Int2(
  120. x / rhs.x,
  121. y / rhs.y);
  122. }
  123. inline const Int2& operator + () const
  124. {
  125. return *this;
  126. }
  127. inline Int2 operator - () const
  128. {
  129. return Int2(-x, -y);
  130. }
  131. // overloaded operators to help Vector2
  132. inline friend Int2 operator * ( const int fScalar, const Int2& rhs )
  133. {
  134. return Int2(
  135. fScalar * rhs.x,
  136. fScalar * rhs.y);
  137. }
  138. inline friend Int2 operator / ( const int fScalar, const Int2& rhs )
  139. {
  140. return Int2(
  141. fScalar / rhs.x,
  142. fScalar / rhs.y);
  143. }
  144. // arithmetic updates
  145. inline Int2& operator += ( const Int2& rhs )
  146. {
  147. x += rhs.x;
  148. y += rhs.y;
  149. return *this;
  150. }
  151. inline Int2& operator -= ( const Int2& rhs )
  152. {
  153. x -= rhs.x;
  154. y -= rhs.y;
  155. return *this;
  156. }
  157. inline Int2& operator *= ( const int fScalar )
  158. {
  159. x *= fScalar;
  160. y *= fScalar;
  161. return *this;
  162. }
  163. inline Int2& operator *= ( const Int2& rhs )
  164. {
  165. x *= rhs.x;
  166. y *= rhs.y;
  167. return *this;
  168. }
  169. inline Int2& operator /= ( const int fScalar )
  170. {
  171. assert( fScalar != 0 );
  172. x /= fScalar;
  173. y /= fScalar;
  174. return *this;
  175. }
  176. inline Int2& operator /= ( const Int2& rhs )
  177. {
  178. x /= rhs.x;
  179. y /= rhs.y;
  180. return *this;
  181. }
  182. };
  183. }