Vec2.inl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. Original file from GamePlay3D: http://gameplay3d.org
  13. This file was modified to fit the cocos2d-x project
  14. */
  15. #include "math/Vec2.h"
  16. #include <float.h>
  17. NS_CC_MATH_BEGIN
  18. inline Vec2::Vec2()
  19. : x(0.0f), y(0.0f)
  20. {
  21. }
  22. inline Vec2::Vec2(float xx, float yy)
  23. : x(xx), y(yy)
  24. {
  25. }
  26. inline Vec2::Vec2(const float* array)
  27. {
  28. set(array);
  29. }
  30. inline Vec2::Vec2(const Vec2& p1, const Vec2& p2)
  31. {
  32. set(p1, p2);
  33. }
  34. inline Vec2::Vec2(const Vec2& copy)
  35. {
  36. set(copy);
  37. }
  38. inline Vec2::~Vec2()
  39. {
  40. }
  41. inline bool Vec2::isZero() const
  42. {
  43. return x == 0.0f && y == 0.0f;
  44. }
  45. bool Vec2::isOne() const
  46. {
  47. return x == 1.0f && y == 1.0f;
  48. }
  49. inline void Vec2::add(const Vec2& v)
  50. {
  51. x += v.x;
  52. y += v.y;
  53. }
  54. inline float Vec2::distanceSquared(const Vec2& v) const
  55. {
  56. float dx = v.x - x;
  57. float dy = v.y - y;
  58. return (dx * dx + dy * dy);
  59. }
  60. inline float Vec2::dot(const Vec2& v) const
  61. {
  62. return (x * v.x + y * v.y);
  63. }
  64. inline float Vec2::lengthSquared() const
  65. {
  66. return (x * x + y * y);
  67. }
  68. inline void Vec2::negate()
  69. {
  70. x = -x;
  71. y = -y;
  72. }
  73. inline void Vec2::scale(float scalar)
  74. {
  75. x *= scalar;
  76. y *= scalar;
  77. }
  78. inline void Vec2::scale(const Vec2& scale)
  79. {
  80. x *= scale.x;
  81. y *= scale.y;
  82. }
  83. inline void Vec2::set(float xx, float yy)
  84. {
  85. this->x = xx;
  86. this->y = yy;
  87. }
  88. inline void Vec2::set(const Vec2& v)
  89. {
  90. this->x = v.x;
  91. this->y = v.y;
  92. }
  93. inline void Vec2::set(const Vec2& p1, const Vec2& p2)
  94. {
  95. x = p2.x - p1.x;
  96. y = p2.y - p1.y;
  97. }
  98. void Vec2::setZero()
  99. {
  100. x = y = 0.0f;
  101. }
  102. inline void Vec2::subtract(const Vec2& v)
  103. {
  104. x -= v.x;
  105. y -= v.y;
  106. }
  107. inline void Vec2::smooth(const Vec2& target, float elapsedTime, float responseTime)
  108. {
  109. if (elapsedTime > 0)
  110. {
  111. *this += (target - *this) * (elapsedTime / (elapsedTime + responseTime));
  112. }
  113. }
  114. inline Vec2 Vec2::operator+(const Vec2& v) const
  115. {
  116. Vec2 result(*this);
  117. result.add(v);
  118. return result;
  119. }
  120. inline Vec2& Vec2::operator+=(const Vec2& v)
  121. {
  122. add(v);
  123. return *this;
  124. }
  125. inline Vec2 Vec2::operator-(const Vec2& v) const
  126. {
  127. Vec2 result(*this);
  128. result.subtract(v);
  129. return result;
  130. }
  131. inline Vec2& Vec2::operator-=(const Vec2& v)
  132. {
  133. subtract(v);
  134. return *this;
  135. }
  136. inline Vec2 Vec2::operator-() const
  137. {
  138. Vec2 result(*this);
  139. result.negate();
  140. return result;
  141. }
  142. inline Vec2 Vec2::operator*(float s) const
  143. {
  144. Vec2 result(*this);
  145. result.scale(s);
  146. return result;
  147. }
  148. inline Vec2& Vec2::operator*=(float s)
  149. {
  150. scale(s);
  151. return *this;
  152. }
  153. inline Vec2 Vec2::operator/(const float s) const
  154. {
  155. return Vec2(this->x / s, this->y / s);
  156. }
  157. inline bool Vec2::operator<(const Vec2& v) const
  158. {
  159. if (x == v.x)
  160. {
  161. return y < v.y;
  162. }
  163. return x < v.x;
  164. }
  165. inline bool Vec2::operator>(const Vec2& v) const
  166. {
  167. if (x == v.x)
  168. {
  169. return y > v.y;
  170. }
  171. return x > v.x;
  172. }
  173. inline bool Vec2::operator==(const Vec2& v) const
  174. {
  175. return x==v.x && y==v.y;
  176. }
  177. inline bool Vec2::operator!=(const Vec2& v) const
  178. {
  179. return x!=v.x || y!=v.y;
  180. }
  181. inline Vec2 operator*(float x, const Vec2& v)
  182. {
  183. Vec2 result(v);
  184. result.scale(x);
  185. return result;
  186. }
  187. void Vec2::setPoint(float xx, float yy)
  188. {
  189. this->x = xx;
  190. this->y = yy;
  191. }
  192. /* --- --- --- --- Add by XuJJ start --- --- --- --- */
  193. inline float Dot (const Vec2& lhs, const Vec2& rhs) { return lhs.x * rhs.x + lhs.y * rhs.y; }
  194. inline float SqrMagnitude (const Vec2& inV) { return Dot (inV, inV); }
  195. inline float Magnitude (const Vec2& inV) { return sqrt(Dot (inV, inV)); }
  196. // Normalizes a vector, asserts if the vector can be normalized
  197. inline Vec2 Normalize (const Vec2& inV) { return inV / Magnitude (inV); }
  198. // Normalizes a vector, returns default vector if it can't be normalized
  199. inline Vec2 NormalizeSafe (const Vec2& inV, const Vec2& defaultV = Vec2::ZERO);
  200. inline Vec2 NormalizeSafe (const Vec2& inV, const Vec2& defaultV)
  201. {
  202. float mag = Magnitude (inV);
  203. if (mag > FLT_EPSILON)
  204. return inV / Magnitude (inV);
  205. else
  206. return defaultV;
  207. }
  208. /// Returns the abs of every component of the vector
  209. inline Vec2 Abs (const Vec2& v) { return Vec2 (abs (v.x), abs (v.y)); }
  210. /* --- --- --- --- Add by XuJJ end --- --- --- --- */
  211. NS_CC_MATH_END