1
0

CCGeometry.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __MATH_CCGEOMETRY_H__
  22. #define __MATH_CCGEOMETRY_H__
  23. #include "platform/CCPlatformMacros.h"
  24. #include "base/ccMacros.h"
  25. #include "math/CCMath.h"
  26. /**
  27. * @addtogroup base
  28. * @{
  29. */
  30. NS_CC_BEGIN
  31. class CC_DLL Size
  32. {
  33. public:
  34. /**Width of the Size.*/
  35. float width;
  36. /**Height of the Size.*/
  37. float height;
  38. public:
  39. /**Conversion from Vec2 to Size.*/
  40. operator Vec2() const
  41. {
  42. return Vec2(width, height);
  43. }
  44. public:
  45. /**
  46. @{
  47. Constructor.
  48. @param width Width of the size.
  49. @param height Height of the size.
  50. @param other Copy constructor.
  51. @param point Conversion from a point.
  52. */
  53. Size();
  54. Size(float width, float height);
  55. Size(const Size& other);
  56. explicit Size(const Vec2& point);
  57. /**@}*/
  58. /**
  59. * @js NA
  60. * @lua NA
  61. */
  62. Size& operator= (const Size& other);
  63. /**
  64. * @js NA
  65. * @lua NA
  66. */
  67. Size& operator= (const Vec2& point);
  68. /**
  69. * @js NA
  70. * @lua NA
  71. */
  72. Size operator+(const Size& right) const;
  73. /**
  74. * @js NA
  75. * @lua NA
  76. */
  77. Size operator-(const Size& right) const;
  78. /**
  79. * @js NA
  80. * @lua NA
  81. */
  82. Size operator*(float a) const;
  83. /**
  84. * @js NA
  85. * @lua NA
  86. */
  87. Size operator/(float a) const;
  88. /**
  89. Set the width and height of Size.
  90. * @js NA
  91. * @lua NA
  92. */
  93. void setSize(float width, float height);
  94. /**
  95. Check if two size is the same.
  96. * @js NA
  97. */
  98. bool equals(const Size& target) const;
  99. /**Size(0,0).*/
  100. static const Size ZERO;
  101. };
  102. /**Rectangle area.*/
  103. class CC_DLL Rect
  104. {
  105. public:
  106. /**Low left point of rect.*/
  107. Vec2 origin;
  108. /**Width and height of the rect.*/
  109. Size size;
  110. public:
  111. /**
  112. Constructor an empty Rect.
  113. * @js NA
  114. */
  115. Rect();
  116. /**
  117. Constructor a rect.
  118. * @js NA
  119. */
  120. Rect(float x, float y, float width, float height);
  121. /**
  122. Constructor a rect.
  123. * @js NA
  124. */
  125. Rect(const Vec2& pos, const Size& dimension);
  126. /**
  127. Copy constructor.
  128. * @js NA
  129. * @lua NA
  130. */
  131. Rect(const Rect& other);
  132. /**
  133. * @js NA
  134. * @lua NA
  135. */
  136. Rect& operator= (const Rect& other);
  137. /**
  138. Set the x, y, width and height of Rect.
  139. * @js NA
  140. * @lua NA
  141. */
  142. void setRect(float x, float y, float width, float height);
  143. /**
  144. Get the left of the rect.
  145. * @js NA
  146. */
  147. float getMinX() const; /// return the leftmost x-value of current rect
  148. /**
  149. Get the X coordinate of center point.
  150. * @js NA
  151. */
  152. float getMidX() const; /// return the midpoint x-value of current rect
  153. /**
  154. Get the right of rect.
  155. * @js NA
  156. */
  157. float getMaxX() const; /// return the rightmost x-value of current rect
  158. /**
  159. Get the bottom of rect.
  160. * @js NA
  161. */
  162. float getMinY() const; /// return the bottommost y-value of current rect
  163. /**
  164. Get the Y coordinate of center point.
  165. * @js NA
  166. */
  167. float getMidY() const; /// return the midpoint y-value of current rect
  168. /**
  169. Get top of rect.
  170. * @js NA
  171. */
  172. float getMaxY() const; /// return the topmost y-value of current rect
  173. /**
  174. Compare two rects.
  175. * @js NA
  176. */
  177. bool equals(const Rect& rect) const;
  178. /**
  179. Check if the points is contained in the rect.
  180. * @js NA
  181. */
  182. bool containsPoint(const Vec2& point) const;
  183. /**
  184. Check the intersect status of two rects.
  185. * @js NA
  186. */
  187. bool intersectsRect(const Rect& rect) const;
  188. /**
  189. Check the intersect status of the rect and a circle.
  190. * @js NA
  191. */
  192. bool intersectsCircle(const Vec2& center, float radius) const;
  193. /**
  194. Get the min rect which can contain this and rect.
  195. * @js NA
  196. * @lua NA
  197. */
  198. Rect unionWithRect(const Rect & rect) const;
  199. /**Compute the min rect which can contain this and rect, assign it to this.*/
  200. void merge(const Rect& rect);
  201. /**An empty Rect.*/
  202. static const Rect ZERO;
  203. };
  204. NS_CC_END
  205. // end of base group
  206. /// @}
  207. #endif // __MATH_CCGEOMETRY_H__