Vec2.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #ifndef Vec2_H
  2. #define Vec2_H
  3. #include "FrameworkMath.h"
  4. namespace Framework
  5. {
  6. //! A two-dimensional vector
  7. template<typename T> class Vec2
  8. {
  9. public:
  10. T x; //! x component of the vector
  11. T y; //! y component of the vector
  12. //! Constructor
  13. inline Vec2() noexcept
  14. : x(0),
  15. y(0)
  16. {}
  17. //! Constructor
  18. //! \param x X component of the vector
  19. //! \param y Y component of the vector
  20. inline Vec2(T x, T y)
  21. : x(x),
  22. y(y)
  23. {}
  24. //! Constructor
  25. //! \param vect A vector whose components are adopted
  26. inline Vec2(const Vec2& vect)
  27. : x((T)vect.x),
  28. y((T)vect.y)
  29. {}
  30. //! Scales the vector so that it has length 1
  31. inline Vec2& normalize()
  32. {
  33. const T length = getLength();
  34. x /= length;
  35. y /= length;
  36. return *this;
  37. }
  38. //! Rotates the vector 90 degrees counter-clockwise
  39. inline Vec2& CCW90()
  40. {
  41. T temp = y;
  42. y = -x;
  43. x = temp;
  44. return *this;
  45. }
  46. //! Rotates the vector 90 degrees clockwise
  47. inline Vec2& CW90()
  48. {
  49. T temp = y;
  50. y = x;
  51. x = -temp;
  52. return *this;
  53. }
  54. //! Swaps the values of the vectors
  55. //! \param vect The vector to swap with
  56. inline Vec2& Swap(Vec2& vect)
  57. {
  58. const Vec2 tmp = vect;
  59. vect = *this;
  60. *this = tmp;
  61. return *this;
  62. }
  63. //! Adopts the values of another vector
  64. //! \param r The other vector
  65. inline Vec2 operator=(const Vec2& r)
  66. {
  67. x = r.x;
  68. y = r.y;
  69. return *this;
  70. }
  71. //! Adds another vector to this one
  72. //! \param r The other vector
  73. inline Vec2 operator+=(const Vec2& r)
  74. {
  75. x += r.x;
  76. y += r.y;
  77. return *this;
  78. }
  79. //! Subtracts another vector from this one
  80. //! \param r The other vector
  81. inline Vec2 operator-=(const Vec2& r)
  82. {
  83. x -= r.x;
  84. y -= r.y;
  85. return *this;
  86. }
  87. //! Scales this vector by a scalar
  88. //! \param r The scalar
  89. inline Vec2 operator*=(const T& r)
  90. {
  91. x *= r;
  92. y *= r;
  93. return *this;
  94. }
  95. //! Divides this vector by a scalar
  96. //! \param r The scalar
  97. inline Vec2 operator/=(const T& r)
  98. {
  99. x /= r;
  100. y /= r;
  101. return *this;
  102. }
  103. //! Negates the vector
  104. inline Vec2 operator-() const
  105. {
  106. return Vec2<T>(-x, -y);
  107. }
  108. //! Converts the vector to a vector of another type
  109. template<typename T2> inline operator Vec2<T2>() const
  110. {
  111. return Vec2<T2>((T2)x, (T2)y);
  112. }
  113. //! Calculates the square of the vector's length
  114. inline T getLengthSq() const
  115. {
  116. return *this * *this;
  117. }
  118. //! Calculates the length of the vector
  119. inline T getLength() const
  120. {
  121. return (T)sqrt(getLengthSq());
  122. }
  123. //! Calculates the dot product between two vectors
  124. //! \param r The other vector
  125. inline T operator*(const Vec2& r) const
  126. {
  127. return x * r.x + y * r.y;
  128. }
  129. //! Adds two vectors
  130. //! \param r The other vector
  131. inline Vec2 operator+(const Vec2& r) const
  132. {
  133. return Vec2(*this) += r;
  134. }
  135. //! Subtracts two vectors
  136. //! \param r The other vector
  137. inline Vec2 operator-(const Vec2& r) const
  138. {
  139. return Vec2(*this) -= r;
  140. }
  141. //! Multiplies the components of two vectors
  142. //! \param r The other vector
  143. inline Vec2 operator*(const T& r) const
  144. {
  145. return Vec2(*this) *= r;
  146. }
  147. //! Divides the components of two vectors
  148. //! \param r The other vector
  149. inline Vec2 operator/(const T& r) const
  150. {
  151. return Vec2(*this) /= r;
  152. }
  153. //! Checks whether the vector is in the rectangle between two vectors
  154. //! p1: A vector to one corner of the rectangle
  155. //! p2: A vector to the opposite corner of the rectangle
  156. inline bool isInRegion(const Vec2& p1, const Vec2& p2) const
  157. {
  158. const T medianX = (T)((p1.x + p2.x) / 2.0);
  159. const T medianY = (T)((p1.y + p2.y) / 2.0);
  160. return abs<T>(medianX - x) <= abs<T>(medianX - p1.x)
  161. && abs<T>(medianY - y) <= abs<T>(medianY - p1.y);
  162. }
  163. //! Checks two vectors for equality
  164. //! \param r The other vector
  165. inline bool operator==(const Vec2& r) const
  166. {
  167. return x == r.x && y == r.y;
  168. }
  169. //! Checks two vectors for inequality
  170. //! \param r The other vector
  171. inline bool operator!=(const Vec2& r) const
  172. {
  173. return !(*this == r);
  174. }
  175. //! Calculates the midpoint between two vectors
  176. //! p2: The other vector
  177. inline Vec2 mittelpunktMit(const Vec2& p2) const
  178. {
  179. return Vec2((T)((x + p2.x) / 2.0), (T)((y + p2.y) / 2.0));
  180. }
  181. //! Rotates the vector counter-clockwise
  182. //! \param angle The angle in radians
  183. inline Vec2 rotation(const float angle) const
  184. {
  185. Vec2 result;
  186. float cosine = lowPrecisionCos(angle);
  187. float sine = lowPrecisionSin(angle);
  188. result.x = (T)(x * cosine - y * sine);
  189. result.y = (T)(x * sine + y * cosine);
  190. return result;
  191. }
  192. //! Determines the angle between two vectors
  193. inline T angle(const Vec2& v2) const
  194. {
  195. return (T)atan2((float)(x * v2.y - y * v2.x), (float)(*this * v2));
  196. }
  197. };
  198. } // namespace Framework
  199. #endif