CmInt2.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 int operator [] ( const size_t i ) const
  38. {
  39. assert( i < 2 );
  40. return *(&x+i);
  41. }
  42. inline int& operator [] ( const size_t i )
  43. {
  44. assert( i < 2 );
  45. return *(&x+i);
  46. }
  47. /// Pointer accessor for direct copying
  48. inline int* ptr()
  49. {
  50. return &x;
  51. }
  52. /// Pointer accessor for direct copying
  53. inline const int* ptr() const
  54. {
  55. return &x;
  56. }
  57. /** Assigns the value of the other vector.
  58. @param
  59. rhs The other vector
  60. */
  61. inline Int2& operator = ( const Int2& rhs )
  62. {
  63. x = rhs.x;
  64. y = rhs.y;
  65. return *this;
  66. }
  67. inline Int2& operator = ( const int fScalar)
  68. {
  69. x = fScalar;
  70. y = fScalar;
  71. return *this;
  72. }
  73. inline bool operator == ( const Int2& rhs ) const
  74. {
  75. return ( x == rhs.x && y == rhs.y );
  76. }
  77. inline bool operator != ( const Int2& rhs ) const
  78. {
  79. return ( x != rhs.x || y != rhs.y );
  80. }
  81. // arithmetic operations
  82. inline Int2 operator + ( const Int2& rhs ) const
  83. {
  84. return Int2(
  85. x + rhs.x,
  86. y + rhs.y);
  87. }
  88. inline Int2 operator - ( const Int2& rhs ) const
  89. {
  90. return Int2(
  91. x - rhs.x,
  92. y - rhs.y);
  93. }
  94. inline Int2 operator * ( const int fScalar ) const
  95. {
  96. return Int2(
  97. x * fScalar,
  98. y * fScalar);
  99. }
  100. inline Int2 operator * ( const Int2& rhs) const
  101. {
  102. return Int2(
  103. x * rhs.x,
  104. y * rhs.y);
  105. }
  106. inline Int2 operator / ( const int fScalar ) const
  107. {
  108. assert( fScalar != 0 );
  109. return Int2(
  110. x / fScalar,
  111. y / fScalar);
  112. }
  113. inline Int2 operator / ( const Int2& rhs) const
  114. {
  115. return Int2(
  116. x / rhs.x,
  117. y / rhs.y);
  118. }
  119. inline const Int2& operator + () const
  120. {
  121. return *this;
  122. }
  123. inline Int2 operator - () const
  124. {
  125. return Int2(-x, -y);
  126. }
  127. // overloaded operators to help Vector2
  128. inline friend Int2 operator * ( const int fScalar, const Int2& rhs )
  129. {
  130. return Int2(
  131. fScalar * rhs.x,
  132. fScalar * rhs.y);
  133. }
  134. inline friend Int2 operator / ( const int fScalar, const Int2& rhs )
  135. {
  136. return Int2(
  137. fScalar / rhs.x,
  138. fScalar / rhs.y);
  139. }
  140. // arithmetic updates
  141. inline Int2& operator += ( const Int2& rhs )
  142. {
  143. x += rhs.x;
  144. y += rhs.y;
  145. return *this;
  146. }
  147. inline Int2& operator -= ( const Int2& rhs )
  148. {
  149. x -= rhs.x;
  150. y -= rhs.y;
  151. return *this;
  152. }
  153. inline Int2& operator *= ( const int fScalar )
  154. {
  155. x *= fScalar;
  156. y *= fScalar;
  157. return *this;
  158. }
  159. inline Int2& operator *= ( const Int2& rhs )
  160. {
  161. x *= rhs.x;
  162. y *= rhs.y;
  163. return *this;
  164. }
  165. inline Int2& operator /= ( const int fScalar )
  166. {
  167. assert( fScalar != 0 );
  168. x /= fScalar;
  169. y /= fScalar;
  170. return *this;
  171. }
  172. inline Int2& operator /= ( const Int2& rhs )
  173. {
  174. x /= rhs.x;
  175. y /= rhs.y;
  176. return *this;
  177. }
  178. };
  179. }