CCGeometry.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga 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. //
  24. // POINT
  25. //
  26. //--------------------------------------------------------
  27. /**
  28. * @class
  29. * @param {Number|cc.Point} _x
  30. * @param {Number} _y
  31. * Constructor
  32. */
  33. cc.Point = function (_x, _y) {
  34. if(_x !== undefined && _y === undefined){
  35. this.x = _x.x;
  36. this.y = _x.y;
  37. } else {
  38. this.x = _x || 0;
  39. this.y = _y || 0;
  40. }
  41. };
  42. /**
  43. * @function
  44. * @param {Number} x
  45. * @param {Number} y
  46. * @return {cc.Point}
  47. * @deprecated
  48. */
  49. cc.PointMake = function (x, y) {
  50. cc.log("cc.PointMake will be deprecated sooner or later. Use cc.p instead.");
  51. return new cc.Point(x, y);
  52. };
  53. /**
  54. * Helper macro that creates a cc.Point.
  55. * @param {Number|cc.Point} x
  56. * @param {Number} y
  57. */
  58. cc.p = function (x, y) {
  59. // This can actually make use of "hidden classes" in JITs and thus decrease
  60. // memory usage and overall performance drastically
  61. // return new cc.Point(x, y);
  62. // but this one will instead flood the heap with newly allocated hash maps
  63. // giving little room for optimization by the JIT,
  64. // note: we have tested this item on Chrome and firefox, it is faster than new cc.Point(x, y)
  65. if (x === undefined)
  66. return {x: 0, y: 0};
  67. else if (y === undefined)
  68. return {x: x.x, y: x.y};
  69. else
  70. return {x: x || 0, y: y || 0};
  71. };
  72. // JSB compatbility: in JSB, cc._p reuses objects instead of creating new ones
  73. cc._p = cc.p;
  74. /**
  75. * The "left bottom" point -- equivalent to cc.p(0, 0).
  76. * @function
  77. * @return {cc.Point}
  78. */
  79. cc.PointZero = function () {
  80. return cc.p(0, 0);
  81. };
  82. /**
  83. * @function
  84. * @param {cc.Point} point1
  85. * @param {cc.Point} point2
  86. * @return {Boolean}
  87. */
  88. cc.pointEqualToPoint = function (point1, point2) {
  89. if (!point1 || !point2)
  90. return false;
  91. return ((point1.x === point2.x) && (point1.y === point2.y));
  92. };
  93. // deprecated
  94. //cc.Point.CCPointEqualToPoint = cc.pointEqualToPoint;
  95. //--------------------------------------------------------
  96. //
  97. // SIZE
  98. //
  99. //--------------------------------------------------------
  100. /**
  101. * @class
  102. * @param {Number|cc.Size} _width
  103. * @param {Number} _height
  104. * Constructor
  105. */
  106. cc.Size = function (_width, _height) {
  107. if(_width !== undefined && _height === undefined){
  108. this.width = _width.width;
  109. this.height = _width.height;
  110. } else {
  111. this.width = _width || 0;
  112. this.height = _height || 0;
  113. }
  114. };
  115. /**
  116. * @function
  117. * @param {Number} width
  118. * @param {Number} height
  119. * @return {cc.Size}
  120. * @deprecated
  121. */
  122. cc.SizeMake = function (width, height) {
  123. cc.log("cc.SizeMake will be deprecated sooner or later. Use cc.size instead.");
  124. return cc.size(width, height);
  125. };
  126. /**
  127. * @function
  128. * @param {Number|cc.Size} w width or a size object
  129. * @param {Number} h height
  130. * @return {cc.Size}
  131. */
  132. cc.size = function (w, h) {
  133. // This can actually make use of "hidden classes" in JITs and thus decrease
  134. // memory usage and overall performance drastically
  135. //return new cc.Size(w, h);
  136. // but this one will instead flood the heap with newly allocated hash maps
  137. // giving little room for optimization by the JIT
  138. // note: we have tested this item on Chrome and firefox, it is faster than new cc.Size(w, h)
  139. if (w === undefined)
  140. return {width: 0, height: 0};
  141. if (h === undefined)
  142. return { width: w.width, height: w.height};
  143. return { width: w || 0, height: h || 0};
  144. };
  145. // JSB compatbility: in JSB, cc._size reuses objects instead of creating new ones
  146. cc._size = cc.size;
  147. /**
  148. * The "zero" size -- equivalent to cc.size(0, 0).
  149. * @function
  150. * @return {cc.Size}
  151. */
  152. cc.SizeZero = function () {
  153. return cc.size(0, 0);
  154. };
  155. Object.defineProperties(cc, {
  156. POINT_ZERO:{
  157. get:function () {
  158. return cc.p();
  159. }
  160. },
  161. SIZE_ZERO:{
  162. get:function () {
  163. return cc.size(0,0);
  164. }
  165. },
  166. RECT_ZERO:{
  167. get:function () {
  168. return cc.rect(0, 0, 0, 0);
  169. }
  170. }
  171. });
  172. /**
  173. * @function
  174. * @param {cc.Size} size1
  175. * @param {cc.Size} size2
  176. * @return {Boolean}
  177. */
  178. cc.sizeEqualToSize = function (size1, size2) {
  179. if (!size1 || !size2)
  180. return false;
  181. return ((size1.width == size2.width) && (size1.height == size2.height));
  182. };
  183. // deprecated
  184. //cc.Size.CCSizeEqualToSize = cc.sizeEqualToSize;
  185. //--------------------------------------------------------
  186. //
  187. // RECT
  188. //
  189. //--------------------------------------------------------
  190. /**
  191. * @class
  192. * @param {Number|cc.Point|cc.Rect} [x1] a Number value as x or a cc.Point object as origin or a cc.Rect clone object
  193. * @param {Number|cc.Size} [y1] x1 a Number value as y or a cc.Size object as size
  194. * @param {Number} [width1]
  195. * @param {Number} [height1]
  196. * Constructor
  197. */
  198. cc.Rect = function (x1, y1, width1, height1) {
  199. var argLen =arguments.length;
  200. if(argLen === 4){
  201. this._origin = new cc.Point(x1 || 0, y1 || 0);
  202. this._size = new cc.Size(width1 || 0, height1 || 0);
  203. return;
  204. }
  205. if(argLen === 1) {
  206. this._origin = new cc.Point(x1._origin.x, x1._origin.y);
  207. this._size = new cc.Size(x1._size.width, x1._size.height);
  208. return;
  209. }
  210. if(argLen === 0) {
  211. this._origin = new cc.Point(0, 0);
  212. this._size = new cc.Size(0,0);
  213. return;
  214. }
  215. if(argLen === 2) {
  216. this._origin = new cc.Point(x1.x, x1.y);
  217. this._size = new cc.Size(y1.width,y1.height);
  218. return;
  219. }
  220. throw "unknown argument type";
  221. };
  222. /**
  223. * @function
  224. * @param {Number} x
  225. * @param {Number} y
  226. * @param {Number} width
  227. * @param {Number} height
  228. * @return {cc.Rect}
  229. */
  230. cc.RectMake = function (x, y, width, height) {
  231. cc.log("cc.RectMake will be deprecated sooner or later. Use cc.rect instead.");
  232. return cc.rect(x, y, width, height);
  233. };
  234. // backward compatible
  235. cc.rect = function (x, y, w, h) {
  236. var argLen =arguments.length;
  237. if(argLen === 0)
  238. return new cc.Rect(0,0,0,0);
  239. if(argLen === 1)
  240. return new cc.Rect(x.x, x.y, x.width, x.height);
  241. if(argLen === 2)
  242. return new cc.Rect(x.x, x.y, y.width, y.height);
  243. if(argLen === 4)
  244. return new cc.Rect(x,y,w,h);
  245. throw "unknown argument type";
  246. };
  247. // JSB compatbility: in JSB, cc._rect reuses objects instead of creating new ones
  248. cc._rect = cc.rect;
  249. /**
  250. * The "zero" rectangle -- equivalent to cc.rect(0, 0, 0, 0).
  251. * @function
  252. * @return {cc.Rect}
  253. */
  254. cc.RectZero = function () {
  255. return cc.rect(0, 0, 0, 0);
  256. };
  257. /**
  258. * @function
  259. * @param {cc.Rect} rect1
  260. * @param {cc.Rect} rect2
  261. * @return {Boolean}
  262. */
  263. cc.rectEqualToRect = function (rect1, rect2) {
  264. if(!rect1 || !rect2)
  265. return false;
  266. return ((cc.pointEqualToPoint(rect1._origin, rect2._origin)) &&
  267. (cc.sizeEqualToSize(rect1._size, rect2._size)));
  268. };
  269. cc._rectEqualToZero = function(rect){
  270. if(!rect)
  271. return false;
  272. return (rect.x === 0) && (rect.y === 0) && (rect.width === 0) && (rect.height === 0);
  273. };
  274. /**
  275. * @function
  276. * @param {cc.Rect} rect1
  277. * @param {cc.Rect} rect2
  278. * @return {Boolean}
  279. */
  280. cc.rectContainsRect = function (rect1, rect2) {
  281. if (!rect1 || !rect2)
  282. return false;
  283. return !((rect1.x >= rect2.x) || (rect1.y >= rect2.y) ||
  284. ( rect1.x + rect1.width <= rect2.x + rect2.width) ||
  285. ( rect1.y + rect1.height <= rect2.y + rect2.height));
  286. };
  287. /**
  288. * return the rightmost x-value of 'rect'
  289. * @function
  290. * @param {cc.Rect} rect
  291. * @return {Number}
  292. */
  293. cc.rectGetMaxX = function (rect) {
  294. return (rect.x + rect.width);
  295. };
  296. /**
  297. * return the midpoint x-value of 'rect'
  298. * @function
  299. * @param {cc.Rect} rect
  300. * @return {Number}
  301. */
  302. cc.rectGetMidX = function (rect) {
  303. return (rect.x + rect.width / 2.0);
  304. };
  305. /**
  306. * return the leftmost x-value of 'rect'
  307. * @function
  308. * @param {cc.Rect} rect
  309. * @return {Number}
  310. */
  311. cc.rectGetMinX = function (rect) {
  312. return rect.x;
  313. };
  314. /**
  315. * Return the topmost y-value of `rect'
  316. * @function
  317. * @param {cc.Rect} rect
  318. * @return {Number}
  319. */
  320. cc.rectGetMaxY = function (rect) {
  321. return(rect.y + rect.height);
  322. };
  323. /**
  324. * Return the midpoint y-value of `rect'
  325. * @function
  326. * @param {cc.Rect} rect
  327. * @return {Number}
  328. */
  329. cc.rectGetMidY = function (rect) {
  330. return rect.y + rect.height / 2.0;
  331. };
  332. /**
  333. * Return the bottommost y-value of `rect'
  334. * @function
  335. * @param {cc.Rect} rect
  336. * @return {Number}
  337. */
  338. cc.rectGetMinY = function (rect) {
  339. return rect.y;
  340. };
  341. /**
  342. * @function
  343. * @param {cc.Rect} rect
  344. * @param {cc.Point} point
  345. * @return {Boolean}
  346. */
  347. cc.rectContainsPoint = function (rect, point) {
  348. return (point.x >= cc.rectGetMinX(rect) && point.x <= cc.rectGetMaxX(rect) &&
  349. point.y >= cc.rectGetMinY(rect) && point.y <= cc.rectGetMaxY(rect)) ;
  350. };
  351. /**
  352. * @function
  353. * @param {cc.Rect} rectA
  354. * @param {cc.Rect} rectB
  355. * @return {Boolean}
  356. */
  357. cc.rectIntersectsRect = function (rectA, rectB) {
  358. return !(cc.rectGetMaxX(rectA) < cc.rectGetMinX(rectB) ||
  359. cc.rectGetMaxX(rectB) < cc.rectGetMinX(rectA) ||
  360. cc.rectGetMaxY(rectA) < cc.rectGetMinY(rectB) ||
  361. cc.rectGetMaxY(rectB) < cc.rectGetMinY(rectA));
  362. };
  363. /**
  364. * @function
  365. * @param {cc.Rect} rectA
  366. * @param {cc.Rect} rectB
  367. * @return {Boolean}
  368. */
  369. cc.rectOverlapsRect = function (rectA, rectB) {
  370. return !((rectA.x + rectA.width < rectB.x) ||
  371. (rectB.x + rectB.width < rectA.x) ||
  372. (rectA.y + rectA.height < rectB.y) ||
  373. (rectB.y + rectB.height < rectA.y));
  374. };
  375. /**
  376. * Returns the smallest rectangle that contains the two source rectangles.
  377. * @function
  378. * @param {cc.Rect} rectA
  379. * @param {cc.Rect} rectB
  380. * @return {cc.Rect}
  381. */
  382. cc.rectUnion = function (rectA, rectB) {
  383. var rect = cc.rect(0, 0, 0, 0);
  384. rect.x = Math.min(rectA.x, rectB.x);
  385. rect.y = Math.min(rectA.y, rectB.y);
  386. rect.width = Math.max(rectA.x + rectA.width, rectB.x + rectB.width) - rect.x;
  387. rect.height = Math.max(rectA.y + rectA.height, rectB.y + rectB.height) - rect.y;
  388. return rect;
  389. };
  390. /**
  391. * Returns the overlapping portion of 2 rectangles
  392. * @function
  393. * @param {cc.Rect} rectA
  394. * @param {cc.Rect} rectB
  395. * @return {cc.Rect}
  396. */
  397. cc.rectIntersection = function (rectA, rectB) {
  398. var intersection = cc.rect(
  399. Math.max(cc.rectGetMinX(rectA), cc.rectGetMinX(rectB)),
  400. Math.max(cc.rectGetMinY(rectA), cc.rectGetMinY(rectB)),
  401. 0, 0);
  402. intersection.width = Math.min(cc.rectGetMaxX(rectA), cc.rectGetMaxX(rectB)) - cc.rectGetMinX(intersection);
  403. intersection.height = Math.min(cc.rectGetMaxY(rectA), cc.rectGetMaxY(rectB)) - cc.rectGetMinY(intersection);
  404. return intersection;
  405. };
  406. //
  407. // Rect JSB compatibility
  408. // JSB uses:
  409. // rect.x, rect.y, rect.width and rect.height
  410. // while HTML5 uses:
  411. // rect.origin, rect.size
  412. //
  413. cc.Rect.prototype.getX = function() {
  414. return this._origin.x;
  415. };
  416. cc.Rect.prototype.setX = function(x) {
  417. this._origin.x = x;
  418. };
  419. cc.Rect.prototype.getY = function() {
  420. return this._origin.y;
  421. };
  422. cc.Rect.prototype.setY = function(y) {
  423. this._origin.y = y;
  424. };
  425. cc.Rect.prototype.getWidth = function(){
  426. return this._size.width;
  427. };
  428. cc.Rect.prototype.setWidth = function(w){
  429. this._size.width = w;
  430. };
  431. cc.Rect.prototype.getHeight = function(){
  432. return this._size.height;
  433. };
  434. cc.Rect.prototype.setHeight = function(h){
  435. this._size.height = h;
  436. };
  437. Object.defineProperties(cc.Rect.prototype,
  438. {
  439. "x": {
  440. get: function () {
  441. return this.getX();
  442. },
  443. set: function (newValue) {
  444. this.setX(newValue);
  445. },
  446. enumerable: true,
  447. configurable: true
  448. },
  449. "y": {
  450. get: function () {
  451. return this.getY();
  452. },
  453. set: function (newValue) {
  454. this.setY(newValue);
  455. },
  456. enumerable: true,
  457. configurable: true
  458. },
  459. "width": {
  460. get: function () {
  461. return this.getWidth();
  462. },
  463. set: function (newValue) {
  464. this.setWidth(newValue);
  465. },
  466. enumerable: true,
  467. configurable: true
  468. },
  469. "height": {
  470. get: function () {
  471. return this.getHeight();
  472. },
  473. set: function (newValue) {
  474. this.setHeight(newValue);
  475. },
  476. enumerable: true,
  477. configurable: true
  478. }
  479. }
  480. );
  481. // Deprecated
  482. /*cc.Rect.CCRectEqualToRect = cc.rectEqualToRect;
  483. cc.Rect.CCRectContainsRect = cc.rectContainsRect;
  484. cc.Rect.CCRectGetMaxX = cc.rectGetMaxX;
  485. cc.Rect.CCRectGetMidX = cc.rectGetMidX;
  486. cc.Rect.CCRectGetMinX = cc.rectGetMinX;
  487. cc.Rect.CCRectGetMaxY = cc.rectGetMaxY;
  488. cc.Rect.CCRectGetMidY = cc.rectGetMidY;
  489. cc.Rect.CCRectGetMinY = cc.rectGetMinY;
  490. cc.Rect.CCRectContainsPoint = cc.rectContainsPoint;
  491. cc.Rect.CCRectIntersectsRect = cc.rectIntersectsRect;
  492. cc.Rect.CCRectUnion = cc.rectUnion;
  493. cc.Rect.CCRectIntersection = cc.rectIntersection;*/