/Include/Rocket/Core/Vector2.h

http://github.com/lloydw/libRocket · C Header · 137 lines · 37 code · 17 blank · 83 comment · 0 complexity · 81afde3e2de98c62e3898296d0be9114 MD5 · raw file

  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. #ifndef ROCKETCOREVECTOR2_H
  28. #define ROCKETCOREVECTOR2_H
  29. #include "Debug.h"
  30. #include "Math.h"
  31. namespace Rocket {
  32. namespace Core {
  33. /**
  34. Templated class for a generic two-component vector.
  35. @author Peter Curry
  36. */
  37. template < typename Type >
  38. class Vector2
  39. {
  40. public:
  41. /// Lightweight, non-initialising constructor.
  42. inline Vector2();
  43. /// Initialising constructor.
  44. /// @param[in] x Initial x-value of the vector.
  45. /// @param[in] y Initial y-value of the vector.
  46. inline Vector2(Type x, Type y);
  47. /// Returns the magnitude of the vector.
  48. /// @return The computed magnitude.
  49. inline float Magnitude() const;
  50. /// Returns the squared magnitude of the vector.
  51. /// @return The computed squared magnitude.
  52. inline Type SquaredMagnitude() const;
  53. /// Generates a normalised vector from this vector.
  54. /// @return The normalised vector.
  55. inline Vector2 Normalise() const;
  56. /// Computes the dot-product between this vector and another.
  57. /// @param[in] rhs The other vector to use in the dot-product.
  58. /// @return The computed dot-product between the two vectors.
  59. inline Type DotProduct(const Vector2& rhs) const;
  60. /// Returns this vector rotated around the origin.
  61. /// @param[in] theta The angle to rotate by, in radians.
  62. /// @return The rotated vector.
  63. inline Vector2 Rotate(float theta) const;
  64. /// Returns the negation of this vector.
  65. /// @return The negation of this vector.
  66. inline Vector2 operator-() const;
  67. /// Returns the sum of this vector and another.
  68. /// @param[in] rhs The vector to add this to.
  69. /// @return The sum of the two vectors.
  70. inline Vector2 operator+(const Vector2& rhs) const;
  71. /// Returns the result of subtracting another vector from this vector.
  72. /// @param[in] rhs The vector to subtract from this vector.
  73. /// @return The result of the subtraction.
  74. inline Vector2 operator-(const Vector2& rhs) const;
  75. /// Returns the result of multiplying this vector by a scalar.
  76. /// @param[in] rhs The scalar value to multiply by.
  77. /// @return The result of the scale.
  78. inline Vector2 operator*(Type rhs) const;
  79. /// Returns the result of dividing this vector by a scalar.
  80. /// @param[in] rhs The scalar value to divide by.
  81. /// @return The result of the scale.
  82. inline Vector2 operator/(Type rhs) const;
  83. /// Adds another vector to this in-place.
  84. /// @param[in] rhs The vector to add.
  85. /// @return This vector, post-operation.
  86. inline Vector2& operator+=(const Vector2& rhs);
  87. /// Subtracts another vector from this in-place.
  88. /// @param[in] rhs The vector to subtract.
  89. /// @return This vector, post-operation.
  90. inline Vector2& operator-=(const Vector2& rhs);
  91. /// Scales this vector in-place.
  92. /// @param[in] rhs The value to scale this vector's components by.
  93. /// @return This vector, post-operation.
  94. inline Vector2& operator*=(const Type& rhs);
  95. /// Scales this vector in-place by the inverse of a value.
  96. /// @param[in] rhs The value to divide this vector's components by.
  97. /// @return This vector, post-operation.
  98. inline Vector2& operator/=(const Type& rhs);
  99. /// Equality operator.
  100. /// @param[in] rhs The vector to compare this against.
  101. /// @return True if the two vectors are equal, false otherwise.
  102. inline bool operator==(const Vector2& rhs) const;
  103. /// Inequality operator.
  104. /// @param[in] rhs The vector to compare this against.
  105. /// @return True if the two vectors are not equal, false otherwise.
  106. inline bool operator!=(const Vector2& rhs) const;
  107. /// Auto-cast operator.
  108. /// @return A pointer to the first value.
  109. inline operator const Type*() const;
  110. /// Constant auto-cast operator.
  111. /// @return A constant pointer to the first value.
  112. inline operator Type*();
  113. // The components of the vector.
  114. Type x;
  115. Type y;
  116. };
  117. #include "Vector2.inl"
  118. }
  119. }
  120. #endif