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