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 * @function 29 * @param {Number} a 30 * @param {Number} b 31 * @param {Number} c 32 * @param {Number} d 33 * @param {Number} tx 34 * @param {Number} ty 35 */ 36 cc.AffineTransform = function (a, b, c, d, tx, ty) { 37 this.a = a; 38 this.b = b; 39 this.c = c; 40 this.d = d; 41 this.tx = tx; 42 this.ty = ty; 43 }; 44 45 cc.__AffineTransformMake = function (a, b, c, d, tx, ty) { 46 return new cc.AffineTransform(a, b, c, d, tx, ty); 47 }; 48 49 /** 50 * @function 51 * @param {Number} a 52 * @param {Number} b 53 * @param {Number} c 54 * @param {Number} d 55 * @param {Number} tx 56 * @param {Number} ty 57 * @return {cc.AffineTransform} 58 * Constructor 59 */ 60 cc.AffineTransformMake = function (a, b, c, d, tx, ty) { 61 return new cc.AffineTransform(a, b, c, d, tx, ty); 62 }; 63 64 cc.__PointApplyAffineTransform = function (point, t) { 65 var p = cc.p(0, 0); 66 p.x = t.a * point.x + t.c * point.y + t.tx; 67 p.y = t.b * point.x + t.d * point.y + t.ty; 68 return p; 69 }; 70 71 /** 72 * @function 73 * @param {cc.Point} point 74 * @param {cc.AffineTransform} t 75 * @return {cc.Point} 76 * Constructor 77 */ 78 cc.PointApplyAffineTransform = function (point, t) { 79 return cc.__PointApplyAffineTransform(point, t); 80 }; 81 82 cc.__SizeApplyAffineTransform = function (size, t) { 83 var s = cc.size(0, 0); 84 s.width = t.a * size.width + t.c * size.height; 85 s.height = t.b * size.width + t.d * size.height; 86 return s; 87 }; 88 89 /** 90 * @function 91 * @param {cc.Size} size 92 * @param {cc.AffineTransform} t 93 * @return {cc.Size} 94 * Constructor 95 */ 96 cc.SizeApplyAffineTransform = function (size, t) { 97 return cc.__SizeApplyAffineTransform(size, t); 98 }; 99 100 /** 101 * @function 102 * @return {cc.AffineTransform} 103 * Constructor 104 */ 105 cc.AffineTransformMakeIdentity = function () { 106 return cc.__AffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0); 107 }; 108 109 /** 110 * @function 111 * @return {cc.AffineTransform} 112 * Constructor 113 */ 114 cc.AffineTransformIdentity = function () { 115 return cc.AffineTransformMakeIdentity(); 116 }; 117 118 /** 119 * @function 120 * @param {cc.Rect} rect 121 * @param {cc.AffineTransform} anAffineTransform 122 * @return {cc.Rect} 123 * Constructor 124 */ 125 cc.RectApplyAffineTransform = function (rect, anAffineTransform) { 126 var top = cc.Rect.CCRectGetMinY(rect); 127 var left = cc.Rect.CCRectGetMinX(rect); 128 var right = cc.Rect.CCRectGetMaxX(rect); 129 var bottom = cc.Rect.CCRectGetMaxY(rect); 130 131 var topLeft = cc.PointApplyAffineTransform(cc.p(left, top), anAffineTransform); 132 var topRight = cc.PointApplyAffineTransform(cc.p(right, top), anAffineTransform); 133 var bottomLeft = cc.PointApplyAffineTransform(cc.p(left, bottom), anAffineTransform); 134 var bottomRight = cc.PointApplyAffineTransform(cc.p(right, bottom), anAffineTransform); 135 136 var minX = Math.min(Math.min(topLeft.x, topRight.x), Math.min(bottomLeft.x, bottomRight.x)); 137 var maxX = Math.max(Math.max(topLeft.x, topRight.x), Math.max(bottomLeft.x, bottomRight.x)); 138 var minY = Math.min(Math.min(topLeft.y, topRight.y), Math.min(bottomLeft.y, bottomRight.y)); 139 var maxY = Math.max(Math.max(topLeft.y, topRight.y), Math.max(bottomLeft.y, bottomRight.y)); 140 141 return cc.rect(minX, minY, (maxX - minX), (maxY - minY)); 142 }; 143 144 /** 145 * @function 146 * @param {cc.AffineTransform} t 147 * @param {Number} tx 148 * @param {Number}ty 149 * @return {cc.AffineTransform} 150 * Constructor 151 */ 152 cc.AffineTransformTranslate = function (t, tx, ty) { 153 return cc.__AffineTransformMake(t.a, t.b, t.c, t.d, t.tx + t.a * tx + t.c * ty, t.ty + t.b * tx + t.d * ty); 154 }; 155 156 /** 157 * @function 158 * @param {cc.AffineTransform} t 159 * @param {Number} sx 160 * @param {Number} sy 161 * @return {cc.AffineTransform} 162 * Constructor 163 */ 164 cc.AffineTransformScale = function (t, sx, sy) { 165 return cc.__AffineTransformMake(t.a * sx, t.b * sx, t.c * sy, t.d * sy, t.tx, t.ty); 166 }; 167 168 /** 169 * @function 170 * @param {cc.AffineTransform} aTransform 171 * @param {Number} anAngle 172 * @return {cc.AffineTransform} 173 * Constructor 174 */ 175 cc.AffineTransformRotate = function (aTransform, anAngle) { 176 var fSin = Math.sin(anAngle); 177 var fCos = Math.cos(anAngle); 178 179 return cc.__AffineTransformMake(aTransform.a * fCos + aTransform.c * fSin, 180 aTransform.b * fCos + aTransform.d * fSin, 181 aTransform.c * fCos - aTransform.a * fSin, 182 aTransform.d * fCos - aTransform.b * fSin, 183 aTransform.tx, 184 aTransform.ty); 185 }; 186 187 /* Concatenate `t2' to `t1' and return the result:<br/> 188 * t' = t1 * t2 189 * @param {cc.AffineTransform} t1 190 * @param {cc.AffineTransform} t2 191 * @return {cc.AffineTransform} 192 * Constructor 193 */ 194 cc.AffineTransformConcat = function (t1, t2) { 195 return cc.__AffineTransformMake(t1.a * t2.a + t1.b * t2.c, t1.a * t2.b + t1.b * t2.d, //a,b 196 t1.c * t2.a + t1.d * t2.c, t1.c * t2.b + t1.d * t2.d, //c,d 197 t1.tx * t2.a + t1.ty * t2.c + t2.tx, //tx 198 t1.tx * t2.b + t1.ty * t2.d + t2.ty); //ty 199 }; 200 201 /** 202 * Return true if `t1' and `t2' are equal, false otherwise. 203 * @function 204 * @param {cc.AffineTransform} t1 205 * @param {cc.AffineTransform} t2 206 * @return {Boolean} 207 * Constructor 208 */ 209 cc.AffineTransformEqualToTransform = function (t1, t2) { 210 return (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c && t1.d == t2.d && t1.tx == t2.tx && t1.ty == t2.ty); 211 }; 212 213 /** 214 * @function 215 * @param {cc.AffineTransform} t 216 * @return {cc.AffineTransform} 217 * Constructor 218 */ 219 cc.AffineTransformInvert = function (t) { 220 var determinant = 1 / (t.a * t.d - t.b * t.c); 221 222 return cc.__AffineTransformMake(determinant * t.d, -determinant * t.b, -determinant * t.c, determinant * t.a, 223 determinant * (t.c * t.ty - t.d * t.tx), determinant * (t.b * t.tx - t.a * t.ty)); 224 }; 225