Vec2.inl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. NS_CC_MATH_BEGIN
  17. inline Vec2::Vec2()
  18. : x(0.0f), y(0.0f)
  19. {
  20. }
  21. inline Vec2::Vec2(float xx, float yy)
  22. : x(xx), y(yy)
  23. {
  24. }
  25. inline Vec2::Vec2(const float* array)
  26. {
  27. set(array);
  28. }
  29. inline Vec2::Vec2(const Vec2& p1, const Vec2& p2)
  30. {
  31. set(p1, p2);
  32. }
  33. inline Vec2::Vec2(const Vec2& copy)
  34. {
  35. set(copy);
  36. }
  37. inline Vec2::~Vec2()
  38. {
  39. }
  40. inline bool Vec2::isZero() const
  41. {
  42. return x == 0.0f && y == 0.0f;
  43. }
  44. bool Vec2::isOne() const
  45. {
  46. return x == 1.0f && y == 1.0f;
  47. }
  48. inline void Vec2::add(const Vec2& v)
  49. {
  50. x += v.x;
  51. y += v.y;
  52. }
  53. inline float Vec2::distanceSquared(const Vec2& v) const
  54. {
  55. float dx = v.x - x;
  56. float dy = v.y - y;
  57. return (dx * dx + dy * dy);
  58. }
  59. inline float Vec2::dot(const Vec2& v) const
  60. {
  61. return (x * v.x + y * v.y);
  62. }
  63. inline float Vec2::lengthSquared() const
  64. {
  65. return (x * x + y * y);
  66. }
  67. inline void Vec2::negate()
  68. {
  69. x = -x;
  70. y = -y;
  71. }
  72. inline void Vec2::scale(float scalar)
  73. {
  74. x *= scalar;
  75. y *= scalar;
  76. }
  77. inline void Vec2::scale(const Vec2& scale)
  78. {
  79. x *= scale.x;
  80. y *= scale.y;
  81. }
  82. inline void Vec2::set(float xx, float yy)
  83. {
  84. this->x = xx;
  85. this->y = yy;
  86. }
  87. inline void Vec2::set(const Vec2& v)
  88. {
  89. this->x = v.x;
  90. this->y = v.y;
  91. }
  92. inline void Vec2::set(const Vec2& p1, const Vec2& p2)
  93. {
  94. x = p2.x - p1.x;
  95. y = p2.y - p1.y;
  96. }
  97. void Vec2::setZero()
  98. {
  99. x = y = 0.0f;
  100. }
  101. inline void Vec2::subtract(const Vec2& v)
  102. {
  103. x -= v.x;
  104. y -= v.y;
  105. }
  106. inline void Vec2::smooth(const Vec2& target, float elapsedTime, float responseTime)
  107. {
  108. if (elapsedTime > 0)
  109. {
  110. *this += (target - *this) * (elapsedTime / (elapsedTime + responseTime));
  111. }
  112. }
  113. inline Vec2 Vec2::operator+(const Vec2& v) const
  114. {
  115. Vec2 result(*this);
  116. result.add(v);
  117. return result;
  118. }
  119. inline Vec2& Vec2::operator+=(const Vec2& v)
  120. {
  121. add(v);
  122. return *this;
  123. }
  124. inline Vec2 Vec2::operator-(const Vec2& v) const
  125. {
  126. Vec2 result(*this);
  127. result.subtract(v);
  128. return result;
  129. }
  130. inline Vec2& Vec2::operator-=(const Vec2& v)
  131. {
  132. subtract(v);
  133. return *this;
  134. }
  135. inline Vec2 Vec2::operator-() const
  136. {
  137. Vec2 result(*this);
  138. result.negate();
  139. return result;
  140. }
  141. inline Vec2 Vec2::operator*(float s) const
  142. {
  143. Vec2 result(*this);
  144. result.scale(s);
  145. return result;
  146. }
  147. inline Vec2& Vec2::operator*=(float s)
  148. {
  149. scale(s);
  150. return *this;
  151. }
  152. inline Vec2 Vec2::operator/(const float s) const
  153. {
  154. return Vec2(this->x / s, this->y / s);
  155. }
  156. inline bool Vec2::operator<(const Vec2& v) const
  157. {
  158. if (x == v.x)
  159. {
  160. return y < v.y;
  161. }
  162. return x < v.x;
  163. }
  164. inline bool Vec2::operator>(const Vec2& v) const
  165. {
  166. if (x == v.x)
  167. {
  168. return y > v.y;
  169. }
  170. return x > v.x;
  171. }
  172. inline bool Vec2::operator==(const Vec2& v) const
  173. {
  174. return x==v.x && y==v.y;
  175. }
  176. inline bool Vec2::operator!=(const Vec2& v) const
  177. {
  178. return x!=v.x || y!=v.y;
  179. }
  180. inline Vec2 operator*(float x, const Vec2& v)
  181. {
  182. Vec2 result(v);
  183. result.scale(x);
  184. return result;
  185. }
  186. void Vec2::setPoint(float xx, float yy)
  187. {
  188. this->x = xx;
  189. this->y = yy;
  190. }
  191. NS_CC_MATH_END