CCGeometry.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  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. /**
  23. * cc.Point is the class for point object, please do not use its constructor to create points, use cc.p() alias function instead.
  24. * @class cc.Point
  25. * @param {Number} x
  26. * @param {Number} y
  27. * @see cc.p
  28. */
  29. cc.Point = function (x, y) {
  30. this.x = x || 0;
  31. this.y = y || 0;
  32. };
  33. /**
  34. * Helper function that creates a cc.Point.
  35. * @function
  36. * @param {Number|cc.Point} x a Number or a size object
  37. * @param {Number} y
  38. * @return {cc.Point}
  39. * @example
  40. * var point1 = cc.p();
  41. * var point2 = cc.p(100, 100);
  42. * var point3 = cc.p(point2);
  43. * var point4 = cc.p({x: 100, y: 100});
  44. */
  45. cc.p = function (x, y) {
  46. // This can actually make use of "hidden classes" in JITs and thus decrease
  47. // memory usage and overall performance drastically
  48. // return cc.p(x, y);
  49. // but this one will instead flood the heap with newly allocated hash maps
  50. // giving little room for optimization by the JIT,
  51. // note: we have tested this item on Chrome and firefox, it is faster than cc.p(x, y)
  52. if (x == undefined)
  53. return {x: 0, y: 0};
  54. if (y == undefined)
  55. return {x: x.x, y: x.y};
  56. return {x: x, y: y};
  57. };
  58. /**
  59. * Check whether a point's value equals to another
  60. * @function
  61. * @param {cc.Point} point1
  62. * @param {cc.Point} point2
  63. * @return {Boolean}
  64. */
  65. cc.pointEqualToPoint = function (point1, point2) {
  66. return point1 && point2 && (point1.x === point2.x) && (point1.y === point2.y);
  67. };
  68. /**
  69. * cc.Size is the class for size object, please do not use its constructor to create sizes, use cc.size() alias function instead.
  70. * @class cc.Size
  71. * @param {Number} width
  72. * @param {Number} height
  73. * @see cc.size
  74. */
  75. cc.Size = function (width, height) {
  76. this.width = width || 0;
  77. this.height = height || 0;
  78. };
  79. /**
  80. * Helper function that creates a cc.Size.
  81. * @function
  82. * @param {Number|cc.Size} w width or a size object
  83. * @param {Number} h height
  84. * @return {cc.Size}
  85. * @example
  86. * var size1 = cc.size();
  87. * var size2 = cc.size(100,100);
  88. * var size3 = cc.size(size2);
  89. * var size4 = cc.size({width: 100, height: 100});
  90. */
  91. cc.size = function (w, h) {
  92. // This can actually make use of "hidden classes" in JITs and thus decrease
  93. // memory usage and overall performance drastically
  94. //return cc.size(w, h);
  95. // but this one will instead flood the heap with newly allocated hash maps
  96. // giving little room for optimization by the JIT
  97. // note: we have tested this item on Chrome and firefox, it is faster than cc.size(w, h)
  98. if (w === undefined)
  99. return {width: 0, height: 0};
  100. if (h === undefined)
  101. return {width: w.width, height: w.height};
  102. return {width: w, height: h};
  103. };
  104. /**
  105. * Check whether a point's value equals to another
  106. * @function
  107. * @param {cc.Size} size1
  108. * @param {cc.Size} size2
  109. * @return {Boolean}
  110. */
  111. cc.sizeEqualToSize = function (size1, size2) {
  112. return (size1 && size2 && (size1.width == size2.width) && (size1.height == size2.height));
  113. };
  114. /**
  115. * cc.Rect is the class for rect object, please do not use its constructor to create rects, use cc.rect() alias function instead.
  116. * @class cc.Rect
  117. * @param {Number} width
  118. * @param {Number} height
  119. * @see cc.rect
  120. */
  121. cc.Rect = function (x, y, width, height) {
  122. this.x = x||0;
  123. this.y = y||0;
  124. this.width = width||0;
  125. this.height = height||0;
  126. };
  127. /**
  128. * Helper function that creates a cc.Rect.
  129. * @function
  130. * @param {Number|cc.Rect} x a number or a rect object
  131. * @param {Number} y
  132. * @param {Number} w
  133. * @param {Number} h
  134. * @returns {cc.Rect}
  135. * @example
  136. * var rect1 = cc.rect();
  137. * var rect2 = cc.rect(100,100,100,100);
  138. * var rect3 = cc.rect(rect2);
  139. * var rect4 = cc.rect({x: 100, y: 100, width: 100, height: 100});
  140. */
  141. cc.rect = function (x, y, w, h) {
  142. if (x === undefined)
  143. return {x: 0, y: 0, width: 0, height: 0};
  144. if (y === undefined)
  145. return {x: x.x, y: x.y, width: x.width, height: x.height};
  146. return {x: x, y: y, width: w, height: h };
  147. };
  148. /**
  149. * Check whether a rect's value equals to another
  150. * @function
  151. * @param {cc.Rect} rect1
  152. * @param {cc.Rect} rect2
  153. * @return {Boolean}
  154. */
  155. cc.rectEqualToRect = function (rect1, rect2) {
  156. return rect1 && rect2 && (rect1.x === rect2.x) && (rect1.y === rect2.y) && (rect1.width === rect2.width) && (rect1.height === rect2.height);
  157. };
  158. cc._rectEqualToZero = function(rect){
  159. return rect && (rect.x === 0) && (rect.y === 0) && (rect.width === 0) && (rect.height === 0);
  160. };
  161. /**
  162. * Check whether the rect1 contains rect2
  163. * @function
  164. * @param {cc.Rect} rect1
  165. * @param {cc.Rect} rect2
  166. * @return {Boolean}
  167. */
  168. cc.rectContainsRect = function (rect1, rect2) {
  169. if (!rect1 || !rect2)
  170. return false;
  171. return !((rect1.x >= rect2.x) || (rect1.y >= rect2.y) ||
  172. ( rect1.x + rect1.width <= rect2.x + rect2.width) ||
  173. ( rect1.y + rect1.height <= rect2.y + rect2.height));
  174. };
  175. /**
  176. * Returns the rightmost x-value of a rect
  177. * @function
  178. * @param {cc.Rect} rect
  179. * @return {Number} The rightmost x value
  180. */
  181. cc.rectGetMaxX = function (rect) {
  182. return (rect.x + rect.width);
  183. };
  184. /**
  185. * Return the midpoint x-value of a rect
  186. * @function
  187. * @param {cc.Rect} rect
  188. * @return {Number} The midpoint x value
  189. */
  190. cc.rectGetMidX = function (rect) {
  191. return (rect.x + rect.width / 2.0);
  192. };
  193. /**
  194. * Returns the leftmost x-value of a rect
  195. * @function
  196. * @param {cc.Rect} rect
  197. * @return {Number} The leftmost x value
  198. */
  199. cc.rectGetMinX = function (rect) {
  200. return rect.x;
  201. };
  202. /**
  203. * Return the topmost y-value of a rect
  204. * @function
  205. * @param {cc.Rect} rect
  206. * @return {Number} The topmost y value
  207. */
  208. cc.rectGetMaxY = function (rect) {
  209. return(rect.y + rect.height);
  210. };
  211. /**
  212. * Return the midpoint y-value of `rect'
  213. * @function
  214. * @param {cc.Rect} rect
  215. * @return {Number} The midpoint y value
  216. */
  217. cc.rectGetMidY = function (rect) {
  218. return rect.y + rect.height / 2.0;
  219. };
  220. /**
  221. * Return the bottommost y-value of a rect
  222. * @function
  223. * @param {cc.Rect} rect
  224. * @return {Number} The bottommost y value
  225. */
  226. cc.rectGetMinY = function (rect) {
  227. return rect.y;
  228. };
  229. /**
  230. * Check whether a rect contains a point
  231. * @function
  232. * @param {cc.Rect} rect
  233. * @param {cc.Point} point
  234. * @return {Boolean}
  235. */
  236. cc.rectContainsPoint = function (rect, point) {
  237. return (point.x >= cc.rectGetMinX(rect) && point.x <= cc.rectGetMaxX(rect) &&
  238. point.y >= cc.rectGetMinY(rect) && point.y <= cc.rectGetMaxY(rect)) ;
  239. };
  240. /**
  241. * Check whether a rect intersect with another
  242. * @function
  243. * @param {cc.Rect} rectA
  244. * @param {cc.Rect} rectB
  245. * @return {Boolean}
  246. */
  247. cc.rectIntersectsRect = function (ra, rb) {
  248. var maxax = ra.x + ra.width,
  249. maxay = ra.y + ra.height,
  250. maxbx = rb.x + rb.width,
  251. maxby = rb.y + rb.height;
  252. return !(maxax < rb.x || maxbx < ra.x || maxay < rb.y || maxby < ra.y);
  253. };
  254. /**
  255. * Check whether a rect overlaps another
  256. * @function
  257. * @param {cc.Rect} rectA
  258. * @param {cc.Rect} rectB
  259. * @return {Boolean}
  260. */
  261. cc.rectOverlapsRect = function (rectA, rectB) {
  262. return !((rectA.x + rectA.width < rectB.x) ||
  263. (rectB.x + rectB.width < rectA.x) ||
  264. (rectA.y + rectA.height < rectB.y) ||
  265. (rectB.y + rectB.height < rectA.y));
  266. };
  267. /**
  268. * Returns the smallest rectangle that contains the two source rectangles.
  269. * @function
  270. * @param {cc.Rect} rectA
  271. * @param {cc.Rect} rectB
  272. * @return {cc.Rect}
  273. */
  274. cc.rectUnion = function (rectA, rectB) {
  275. var rect = cc.rect(0, 0, 0, 0);
  276. rect.x = Math.min(rectA.x, rectB.x);
  277. rect.y = Math.min(rectA.y, rectB.y);
  278. rect.width = Math.max(rectA.x + rectA.width, rectB.x + rectB.width) - rect.x;
  279. rect.height = Math.max(rectA.y + rectA.height, rectB.y + rectB.height) - rect.y;
  280. return rect;
  281. };
  282. /**
  283. * Returns the overlapping portion of 2 rectangles
  284. * @function
  285. * @param {cc.Rect} rectA
  286. * @param {cc.Rect} rectB
  287. * @return {cc.Rect}
  288. */
  289. cc.rectIntersection = function (rectA, rectB) {
  290. var intersection = cc.rect(
  291. Math.max(cc.rectGetMinX(rectA), cc.rectGetMinX(rectB)),
  292. Math.max(cc.rectGetMinY(rectA), cc.rectGetMinY(rectB)),
  293. 0, 0);
  294. intersection.width = Math.min(cc.rectGetMaxX(rectA), cc.rectGetMaxX(rectB)) - cc.rectGetMinX(intersection);
  295. intersection.height = Math.min(cc.rectGetMaxY(rectA), cc.rectGetMaxY(rectB)) - cc.rectGetMinY(intersection);
  296. return intersection;
  297. };