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 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * cc.Point is the class for point object, please do not use its constructor to create points, use cc.p() alias function instead.
 29  * @class cc.Point
 30  * @param {Number} x
 31  * @param {Number} y
 32  * @see cc.p
 33  */
 34 cc.Point = function (x, y) {
 35     this.x = x || 0;
 36     this.y = y || 0;
 37 };
 38 
 39 /**
 40  * Helper function that creates a cc.Point.
 41  * @function
 42  * @param {Number|cc.Point} x a Number or a size object
 43  * @param {Number} y
 44  * @return {cc.Point}
 45  * @example
 46  * var point1 = cc.p();
 47  * var point2 = cc.p(100, 100);
 48  * var point3 = cc.p(point2);
 49  * var point4 = cc.p({x: 100, y: 100});
 50  */
 51 cc.p = function (x, y) {
 52     // This can actually make use of "hidden classes" in JITs and thus decrease
 53     // memory usage and overall performance drastically
 54     // return cc.p(x, y);
 55     // but this one will instead flood the heap with newly allocated hash maps
 56     // giving little room for optimization by the JIT,
 57     // note: we have tested this item on Chrome and firefox, it is faster than cc.p(x, y)
 58     if (x == undefined)
 59         return {x: 0, y: 0};
 60     if (y == undefined)
 61         return {x: x.x, y: x.y};
 62     return {x: x, y: y};
 63 };
 64 
 65 /**
 66  * Check whether a point's value equals to another
 67  * @function
 68  * @param {cc.Point} point1
 69  * @param {cc.Point} point2
 70  * @return {Boolean}
 71  */
 72 cc.pointEqualToPoint = function (point1, point2) {
 73     return point1 && point2 && (point1.x === point2.x) && (point1.y === point2.y);
 74 };
 75 
 76 
 77 /**
 78  * cc.Size is the class for size object, please do not use its constructor to create sizes, use cc.size() alias function instead.
 79  * @class cc.Size
 80  * @param {Number} width
 81  * @param {Number} height
 82  * @see cc.size
 83  */
 84 cc.Size = function (width, height) {
 85     this.width = width || 0;
 86     this.height = height || 0;
 87 };
 88 
 89 /**
 90  * Helper function that creates a cc.Size.
 91  * @function
 92  * @param {Number|cc.Size} w width or a size object
 93  * @param {Number} h height
 94  * @return {cc.Size}
 95  * @example
 96  * var size1 = cc.size();
 97  * var size2 = cc.size(100,100);
 98  * var size3 = cc.size(size2);
 99  * var size4 = cc.size({width: 100, height: 100});
100  */
101 cc.size = function (w, h) {
102     // This can actually make use of "hidden classes" in JITs and thus decrease
103     // memory usage and overall performance drastically
104     //return cc.size(w, h);
105     // but this one will instead flood the heap with newly allocated hash maps
106     // giving little room for optimization by the JIT
107     // note: we have tested this item on Chrome and firefox, it is faster than cc.size(w, h)
108     if (w === undefined)
109         return {width: 0, height: 0};
110     if (h === undefined)
111         return {width: w.width, height: w.height};
112     return {width: w, height: h};
113 };
114 
115 /**
116  * Check whether a point's value equals to another
117  * @function
118  * @param {cc.Size} size1
119  * @param {cc.Size} size2
120  * @return {Boolean}
121  */
122 cc.sizeEqualToSize = function (size1, size2) {
123     return (size1 && size2 && (size1.width == size2.width) && (size1.height == size2.height));
124 };
125 
126 
127 /**
128  * cc.Rect is the class for rect object, please do not use its constructor to create rects, use cc.rect() alias function instead.
129  * @class cc.Rect
130  * @param {Number} width
131  * @param {Number} height
132  * @see cc.rect
133  */
134 cc.Rect = function (x, y, width, height) {
135     this.x = x||0;
136     this.y = y||0;
137     this.width = width||0;
138     this.height = height||0;
139 };
140 
141 /**
142  * Helper function that creates a cc.Rect.
143  * @function
144  * @param {Number|cc.Rect} x a number or a rect object
145  * @param {Number} y
146  * @param {Number} w
147  * @param {Number} h
148  * @returns {cc.Rect}
149  * @example
150  * var rect1 = cc.rect();
151  * var rect2 = cc.rect(100,100,100,100);
152  * var rect3 = cc.rect(rect2);
153  * var rect4 = cc.rect({x: 100, y: 100, width: 100, height: 100});
154  */
155 cc.rect = function (x, y, w, h) {
156     if (x === undefined)
157         return {x: 0, y: 0, width: 0, height: 0};
158     if (y === undefined)
159         return {x: x.x, y: x.y, width: x.width, height: x.height};
160     return {x: x, y: y, width: w, height: h };
161 };
162 
163 /**
164  * Check whether a rect's value equals to another
165  * @function
166  * @param {cc.Rect} rect1
167  * @param {cc.Rect} rect2
168  * @return {Boolean}
169  */
170 cc.rectEqualToRect = function (rect1, rect2) {
171     return rect1 && rect2 && (rect1.x === rect2.x) && (rect1.y === rect2.y) && (rect1.width === rect2.width) && (rect1.height === rect2.height);
172 };
173 
174 cc._rectEqualToZero = function(rect){
175     return rect && (rect.x === 0) && (rect.y === 0) && (rect.width === 0) && (rect.height === 0);
176 };
177 
178 /**
179  * Check whether the rect1 contains rect2
180  * @function
181  * @param {cc.Rect} rect1
182  * @param {cc.Rect} rect2
183  * @return {Boolean}
184  */
185 cc.rectContainsRect = function (rect1, rect2) {
186     if (!rect1 || !rect2)
187         return false;
188     return !((rect1.x >= rect2.x) || (rect1.y >= rect2.y) ||
189         ( rect1.x + rect1.width <= rect2.x + rect2.width) ||
190         ( rect1.y + rect1.height <= rect2.y + rect2.height));
191 };
192 
193 /**
194  * Returns the rightmost x-value of a rect
195  * @function
196  * @param {cc.Rect} rect
197  * @return {Number} The rightmost x value
198  */
199 cc.rectGetMaxX = function (rect) {
200     return (rect.x + rect.width);
201 };
202 
203 /**
204  * Return the midpoint x-value of a rect
205  * @function
206  * @param {cc.Rect} rect
207  * @return {Number} The midpoint x value
208  */
209 cc.rectGetMidX = function (rect) {
210     return (rect.x + rect.width / 2.0);
211 };
212 /**
213  * Returns the leftmost x-value of a rect
214  * @function
215  * @param {cc.Rect} rect
216  * @return {Number} The leftmost x value
217  */
218 cc.rectGetMinX = function (rect) {
219     return rect.x;
220 };
221 
222 /**
223  * Return the topmost y-value of a rect
224  * @function
225  * @param {cc.Rect} rect
226  * @return {Number} The topmost y value
227  */
228 cc.rectGetMaxY = function (rect) {
229     return(rect.y + rect.height);
230 };
231 
232 /**
233  * Return the midpoint y-value of `rect'
234  * @function
235  * @param {cc.Rect} rect
236  * @return {Number} The midpoint y value
237  */
238 cc.rectGetMidY = function (rect) {
239     return rect.y + rect.height / 2.0;
240 };
241 
242 /**
243  * Return the bottommost y-value of a rect
244  * @function
245  * @param {cc.Rect} rect
246  * @return {Number} The bottommost y value
247  */
248 cc.rectGetMinY = function (rect) {
249     return rect.y;
250 };
251 
252 /**
253  * Check whether a rect contains a point
254  * @function
255  * @param {cc.Rect} rect
256  * @param {cc.Point} point
257  * @return {Boolean}
258  */
259 cc.rectContainsPoint = function (rect, point) {
260     return (point.x >= cc.rectGetMinX(rect) && point.x <= cc.rectGetMaxX(rect) &&
261         point.y >= cc.rectGetMinY(rect) && point.y <= cc.rectGetMaxY(rect)) ;
262 };
263 
264 /**
265  * Check whether a rect intersect with another
266  * @function
267  * @param {cc.Rect} rectA
268  * @param {cc.Rect} rectB
269  * @return {Boolean}
270  */
271 cc.rectIntersectsRect = function (ra, rb) {
272     var maxax = ra.x + ra.width,
273         maxay = ra.y + ra.height,
274         maxbx = rb.x + rb.width,
275         maxby = rb.y + rb.height;
276     return !(maxax < rb.x || maxbx < ra.x || maxay < rb.y || maxby < ra.y);
277 };
278 
279 /**
280  * Check whether a rect overlaps another
281  * @function
282  * @param {cc.Rect} rectA
283  * @param {cc.Rect} rectB
284  * @return {Boolean}
285  */
286 cc.rectOverlapsRect = function (rectA, rectB) {
287     return !((rectA.x + rectA.width < rectB.x) ||
288         (rectB.x + rectB.width < rectA.x) ||
289         (rectA.y + rectA.height < rectB.y) ||
290         (rectB.y + rectB.height < rectA.y));
291 };
292 
293 /**
294  * Returns the smallest rectangle that contains the two source rectangles.
295  * @function
296  * @param {cc.Rect} rectA
297  * @param {cc.Rect} rectB
298  * @return {cc.Rect}
299  */
300 cc.rectUnion = function (rectA, rectB) {
301     var rect = cc.rect(0, 0, 0, 0);
302     rect.x = Math.min(rectA.x, rectB.x);
303     rect.y = Math.min(rectA.y, rectB.y);
304     rect.width = Math.max(rectA.x + rectA.width, rectB.x + rectB.width) - rect.x;
305     rect.height = Math.max(rectA.y + rectA.height, rectB.y + rectB.height) - rect.y;
306     return rect;
307 };
308 
309 /**
310  * Returns the overlapping portion of 2 rectangles
311  * @function
312  * @param {cc.Rect} rectA
313  * @param {cc.Rect} rectB
314  * @return {cc.Rect}
315  */
316 cc.rectIntersection = function (rectA, rectB) {
317     var intersection = cc.rect(
318         Math.max(cc.rectGetMinX(rectA), cc.rectGetMinX(rectB)),
319         Math.max(cc.rectGetMinY(rectA), cc.rectGetMinY(rectB)),
320         0, 0);
321 
322     intersection.width = Math.min(cc.rectGetMaxX(rectA), cc.rectGetMaxX(rectB)) - cc.rectGetMinX(intersection);
323     intersection.height = Math.min(cc.rectGetMaxY(rectA), cc.rectGetMaxY(rectB)) - cc.rectGetMinY(intersection);
324     return intersection;
325 };
326 
327 
328