vector2d.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __IRR_POINT_2D_H_INCLUDED__
  5. #define __IRR_POINT_2D_H_INCLUDED__
  6. #include "irrMath.h"
  7. #include "dimension2d.h"
  8. namespace irr
  9. {
  10. namespace core
  11. {
  12. //! 2d vector template class with lots of operators and methods.
  13. /** As of Irrlicht 1.6, this class supercedes position2d, which should
  14. be considered deprecated. */
  15. template <class T>
  16. class vector2d
  17. {
  18. public:
  19. //! Default constructor (null vector)
  20. vector2d() : X(0), Y(0) {}
  21. //! Constructor with two different values
  22. vector2d(T nx, T ny) : X(nx), Y(ny) {}
  23. //! Constructor with the same value for both members
  24. explicit vector2d(T n) : X(n), Y(n) {}
  25. //! Copy constructor
  26. vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {}
  27. vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}
  28. // operators
  29. vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }
  30. vector2d<T>& operator=(const vector2d<T>& other) { X = other.X; Y = other.Y; return *this; }
  31. vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Height; return *this; }
  32. vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }
  33. vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }
  34. vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }
  35. vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }
  36. vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }
  37. vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this; }
  38. vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }
  39. vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }
  40. vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }
  41. vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }
  42. vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }
  43. vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this; }
  44. vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }
  45. vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }
  46. vector2d<T> operator*(const T v) const { return vector2d<T>(X * v, Y * v); }
  47. vector2d<T>& operator*=(const T v) { X*=v; Y*=v; return *this; }
  48. vector2d<T> operator/(const vector2d<T>& other) const { return vector2d<T>(X / other.X, Y / other.Y); }
  49. vector2d<T>& operator/=(const vector2d<T>& other) { X/=other.X; Y/=other.Y; return *this; }
  50. vector2d<T> operator/(const T v) const { return vector2d<T>(X / v, Y / v); }
  51. vector2d<T>& operator/=(const T v) { X/=v; Y/=v; return *this; }
  52. //! sort in order X, Y. Equality with rounding tolerance.
  53. bool operator<=(const vector2d<T>&other) const
  54. {
  55. return (X<other.X || core::equals(X, other.X)) ||
  56. (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y)));
  57. }
  58. //! sort in order X, Y. Equality with rounding tolerance.
  59. bool operator>=(const vector2d<T>&other) const
  60. {
  61. return (X>other.X || core::equals(X, other.X)) ||
  62. (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y)));
  63. }
  64. //! sort in order X, Y. Difference must be above rounding tolerance.
  65. bool operator<(const vector2d<T>&other) const
  66. {
  67. return (X<other.X && !core::equals(X, other.X)) ||
  68. (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y));
  69. }
  70. //! sort in order X, Y. Difference must be above rounding tolerance.
  71. bool operator>(const vector2d<T>&other) const
  72. {
  73. return (X>other.X && !core::equals(X, other.X)) ||
  74. (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y));
  75. }
  76. bool operator==(const vector2d<T>& other) const { return equals(other); }
  77. bool operator!=(const vector2d<T>& other) const { return !equals(other); }
  78. // functions
  79. //! Checks if this vector equals the other one.
  80. /** Takes floating point rounding errors into account.
  81. \param other Vector to compare with.
  82. \return True if the two vector are (almost) equal, else false. */
  83. bool equals(const vector2d<T>& other) const
  84. {
  85. return core::equals(X, other.X) && core::equals(Y, other.Y);
  86. }
  87. vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }
  88. vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }
  89. //! Gets the length of the vector.
  90. /** \return The length of the vector. */
  91. T getLength() const { return core::squareroot( X*X + Y*Y ); }
  92. //! Get the squared length of this vector
  93. /** This is useful because it is much faster than getLength().
  94. \return The squared length of the vector. */
  95. T getLengthSQ() const { return X*X + Y*Y; }
  96. //! Get the dot product of this vector with another.
  97. /** \param other Other vector to take dot product with.
  98. \return The dot product of the two vectors. */
  99. T dotProduct(const vector2d<T>& other) const
  100. {
  101. return X*other.X + Y*other.Y;
  102. }
  103. //! Gets distance from another point.
  104. /** Here, the vector is interpreted as a point in 2-dimensional space.
  105. \param other Other vector to measure from.
  106. \return Distance from other point. */
  107. T getDistanceFrom(const vector2d<T>& other) const
  108. {
  109. return vector2d<T>(X - other.X, Y - other.Y).getLength();
  110. }
  111. //! Returns squared distance from another point.
  112. /** Here, the vector is interpreted as a point in 2-dimensional space.
  113. \param other Other vector to measure from.
  114. \return Squared distance from other point. */
  115. T getDistanceFromSQ(const vector2d<T>& other) const
  116. {
  117. return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();
  118. }
  119. //! rotates the point anticlockwise around a center by an amount of degrees.
  120. /** \param degrees Amount of degrees to rotate by, anticlockwise.
  121. \param center Rotation center.
  122. \return This vector after transformation. */
  123. vector2d<T>& rotateBy(f64 degrees, const vector2d<T>& center=vector2d<T>())
  124. {
  125. degrees *= DEGTORAD64;
  126. const f64 cs = cos(degrees);
  127. const f64 sn = sin(degrees);
  128. X -= center.X;
  129. Y -= center.Y;
  130. set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));
  131. X += center.X;
  132. Y += center.Y;
  133. return *this;
  134. }
  135. //! Normalize the vector.
  136. /** The null vector is left untouched.
  137. \return Reference to this vector, after normalization. */
  138. vector2d<T>& normalize()
  139. {
  140. f32 length = (f32)(X*X + Y*Y);
  141. if ( length == 0 )
  142. return *this;
  143. length = core::reciprocal_squareroot ( length );
  144. X = (T)(X * length);
  145. Y = (T)(Y * length);
  146. return *this;
  147. }
  148. //! Calculates the angle of this vector in degrees in the trigonometric sense.
  149. /** 0 is to the right (3 o'clock), values increase counter-clockwise.
  150. This method has been suggested by Pr3t3nd3r.
  151. \return Returns a value between 0 and 360. */
  152. f64 getAngleTrig() const
  153. {
  154. if (Y == 0)
  155. return X < 0 ? 180 : 0;
  156. else
  157. if (X == 0)
  158. return Y < 0 ? 270 : 90;
  159. if ( Y > 0)
  160. if (X > 0)
  161. return atan((irr::f64)Y/(irr::f64)X) * RADTODEG64;
  162. else
  163. return 180.0-atan((irr::f64)Y/-(irr::f64)X) * RADTODEG64;
  164. else
  165. if (X > 0)
  166. return 360.0-atan(-(irr::f64)Y/(irr::f64)X) * RADTODEG64;
  167. else
  168. return 180.0+atan(-(irr::f64)Y/-(irr::f64)X) * RADTODEG64;
  169. }
  170. //! Calculates the angle of this vector in degrees in the counter trigonometric sense.
  171. /** 0 is to the right (3 o'clock), values increase clockwise.
  172. \return Returns a value between 0 and 360. */
  173. inline f64 getAngle() const
  174. {
  175. if (Y == 0) // corrected thanks to a suggestion by Jox
  176. return X < 0 ? 180 : 0;
  177. else if (X == 0)
  178. return Y < 0 ? 90 : 270;
  179. // don't use getLength here to avoid precision loss with s32 vectors
  180. // avoid floating-point trouble as sqrt(y*y) is occasionally larger than y, so clamp
  181. const f64 tmp = core::clamp(Y / sqrt((f64)(X*X + Y*Y)), -1.0, 1.0);
  182. const f64 angle = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;
  183. if (X>0 && Y>0)
  184. return angle + 270;
  185. else
  186. if (X>0 && Y<0)
  187. return angle + 90;
  188. else
  189. if (X<0 && Y<0)
  190. return 90 - angle;
  191. else
  192. if (X<0 && Y>0)
  193. return 270 - angle;
  194. return angle;
  195. }
  196. //! Calculates the angle between this vector and another one in degree.
  197. /** \param b Other vector to test with.
  198. \return Returns a value between 0 and 90. */
  199. inline f64 getAngleWith(const vector2d<T>& b) const
  200. {
  201. f64 tmp = (f64)(X*b.X + Y*b.Y);
  202. if (tmp == 0.0)
  203. return 90.0;
  204. tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));
  205. if (tmp < 0.0)
  206. tmp = -tmp;
  207. if ( tmp > 1.0 ) // avoid floating-point trouble
  208. tmp = 1.0;
  209. return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;
  210. }
  211. //! Returns if this vector interpreted as a point is on a line between two other points.
  212. /** It is assumed that the point is on the line.
  213. \param begin Beginning vector to compare between.
  214. \param end Ending vector to compare between.
  215. \return True if this vector is between begin and end, false if not. */
  216. bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const
  217. {
  218. if (begin.X != end.X)
  219. {
  220. return ((begin.X <= X && X <= end.X) ||
  221. (begin.X >= X && X >= end.X));
  222. }
  223. else
  224. {
  225. return ((begin.Y <= Y && Y <= end.Y) ||
  226. (begin.Y >= Y && Y >= end.Y));
  227. }
  228. }
  229. //! Creates an interpolated vector between this vector and another vector.
  230. /** \param other The other vector to interpolate with.
  231. \param d Interpolation value between 0.0f (all the other vector) and 1.0f (all this vector).
  232. Note that this is the opposite direction of interpolation to getInterpolated_quadratic()
  233. \return An interpolated vector. This vector is not modified. */
  234. vector2d<T> getInterpolated(const vector2d<T>& other, f64 d) const
  235. {
  236. f64 inv = 1.0f - d;
  237. return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));
  238. }
  239. //! Creates a quadratically interpolated vector between this and two other vectors.
  240. /** \param v2 Second vector to interpolate with.
  241. \param v3 Third vector to interpolate with (maximum at 1.0f)
  242. \param d Interpolation value between 0.0f (all this vector) and 1.0f (all the 3rd vector).
  243. Note that this is the opposite direction of interpolation to getInterpolated() and interpolate()
  244. \return An interpolated vector. This vector is not modified. */
  245. vector2d<T> getInterpolated_quadratic(const vector2d<T>& v2, const vector2d<T>& v3, f64 d) const
  246. {
  247. // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
  248. const f64 inv = 1.0f - d;
  249. const f64 mul0 = inv * inv;
  250. const f64 mul1 = 2.0f * d * inv;
  251. const f64 mul2 = d * d;
  252. return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
  253. (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));
  254. }
  255. //! Sets this vector to the linearly interpolated vector between a and b.
  256. /** \param a first vector to interpolate with, maximum at 1.0f
  257. \param b second vector to interpolate with, maximum at 0.0f
  258. \param d Interpolation value between 0.0f (all vector b) and 1.0f (all vector a)
  259. Note that this is the opposite direction of interpolation to getInterpolated_quadratic()
  260. */
  261. vector2d<T>& interpolate(const vector2d<T>& a, const vector2d<T>& b, f64 d)
  262. {
  263. X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
  264. Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
  265. return *this;
  266. }
  267. //! X coordinate of vector.
  268. T X;
  269. //! Y coordinate of vector.
  270. T Y;
  271. };
  272. //! Typedef for f32 2d vector.
  273. typedef vector2d<f32> vector2df;
  274. //! Typedef for integer 2d vector.
  275. typedef vector2d<s32> vector2di;
  276. template<class S, class T>
  277. vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
  278. // These methods are declared in dimension2d, but need definitions of vector2d
  279. template<class T>
  280. dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
  281. template<class T>
  282. bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
  283. } // end namespace core
  284. } // end namespace irr
  285. #endif