Vector2.inl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. namespace Rocket {
  28. namespace Core {
  29. // Default constructor.
  30. template < typename Type >
  31. Vector2< Type >::Vector2()
  32. {
  33. }
  34. // Initialising constructor.
  35. template < typename Type >
  36. Vector2< Type >::Vector2(Type _x, Type _y)
  37. {
  38. x = _x;
  39. y = _y;
  40. }
  41. // Returns the magnitude of the vector.
  42. template < typename Type >
  43. float Vector2< Type >::Magnitude() const
  44. {
  45. float squared_magnitude = (float)SquaredMagnitude();
  46. if (Math::IsZero(squared_magnitude))
  47. return 0;
  48. return Math::SquareRoot(squared_magnitude);
  49. }
  50. // Returns the squared magnitude of the vector.
  51. template < typename Type >
  52. Type Vector2< Type >::SquaredMagnitude() const
  53. {
  54. return x * x +
  55. y * y;
  56. }
  57. // Generates a normalised vector from this vector.
  58. template < typename Type >
  59. Vector2< Type > Vector2< Type >::Normalise() const
  60. {
  61. float magnitude = Magnitude();
  62. if (Math::IsZero(magnitude))
  63. return *this;
  64. return *this / magnitude;
  65. }
  66. // Generates a rounded vector from this vector.
  67. template < >
  68. inline Vector2< float > Vector2< float >::Round() const
  69. {
  70. Vector2 < float > result;
  71. result.x = Math::RoundFloat(x);
  72. result.y = Math::RoundFloat(y);
  73. return result;
  74. }
  75. // Generates a rounded vector from this vector.
  76. template < >
  77. inline Vector2< int > Vector2< int >::Round() const
  78. {
  79. return *this;
  80. }
  81. // Computes the dot-product between this vector and another.
  82. template < typename Type >
  83. Type Vector2< Type >::DotProduct(const Vector2< Type >& rhs) const
  84. {
  85. return x * rhs.x +
  86. y * rhs.y;
  87. }
  88. // Returns this vector rotated around the origin.
  89. template < typename Type >
  90. Vector2< Type > Vector2< Type >::Rotate(float theta) const
  91. {
  92. float cos_theta = Math::Cos(theta);
  93. float sin_theta = Math::Sin(theta);
  94. return Vector2< Type >(((Type)(cos_theta * x - sin_theta * y)),
  95. ((Type)(sin_theta * x + cos_theta * y)));
  96. }
  97. // Returns the negation of this vector.
  98. template < typename Type >
  99. Vector2< Type > Vector2< Type >::operator-() const
  100. {
  101. return Vector2(-x, -y);
  102. }
  103. // Returns the sum of this vector and another.
  104. template < typename Type >
  105. Vector2< Type > Vector2< Type >::operator+(const Vector2< Type > & rhs) const
  106. {
  107. return Vector2< Type >(x + rhs.x, y + rhs.y);
  108. }
  109. // Returns the result of subtracting another vector from this vector.
  110. template < typename Type >
  111. Vector2< Type > Vector2< Type >::operator-(const Vector2< Type > & rhs) const
  112. {
  113. return Vector2(x - rhs.x, y - rhs.y);
  114. }
  115. // Returns the result of multiplying this vector by a scalar.
  116. template < typename Type >
  117. Vector2< Type > Vector2< Type >::operator*(Type rhs) const
  118. {
  119. return Vector2(x * rhs, y * rhs);
  120. }
  121. // Returns the result of dividing this vector by a scalar.
  122. template < typename Type >
  123. Vector2< Type > Vector2< Type >::operator/(Type rhs) const
  124. {
  125. return Vector2(x / rhs, y / rhs);
  126. }
  127. // Adds another vector to this in-place.
  128. template < typename Type >
  129. Vector2< Type >& Vector2< Type >::operator+=(const Vector2 & rhs)
  130. {
  131. x += rhs.x;
  132. y += rhs.y;
  133. return *this;
  134. }
  135. // Subtracts another vector from this in-place.
  136. template < typename Type >
  137. Vector2< Type >& Vector2< Type >::operator-=(const Vector2 & rhs)
  138. {
  139. x -= rhs.x;
  140. y -= rhs.y;
  141. return *this;
  142. }
  143. // Scales this vector in-place.
  144. template < typename Type >
  145. Vector2< Type >& Vector2< Type >::operator*=(const Type & rhs)
  146. {
  147. x *= rhs;
  148. y *= rhs;
  149. return *this;
  150. }
  151. // Scales this vector in-place by the inverse of a value.
  152. template < typename Type >
  153. Vector2< Type >& Vector2< Type >::operator/=(const Type & rhs)
  154. {
  155. x /= rhs;
  156. y /= rhs;
  157. return *this;
  158. }
  159. // Equality operator.
  160. template < typename Type >
  161. bool Vector2< Type >::operator==(const Vector2 & rhs) const
  162. {
  163. return (x == rhs.x && y == rhs.y);
  164. }
  165. // Inequality operator.
  166. template < typename Type >
  167. bool Vector2< Type >::operator!=(const Vector2 & rhs) const
  168. {
  169. return (x != rhs.x || y != rhs.y);
  170. }
  171. // Auto-cast operator.
  172. template < typename Type >
  173. Vector2< Type >::operator const Type* () const
  174. {
  175. return &x;
  176. }
  177. // Constant auto-cast operator.
  178. template < typename Type >
  179. Vector2< Type >::operator Type* ()
  180. {
  181. return &x;
  182. }
  183. }
  184. }