CCGeometry.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "math/CCGeometry.h"
  23. #include <algorithm>
  24. #include <cmath>
  25. #include "base/ccMacros.h"
  26. // implementation of Vec2
  27. NS_CC_BEGIN
  28. // implementation of Size
  29. Size::Size(void) : width(0), height(0)
  30. {
  31. }
  32. Size::Size(float w, float h) : width(w), height(h)
  33. {
  34. }
  35. Size::Size(const Size& other) : width(other.width), height(other.height)
  36. {
  37. }
  38. Size::Size(const Vec2& point) : width(point.x), height(point.y)
  39. {
  40. }
  41. Size& Size::operator= (const Size& other)
  42. {
  43. setSize(other.width, other.height);
  44. return *this;
  45. }
  46. Size& Size::operator= (const Vec2& point)
  47. {
  48. setSize(point.x, point.y);
  49. return *this;
  50. }
  51. Size Size::operator+(const Size& right) const
  52. {
  53. return Size(this->width + right.width, this->height + right.height);
  54. }
  55. Size Size::operator-(const Size& right) const
  56. {
  57. return Size(this->width - right.width, this->height - right.height);
  58. }
  59. Size Size::operator*(float a) const
  60. {
  61. return Size(this->width * a, this->height * a);
  62. }
  63. Size Size::operator/(float a) const
  64. {
  65. CCASSERT(a!=0, "CCSize division by 0.");
  66. return Size(this->width / a, this->height / a);
  67. }
  68. void Size::setSize(float w, float h)
  69. {
  70. this->width = w;
  71. this->height = h;
  72. }
  73. bool Size::equals(const Size& target) const
  74. {
  75. return (std::abs(this->width - target.width) < FLT_EPSILON)
  76. && (std::abs(this->height - target.height) < FLT_EPSILON);
  77. }
  78. const Size Size::ZERO = Size(0, 0);
  79. // implementation of Rect
  80. Rect::Rect(void)
  81. {
  82. setRect(0.0f, 0.0f, 0.0f, 0.0f);
  83. }
  84. Rect::Rect(float x, float y, float width, float height)
  85. {
  86. setRect(x, y, width, height);
  87. }
  88. Rect::Rect(const Vec2& pos, const Size& dimension)
  89. {
  90. setRect(pos.x, pos.y, dimension.width, dimension.height);
  91. }
  92. Rect::Rect(const Rect& other)
  93. {
  94. setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
  95. }
  96. Rect& Rect::operator= (const Rect& other)
  97. {
  98. setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
  99. return *this;
  100. }
  101. void Rect::setRect(float x, float y, float width, float height)
  102. {
  103. // CGRect can support width<0 or height<0
  104. // CCASSERT(width >= 0.0f && height >= 0.0f, "width and height of Rect must not less than 0.");
  105. origin.x = x;
  106. origin.y = y;
  107. size.width = width;
  108. size.height = height;
  109. }
  110. bool Rect::equals(const Rect& rect) const
  111. {
  112. return (origin.equals(rect.origin) &&
  113. size.equals(rect.size));
  114. }
  115. float Rect::getMaxX() const
  116. {
  117. return origin.x + size.width;
  118. }
  119. float Rect::getMidX() const
  120. {
  121. return origin.x + size.width / 2.0f;
  122. }
  123. float Rect::getMinX() const
  124. {
  125. return origin.x;
  126. }
  127. float Rect::getMaxY() const
  128. {
  129. return origin.y + size.height;
  130. }
  131. float Rect::getMidY() const
  132. {
  133. return origin.y + size.height / 2.0f;
  134. }
  135. float Rect::getMinY() const
  136. {
  137. return origin.y;
  138. }
  139. bool Rect::containsPoint(const Vec2& point) const
  140. {
  141. bool bRet = false;
  142. if (point.x >= getMinX() && point.x <= getMaxX()
  143. && point.y >= getMinY() && point.y <= getMaxY())
  144. {
  145. bRet = true;
  146. }
  147. return bRet;
  148. }
  149. bool Rect::intersectsRect(const Rect& rect) const
  150. {
  151. return !( getMaxX() < rect.getMinX() ||
  152. rect.getMaxX() < getMinX() ||
  153. getMaxY() < rect.getMinY() ||
  154. rect.getMaxY() < getMinY());
  155. }
  156. bool Rect::intersectsCircle(const Vec2& center, float radius) const
  157. {
  158. Vec2 rectangleCenter((origin.x + size.width / 2),
  159. (origin.y + size.height / 2));
  160. float w = size.width / 2;
  161. float h = size.height / 2;
  162. float dx = std::abs(center.x - rectangleCenter.x);
  163. float dy = std::abs(center.y - rectangleCenter.y);
  164. if (dx > (radius + w) || dy > (radius + h))
  165. {
  166. return false;
  167. }
  168. Vec2 circleDistance(std::abs(center.x - origin.x - w),
  169. std::abs(center.y - origin.y - h));
  170. if (circleDistance.x <= (w))
  171. {
  172. return true;
  173. }
  174. if (circleDistance.y <= (h))
  175. {
  176. return true;
  177. }
  178. float cornerDistanceSq = powf(circleDistance.x - w, 2) + powf(circleDistance.y - h, 2);
  179. return (cornerDistanceSq <= (powf(radius, 2)));
  180. }
  181. void Rect::merge(const Rect& rect)
  182. {
  183. float minX = std::min(getMinX(), rect.getMinX());
  184. float minY = std::min(getMinY(), rect.getMinY());
  185. float maxX = std::max(getMaxX(), rect.getMaxX());
  186. float maxY = std::max(getMaxY(), rect.getMaxY());
  187. setRect(minX, minY, maxX - minX, maxY - minY);
  188. }
  189. Rect Rect::unionWithRect(const Rect & rect) const
  190. {
  191. float thisLeftX = origin.x;
  192. float thisRightX = origin.x + size.width;
  193. float thisTopY = origin.y + size.height;
  194. float thisBottomY = origin.y;
  195. if (thisRightX < thisLeftX)
  196. {
  197. std::swap(thisRightX, thisLeftX); // This rect has negative width
  198. }
  199. if (thisTopY < thisBottomY)
  200. {
  201. std::swap(thisTopY, thisBottomY); // This rect has negative height
  202. }
  203. float otherLeftX = rect.origin.x;
  204. float otherRightX = rect.origin.x + rect.size.width;
  205. float otherTopY = rect.origin.y + rect.size.height;
  206. float otherBottomY = rect.origin.y;
  207. if (otherRightX < otherLeftX)
  208. {
  209. std::swap(otherRightX, otherLeftX); // Other rect has negative width
  210. }
  211. if (otherTopY < otherBottomY)
  212. {
  213. std::swap(otherTopY, otherBottomY); // Other rect has negative height
  214. }
  215. float combinedLeftX = std::min(thisLeftX, otherLeftX);
  216. float combinedRightX = std::max(thisRightX, otherRightX);
  217. float combinedTopY = std::max(thisTopY, otherTopY);
  218. float combinedBottomY = std::min(thisBottomY, otherBottomY);
  219. return Rect(combinedLeftX, combinedBottomY, combinedRightX - combinedLeftX, combinedTopY - combinedBottomY);
  220. }
  221. const Rect Rect::ZERO = Rect(0, 0, 0, 0);
  222. NS_CC_END