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  * @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 /**
 46  * 
 47  * @function
 48  * @param {Number} a
 49  * @param {Number} b
 50  * @param {Number} c
 51  * @param {Number} d
 52  * @param {Number} tx
 53  * @param {Number} ty
 54  * @return {cc.AffineTransform}
 55  * Constructor
 56  */
 57 cc.AffineTransformMake = function (a, b, c, d, tx, ty) {
 58     return {a: a, b: b, c: c, d: d, tx: tx, ty: ty};
 59 };
 60 
 61 /**
 62  * 
 63  * @function
 64  * @param {cc.Point} point
 65  * @param {cc.AffineTransform} t
 66  * @return {cc.Point}
 67  * Constructor
 68  */
 69 cc.PointApplyAffineTransform = function (point, t) {
 70     return {x: t.a * point.x + t.c * point.y + t.tx, y: t.b * point.x + t.d * point.y + t.ty};
 71 };
 72 
 73 cc._PointApplyAffineTransform = function (x, y, t) {
 74     return {x: t.a * x + t.c * y + t.tx,
 75         y: t.b * x + t.d * y + t.ty};
 76 };
 77 
 78 /**
 79  * 
 80  * @function
 81  * @param {cc.Size} size
 82  * @param {cc.AffineTransform} t
 83  * @return {cc.Size}
 84  * Constructor
 85  */
 86 cc.SizeApplyAffineTransform = function (size, t) {
 87     return {width: t.a * size.width + t.c * size.height, height: t.b * size.width + t.d * size.height};
 88 };
 89 
 90 /**
 91  * 
 92  * @function
 93  * @return {cc.AffineTransform}
 94  * Constructor
 95  */
 96 cc.AffineTransformMakeIdentity = function () {
 97     return {a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0};
 98 };
 99 
100 /**
101  * 
102  * @function
103  * @return {cc.AffineTransform}
104  * Constructor
105  */
106 cc.AffineTransformIdentity = function () {
107     return {a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0};
108 };
109 
110 /**
111  * 
112  * @function
113  * @param {cc.Rect} rect
114  * @param {cc.AffineTransform} anAffineTransform
115  * @return {cc.Rect}
116  * Constructor
117  */
118 cc.RectApplyAffineTransform = function (rect, anAffineTransform) {
119     var top = cc.rectGetMinY(rect);
120     var left = cc.rectGetMinX(rect);
121     var right = cc.rectGetMaxX(rect);
122     var bottom = cc.rectGetMaxY(rect);
123 
124     var topLeft = cc._PointApplyAffineTransform(left, top, anAffineTransform);
125     var topRight = cc._PointApplyAffineTransform(right, top, anAffineTransform);
126     var bottomLeft = cc._PointApplyAffineTransform(left, bottom, anAffineTransform);
127     var bottomRight = cc._PointApplyAffineTransform(right, bottom, anAffineTransform);
128 
129     var minX = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
130     var maxX = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
131     var minY = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
132     var maxY = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
133 
134     return cc.rect(minX, minY, (maxX - minX), (maxY - minY));
135 };
136 
137 cc._RectApplyAffineTransformIn = function(rect, anAffineTransform){
138     var top = cc.rectGetMinY(rect);
139     var left = cc.rectGetMinX(rect);
140     var right = cc.rectGetMaxX(rect);
141     var bottom = cc.rectGetMaxY(rect);
142 
143     var topLeft = cc._PointApplyAffineTransform(left, top, anAffineTransform);
144     var topRight = cc._PointApplyAffineTransform(right, top, anAffineTransform);
145     var bottomLeft = cc._PointApplyAffineTransform(left, bottom, anAffineTransform);
146     var bottomRight = cc._PointApplyAffineTransform(right, bottom, anAffineTransform);
147 
148     var minX = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
149     var maxX = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
150     var minY = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
151     var maxY = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
152 
153     rect.x = minX;
154     rect.y = minY;
155     rect.width = maxX - minX;
156     rect.height = maxY - minY;
157     return rect;
158 };
159 
160 /**
161  * 
162  * @function
163  * @param {cc.AffineTransform} t
164  * @param {Number} tx
165  * @param {Number}ty
166  * @return {cc.AffineTransform}
167  * Constructor
168  */
169 cc.AffineTransformTranslate = function (t, tx, ty) {
170     return {
171         a: t.a,
172         b: t.b,
173         c: t.c,
174         d: t.d,
175         tx: t.tx + t.a * tx + t.c * ty,
176         ty: t.ty + t.b * tx + t.d * ty
177     };
178 };
179 
180 /**
181  * 
182  * @function
183  * @param {cc.AffineTransform} t
184  * @param {Number} sx
185  * @param {Number} sy
186  * @return {cc.AffineTransform}
187  * Constructor
188  */
189 cc.AffineTransformScale = function (t, sx, sy) {
190     return {a: t.a * sx, b: t.b * sx, c: t.c * sy, d: t.d * sy, tx: t.tx, ty: t.ty};
191 };
192 
193 /**
194  * 
195  * @function
196  * @param {cc.AffineTransform} aTransform
197  * @param {Number} anAngle
198  * @return {cc.AffineTransform}
199  * Constructor
200  */
201 cc.AffineTransformRotate = function (aTransform, anAngle) {
202     var fSin = Math.sin(anAngle);
203     var fCos = Math.cos(anAngle);
204 
205     return {a: aTransform.a * fCos + aTransform.c * fSin,
206         b: aTransform.b * fCos + aTransform.d * fSin,
207         c: aTransform.c * fCos - aTransform.a * fSin,
208         d: aTransform.d * fCos - aTransform.b * fSin,
209         tx: aTransform.tx,
210         ty: aTransform.ty};
211 };
212 
213 /**
214  * Concatenate `t2' to `t1' and return the result:<br/>
215  * t' = t1 * t2
216  * 
217  * @function
218  * @param {cc.AffineTransform} t1
219  * @param {cc.AffineTransform} t2
220  * @return {cc.AffineTransform}
221  * Constructor
222  */
223 cc.AffineTransformConcat = function (t1, t2) {
224     return {a: t1.a * t2.a + t1.b * t2.c,                          //a
225         b: t1.a * t2.b + t1.b * t2.d,                               //b
226         c: t1.c * t2.a + t1.d * t2.c,                               //c
227         d: t1.c * t2.b + t1.d * t2.d,                               //d
228         tx: t1.tx * t2.a + t1.ty * t2.c + t2.tx,                    //tx
229         ty: t1.tx * t2.b + t1.ty * t2.d + t2.ty};				    //ty
230 };
231 
232 /**
233  * Return true if `t1' and `t2' are equal, false otherwise.
234  * 
235  * @function
236  * @param {cc.AffineTransform} t1
237  * @param {cc.AffineTransform} t2
238  * @return {Boolean}
239  * Constructor
240  */
241 cc.AffineTransformEqualToTransform = function (t1, t2) {
242     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));
243 };
244 
245 /**
246  * Get the invert value of an AffineTransform object
247  * 
248  * @function
249  * @param {cc.AffineTransform} t
250  * @return {cc.AffineTransform}
251  * Constructor
252  */
253 cc.AffineTransformInvert = function (t) {
254     var determinant = 1 / (t.a * t.d - t.b * t.c);
255     return {a: determinant * t.d, b: -determinant * t.b, c: -determinant * t.c, d: determinant * t.a,
256         tx: determinant * (t.c * t.ty - t.d * t.tx), ty: determinant * (t.b * t.tx - t.a * t.ty)};
257 };
258