Vector2.inl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Default constructor.
  28. template < typename Type >
  29. Vector2< Type >::Vector2()
  30. {
  31. }
  32. // Initialising constructor.
  33. template < typename Type >
  34. Vector2< Type >::Vector2(Type _x, Type _y)
  35. {
  36. x = _x;
  37. y = _y;
  38. }
  39. // Returns the magnitude of the vector.
  40. template < typename Type >
  41. float Vector2< Type >::Magnitude() const
  42. {
  43. float squared_magnitude = (float) SquaredMagnitude();
  44. if (Math::IsZero(squared_magnitude))
  45. return 0;
  46. return Math::SquareRoot(squared_magnitude);
  47. }
  48. // Returns the squared magnitude of the vector.
  49. template < typename Type >
  50. Type Vector2< Type >::SquaredMagnitude() const
  51. {
  52. return x * x +
  53. y * y;
  54. }
  55. // Generates a normalised vector from this vector.
  56. template < typename Type >
  57. Vector2< Type > Vector2< Type >::Normalise() const
  58. {
  59. float magnitude = Magnitude();
  60. if (Math::IsZero(magnitude))
  61. return *this;
  62. return *this / magnitude;
  63. }
  64. // Computes the dot-product between this vector and another.
  65. template < typename Type >
  66. Type Vector2< Type >::DotProduct(const Vector2< Type >& rhs) const
  67. {
  68. return x * rhs.x +
  69. y * rhs.y;
  70. }
  71. // Returns this vector rotated around the origin.
  72. template < typename Type >
  73. Vector2< Type > Vector2< Type >::Rotate(float theta) const
  74. {
  75. float cos_theta = Math::Cos(theta);
  76. float sin_theta = Math::Sin(theta);
  77. return Vector2< Type >(((Type)(cos_theta * x - sin_theta * y)),
  78. ((Type)(sin_theta * x + cos_theta * y)));
  79. }
  80. // Returns the negation of this vector.
  81. template < typename Type >
  82. Vector2< Type > Vector2< Type >::operator-() const
  83. {
  84. return Vector2(-x, -y);
  85. }
  86. // Returns the sum of this vector and another.
  87. template < typename Type >
  88. Vector2< Type > Vector2< Type >::operator+(const Vector2< Type >& rhs) const
  89. {
  90. return Vector2< Type >(x + rhs.x, y + rhs.y);
  91. }
  92. // Returns the result of subtracting another vector from this vector.
  93. template < typename Type >
  94. Vector2< Type > Vector2< Type >::operator-(const Vector2< Type >& rhs) const
  95. {
  96. return Vector2(x - rhs.x, y - rhs.y);
  97. }
  98. // Returns the result of multiplying this vector by a scalar.
  99. template < typename Type >
  100. Vector2< Type > Vector2< Type >::operator*(Type rhs) const
  101. {
  102. return Vector2(x * rhs, y * rhs);
  103. }
  104. // Returns the result of dividing this vector by a scalar.
  105. template < typename Type >
  106. Vector2< Type > Vector2< Type >::operator/(Type rhs) const
  107. {
  108. return Vector2(x / rhs, y / rhs);
  109. }
  110. // Adds another vector to this in-place.
  111. template < typename Type >
  112. Vector2< Type >& Vector2< Type >::operator+=(const Vector2& rhs)
  113. {
  114. x += rhs.x;
  115. y += rhs.y;
  116. return *this;
  117. }
  118. // Subtracts another vector from this in-place.
  119. template < typename Type >
  120. Vector2< Type >& Vector2< Type >::operator-=(const Vector2& rhs)
  121. {
  122. x -= rhs.x;
  123. y -= rhs.y;
  124. return *this;
  125. }
  126. // Scales this vector in-place.
  127. template < typename Type >
  128. Vector2< Type >& Vector2< Type >::operator*=(const Type& rhs)
  129. {
  130. x *= rhs;
  131. y *= rhs;
  132. return *this;
  133. }
  134. // Scales this vector in-place by the inverse of a value.
  135. template < typename Type >
  136. Vector2< Type >& Vector2< Type >::operator/=(const Type& rhs)
  137. {
  138. x /= rhs;
  139. y /= rhs;
  140. return *this;
  141. }
  142. // Equality operator.
  143. template < typename Type >
  144. bool Vector2< Type >::operator==(const Vector2& rhs) const
  145. {
  146. return (x == rhs.x && y == rhs.y);
  147. }
  148. // Inequality operator.
  149. template < typename Type >
  150. bool Vector2< Type >::operator!=(const Vector2& rhs) const
  151. {
  152. return (x != rhs.x || y != rhs.y);
  153. }
  154. // Auto-cast operator.
  155. template < typename Type >
  156. Vector2< Type >::operator const Type*() const
  157. {
  158. return &x;
  159. }
  160. // Constant auto-cast operator.
  161. template < typename Type >
  162. Vector2< Type >::operator Type*()
  163. {
  164. return &x;
  165. }