Point2.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Assert.h"
  25. #include "Types.h"
  26. #include "MathUtils.h"
  27. #include "Vec2.h"
  28. #include "Vec3.h"
  29. namespace crown
  30. {
  31. /// 2D point.
  32. class Point2
  33. {
  34. public:
  35. int32_t x, y;
  36. public:
  37. Point2(); //!< Constructor, does nothing for efficiency
  38. Point2(int32_t nx, int32_t ny); //! Constructs from two components
  39. Point2(const int32_t a[2]); //! Constructs from the array
  40. Point2(const Point2& a); //! Copy constructor
  41. ~Point2(); //! Destructor
  42. int32_t operator[](uint32_t i) const; //! Random access by index
  43. int32_t& operator[](uint32_t i); //! Random access by index
  44. Point2 operator+(const Point2& a) const; //! Addition
  45. Point2& operator+=(const Point2& a); //! Addition
  46. Point2 operator-(const Point2& a) const; //! Subtraction
  47. Point2& operator-=(const Point2& a); //! Subtraction
  48. Point2 operator*(int32_t k) const; //! Multiplication by scalar
  49. Point2& operator*=(int32_t k); //! Multiplication by scalar
  50. Point2 operator/(int32_t k) const; //! Division by scalar
  51. Point2& operator/=(int32_t k); //! Division by scalar
  52. int32_t dot(const Point2& a); //! dot product
  53. friend Point2 operator*(int32_t k, const Point2& a); //! For simmetry
  54. bool operator==(const Point2& other) const; //! Equality operator
  55. bool operator!=(const Point2& other) const; //! Disequality operator
  56. bool operator<(const Point2& other) const; //! Returns whether all the components of this point32_t are smaller than all of the "other" point32_t
  57. bool operator>(const Point2& other) const; //! Returns whether all the components of this point32_t are greater than all of the "other" point32_t
  58. float length() const; //! Returns the point32_t's length
  59. int32_t squared_length() const; //! Returns the point32_t's squared length
  60. void negate(); //! Negates the point32_t (i.e. builds the inverse)
  61. float get_distance_to(const Point2& a); //!< Returns the distance
  62. float get_angle_between(const Point2& a); //!< Returns the angle in radians
  63. Point2 operator-() const; //! Negates the point32_t (i.e. builds the inverse)
  64. void zero(); //! Builds the zero point32_t
  65. int32_t* to_int_ptr(); //! Returns the point32_ter to the point32_t's data
  66. const int32_t* to_int_ptr() const; //! Returns the point32_ter to the point32_t's data
  67. Vec2 to_vec2() const; //! Returns a vector from this point32_t
  68. Vec3 to_vec3() const; //! Returns a vector from this point32_t
  69. static const Point2 ZERO;
  70. static const Point2 ONE;
  71. static const Point2 XAXIS;
  72. static const Point2 YAXIS;
  73. };
  74. //-----------------------------------------------------------------------------
  75. inline Point2::Point2() : x(0), y(0)
  76. {
  77. }
  78. //-----------------------------------------------------------------------------
  79. inline Point2::Point2(int32_t nx, int32_t ny) : x(nx), y(ny)
  80. {
  81. }
  82. //-----------------------------------------------------------------------------
  83. inline Point2::Point2(const int32_t a[2]) : x(a[0]), y(a[1])
  84. {
  85. }
  86. //-----------------------------------------------------------------------------
  87. inline Point2::Point2(const Point2& a) : x(a.x), y(a.y)
  88. {
  89. }
  90. //-----------------------------------------------------------------------------
  91. inline Point2::~Point2()
  92. {
  93. }
  94. //-----------------------------------------------------------------------------
  95. inline int32_t Point2::operator[](uint32_t i) const
  96. {
  97. CE_ASSERT(i < 2, "Index must be < 2");
  98. return (&x)[i];
  99. }
  100. //-----------------------------------------------------------------------------
  101. inline int32_t& Point2::operator[](uint32_t i)
  102. {
  103. CE_ASSERT(i < 2, "Index must be < 2");
  104. return (&x)[i];
  105. }
  106. //-----------------------------------------------------------------------------
  107. inline Point2 Point2::operator+(const Point2& a) const
  108. {
  109. return Point2(x + a.x, y + a.y);
  110. }
  111. //-----------------------------------------------------------------------------
  112. inline Point2& Point2::operator+=(const Point2& a)
  113. {
  114. x += a.x;
  115. y += a.y;
  116. return *this;
  117. }
  118. //-----------------------------------------------------------------------------
  119. inline Point2 Point2::operator-(const Point2& a) const
  120. {
  121. return Point2(x - a.x, y - a.y);
  122. }
  123. //-----------------------------------------------------------------------------
  124. inline Point2& Point2::operator-=(const Point2& a)
  125. {
  126. x -= a.x;
  127. y -= a.y;
  128. return *this;
  129. }
  130. //-----------------------------------------------------------------------------
  131. inline Point2 Point2::operator*(int32_t k) const
  132. {
  133. return Point2(x * k, y * k);
  134. }
  135. //-----------------------------------------------------------------------------
  136. inline Point2& Point2::operator*=(int32_t k)
  137. {
  138. x *= k;
  139. y *= k;
  140. return *this;
  141. }
  142. //-----------------------------------------------------------------------------
  143. inline Point2 Point2::operator/(int32_t k) const
  144. {
  145. CE_ASSERT(k != 0, "Division by zero");
  146. return Point2(x / k, y / k);
  147. }
  148. //-----------------------------------------------------------------------------
  149. inline Point2& Point2::operator/=(int32_t k)
  150. {
  151. CE_ASSERT(k != 0, "Division by zero");
  152. x /= k;
  153. y /= k;
  154. return *this;
  155. }
  156. //-----------------------------------------------------------------------------
  157. inline int32_t Point2::dot(const Point2& a)
  158. {
  159. return x * a.x + y * a.y;
  160. }
  161. //-----------------------------------------------------------------------------
  162. inline Point2 operator*(int32_t k, const Point2& a)
  163. {
  164. return a * k;
  165. }
  166. //-----------------------------------------------------------------------------
  167. inline bool Point2::operator==(const Point2& other) const
  168. {
  169. return x == other.x && y == other.y;
  170. }
  171. //-----------------------------------------------------------------------------
  172. inline bool Point2::operator!=(const Point2& other) const
  173. {
  174. return x != other.x || y != other.y;
  175. }
  176. //-----------------------------------------------------------------------------
  177. inline bool Point2::operator<(const Point2& other) const
  178. {
  179. return ((x < other.x) && (y < other.y));
  180. }
  181. //-----------------------------------------------------------------------------
  182. inline bool Point2::operator>(const Point2& other) const
  183. {
  184. return ((x > other.x) && (y > other.y));
  185. }
  186. //-----------------------------------------------------------------------------
  187. inline float Point2::length() const
  188. {
  189. return math::acos((float)(x * x + y * y));
  190. }
  191. //-----------------------------------------------------------------------------
  192. inline int32_t Point2::squared_length() const
  193. {
  194. return x * x + y * y;
  195. }
  196. //-----------------------------------------------------------------------------
  197. inline void Point2::negate()
  198. {
  199. x = -x;
  200. y = -y;
  201. }
  202. //-----------------------------------------------------------------------------
  203. inline float Point2::get_distance_to(const Point2& a)
  204. {
  205. return (*this - a).length();
  206. }
  207. //-----------------------------------------------------------------------------
  208. inline float Point2::get_angle_between(const Point2& a)
  209. {
  210. return math::acos(this->dot(a) / (this->length() * a.length()));
  211. }
  212. //-----------------------------------------------------------------------------
  213. inline Point2 Point2::operator-() const
  214. {
  215. return Point2(-x, -y);
  216. }
  217. //-----------------------------------------------------------------------------
  218. inline void Point2::zero()
  219. {
  220. x = y = 0;
  221. }
  222. //-----------------------------------------------------------------------------
  223. inline int32_t* Point2::to_int_ptr()
  224. {
  225. return &x;
  226. }
  227. //-----------------------------------------------------------------------------
  228. inline const int32_t* Point2::to_int_ptr() const
  229. {
  230. return &x;
  231. }
  232. //-----------------------------------------------------------------------------
  233. inline Vec2 Point2::to_vec2() const
  234. {
  235. return Vec2((float)x, (float)y);
  236. }
  237. //-----------------------------------------------------------------------------
  238. inline Vec3 Point2::to_vec3() const
  239. {
  240. return Vec3((float)x, (float)y, 0.0);
  241. }
  242. } // namespace crown