CmInt2.h 4.5 KB

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