Vector2.cpp 4.5 KB

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