Vector2.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "Base.h"
  2. #include "Vector2.h"
  3. namespace gameplay
  4. {
  5. Vector2::Vector2()
  6. : x(0.0f), y(0.0f)
  7. {
  8. }
  9. Vector2::Vector2(float x, float y)
  10. {
  11. set(x, y);
  12. }
  13. Vector2::Vector2(float* array)
  14. {
  15. set(array);
  16. }
  17. Vector2::Vector2(const Vector2& p1, const Vector2& p2)
  18. {
  19. set(p1, p2);
  20. }
  21. Vector2::Vector2(const Vector2& copy)
  22. {
  23. set(copy);
  24. }
  25. Vector2::~Vector2()
  26. {
  27. }
  28. const Vector2& Vector2::zero()
  29. {
  30. static Vector2 value(0.0f, 0.0f);
  31. return value;
  32. }
  33. const Vector2& Vector2::one()
  34. {
  35. static Vector2 value(1.0f, 1.0f);
  36. return value;
  37. }
  38. const Vector2& Vector2::unitX()
  39. {
  40. static Vector2 value(1.0f, 0.0f);
  41. return value;
  42. }
  43. const Vector2& Vector2::unitY()
  44. {
  45. static Vector2 value(0.0f, 1.0f);
  46. return value;
  47. }
  48. bool Vector2::isZero() const
  49. {
  50. return x == 0.0f && y == 0.0f;
  51. }
  52. bool Vector2::isOne() const
  53. {
  54. return x == 1.0f && y == 1.0f;
  55. }
  56. float Vector2::angle(const Vector2& v1, const Vector2& v2)
  57. {
  58. float dz = v1.x * v2.y - v1.y * v2.x;
  59. return atan2f(fabsf(dz) + MATH_FLOAT_SMALL, dot(v1, v2));
  60. }
  61. void Vector2::add(const Vector2& v)
  62. {
  63. x += v.x;
  64. y += v.y;
  65. }
  66. void Vector2::add(const Vector2& v1, const Vector2& v2, Vector2* dst)
  67. {
  68. assert(dst);
  69. dst->x = v1.x + v2.x;
  70. dst->y = v1.y + v2.y;
  71. }
  72. void Vector2::clamp(const Vector2& min, const Vector2& max)
  73. {
  74. assert(!( min.x > max.x || min.y > max.y ));
  75. // Clamp the x value.
  76. if ( x < min.x )
  77. x = min.x;
  78. if ( x > max.x )
  79. x = max.x;
  80. // Clamp the y value.
  81. if ( y < min.y )
  82. y = min.y;
  83. if ( y > max.y )
  84. y = max.y;
  85. }
  86. void Vector2::clamp(const Vector2& v, const Vector2& min, const Vector2& max, Vector2* dst)
  87. {
  88. assert(dst);
  89. assert(!( min.x > max.x || min.y > max.y ));
  90. // Clamp the x value.
  91. dst->x = v.x;
  92. if ( dst->x < min.x )
  93. dst->x = min.x;
  94. if ( dst->x > max.x )
  95. dst->x = max.x;
  96. // Clamp the y value.
  97. dst->y = v.y;
  98. if ( dst->y < min.y )
  99. dst->y = min.y;
  100. if ( dst->y > max.y )
  101. dst->y = max.y;
  102. }
  103. float Vector2::distance(const Vector2& v) const
  104. {
  105. float dx = v.x - x;
  106. float dy = v.y - y;
  107. return sqrt(dx * dx + dy * dy);
  108. }
  109. float Vector2::distanceSquared(const Vector2& v) const
  110. {
  111. float dx = v.x - x;
  112. float dy = v.y - y;
  113. return (dx * dx + dy * dy);
  114. }
  115. float Vector2::dot(const Vector2& v)
  116. {
  117. return (x * v.x + y * v.y);
  118. }
  119. float Vector2::dot(const Vector2& v1, const Vector2& v2)
  120. {
  121. return (v1.x * v2.x + v1.y * v2.y);
  122. }
  123. float Vector2::length() const
  124. {
  125. return sqrt(x * x + y * y);
  126. }
  127. float Vector2::lengthSquared() const
  128. {
  129. return (x * x + y * y);
  130. }
  131. void Vector2::negate()
  132. {
  133. x = -x;
  134. y = -y;
  135. }
  136. void Vector2::normalize()
  137. {
  138. normalize(this);
  139. }
  140. void Vector2::normalize(Vector2* dst)
  141. {
  142. assert(dst);
  143. if (dst != this)
  144. {
  145. dst->x = x;
  146. dst->y = y;
  147. }
  148. float n = x * x + y * y;
  149. // Already normalized.
  150. if (n == 1.0f)
  151. return;
  152. n = sqrt(n);
  153. // Too close to zero.
  154. if (n < MATH_TOLERANCE)
  155. return;
  156. n = 1.0f / n;
  157. dst->x *= n;
  158. dst->y *= n;
  159. }
  160. void Vector2::scale(float scalar)
  161. {
  162. x *= scalar;
  163. y *= scalar;
  164. }
  165. void Vector2::scale(const Vector2& scale)
  166. {
  167. x *= scale.x;
  168. y *= scale.y;
  169. }
  170. void Vector2::rotate(const Vector2& point, float angle)
  171. {
  172. float sinAngle = sin(angle);
  173. float cosAngle = cos(angle);
  174. if (point.isZero())
  175. {
  176. float tempX = x * cosAngle - y * sinAngle;
  177. y = y * cosAngle + x * sinAngle;
  178. x = tempX;
  179. }
  180. else
  181. {
  182. float tempX = x - point.x;
  183. float tempY = y - point.y;
  184. x = tempX * cosAngle - tempY * sinAngle + point.x;
  185. y = tempY * cosAngle + tempX * sinAngle + point.y;
  186. }
  187. }
  188. void Vector2::set(float x, float y)
  189. {
  190. this->x = x;
  191. this->y = y;
  192. }
  193. void Vector2::set(float* array)
  194. {
  195. assert(array);
  196. x = array[0];
  197. y = array[1];
  198. }
  199. void Vector2::set(const Vector2& v)
  200. {
  201. this->x = v.x;
  202. this->y = v.y;
  203. }
  204. void Vector2::set(const Vector2& p1, const Vector2& p2)
  205. {
  206. x = p2.x - p1.x;
  207. y = p2.y - p1.y;
  208. }
  209. void Vector2::subtract(const Vector2& v)
  210. {
  211. x -= v.x;
  212. y -= v.y;
  213. }
  214. void Vector2::subtract(const Vector2& v1, const Vector2& v2, Vector2* dst)
  215. {
  216. assert(dst);
  217. dst->x = v1.x - v2.x;
  218. dst->y = v1.y - v2.y;
  219. }
  220. }