CCAffineTransform.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. 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. * <p>cc.AffineTransform class represent an affine transform matrix. It's composed basically by translation, rotation, scale transformations.<br/>
  24. * Please do not use its constructor directly, use cc.affineTransformMake alias function instead.
  25. * </p>
  26. * @class cc.AffineTransform
  27. * @param {Number} a
  28. * @param {Number} b
  29. * @param {Number} c
  30. * @param {Number} d
  31. * @param {Number} tx
  32. * @param {Number} ty
  33. * @see cc.affineTransformMake
  34. */
  35. cc.AffineTransform = function (a, b, c, d, tx, ty) {
  36. this.a = a;
  37. this.b = b;
  38. this.c = c;
  39. this.d = d;
  40. this.tx = tx;
  41. this.ty = ty;
  42. };
  43. /**
  44. * Create a cc.AffineTransform object with all contents in the matrix
  45. * @function
  46. *
  47. * @param {Number} a
  48. * @param {Number} b
  49. * @param {Number} c
  50. * @param {Number} d
  51. * @param {Number} tx
  52. * @param {Number} ty
  53. * @return {cc.AffineTransform}
  54. */
  55. cc.affineTransformMake = function (a, b, c, d, tx, ty) {
  56. return {a: a, b: b, c: c, d: d, tx: tx, ty: ty};
  57. };
  58. /**
  59. * Apply the affine transformation on a point.
  60. * @function
  61. *
  62. * @param {cc.Point} point
  63. * @param {cc.AffineTransform} t
  64. * @return {cc.Point}
  65. */
  66. cc.pointApplyAffineTransform = function (point, t) {
  67. return {x: t.a * point.x + t.c * point.y + t.tx, y: t.b * point.x + t.d * point.y + t.ty};
  68. };
  69. cc._pointApplyAffineTransform = function (x, y, t) {
  70. return {x: t.a * x + t.c * y + t.tx,
  71. y: t.b * x + t.d * y + t.ty};
  72. };
  73. /**
  74. * Apply the affine transformation on a size.
  75. * @function
  76. *
  77. * @param {cc.Size} size
  78. * @param {cc.AffineTransform} t
  79. * @return {cc.Size}
  80. */
  81. cc.sizeApplyAffineTransform = function (size, t) {
  82. return {width: t.a * size.width + t.c * size.height, height: t.b * size.width + t.d * size.height};
  83. };
  84. /**
  85. * <p>Create a identity transformation matrix: <br/>
  86. * [ 1, 0, 0, <br/>
  87. * 0, 1, 0 ]</p>
  88. * @function
  89. *
  90. * @return {cc.AffineTransform}
  91. */
  92. cc.affineTransformMakeIdentity = function () {
  93. return {a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0};
  94. };
  95. /**
  96. * <p>Create a identity transformation matrix: <br/>
  97. * [ 1, 0, 0, <br/>
  98. * 0, 1, 0 ]</p>
  99. * @function
  100. *
  101. * @return {cc.AffineTransform}
  102. * @deprecated since v3.0, please use cc.affineTransformMakeIdentity() instead
  103. * @see cc.affineTransformMakeIdentity
  104. */
  105. cc.affineTransformIdentity = function () {
  106. return {a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0};
  107. };
  108. /**
  109. * Apply the affine transformation on a rect.
  110. * @function
  111. *
  112. * @param {cc.Rect} rect
  113. * @param {cc.AffineTransform} anAffineTransform
  114. * @return {cc.Rect}
  115. */
  116. cc.rectApplyAffineTransform = function (rect, anAffineTransform) {
  117. var top = cc.rectGetMinY(rect);
  118. var left = cc.rectGetMinX(rect);
  119. var right = cc.rectGetMaxX(rect);
  120. var bottom = cc.rectGetMaxY(rect);
  121. var topLeft = cc._pointApplyAffineTransform(left, top, anAffineTransform);
  122. var topRight = cc._pointApplyAffineTransform(right, top, anAffineTransform);
  123. var bottomLeft = cc._pointApplyAffineTransform(left, bottom, anAffineTransform);
  124. var bottomRight = cc._pointApplyAffineTransform(right, bottom, anAffineTransform);
  125. var minX = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
  126. var maxX = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
  127. var minY = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
  128. var maxY = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
  129. return cc.rect(minX, minY, (maxX - minX), (maxY - minY));
  130. };
  131. cc._rectApplyAffineTransformIn = function(rect, anAffineTransform){
  132. var top = cc.rectGetMinY(rect);
  133. var left = cc.rectGetMinX(rect);
  134. var right = cc.rectGetMaxX(rect);
  135. var bottom = cc.rectGetMaxY(rect);
  136. var topLeft = cc._pointApplyAffineTransform(left, top, anAffineTransform);
  137. var topRight = cc._pointApplyAffineTransform(right, top, anAffineTransform);
  138. var bottomLeft = cc._pointApplyAffineTransform(left, bottom, anAffineTransform);
  139. var bottomRight = cc._pointApplyAffineTransform(right, bottom, anAffineTransform);
  140. var minX = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
  141. var maxX = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
  142. var minY = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
  143. var maxY = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
  144. rect.x = minX;
  145. rect.y = minY;
  146. rect.width = maxX - minX;
  147. rect.height = maxY - minY;
  148. return rect;
  149. };
  150. /**
  151. * Create a new affine transformation with a base transformation matrix and a translation based on it.
  152. * @function
  153. *
  154. * @param {cc.AffineTransform} t The base affine transform object
  155. * @param {Number} tx The translation on x axis
  156. * @param {Number} ty The translation on y axis
  157. * @return {cc.AffineTransform}
  158. */
  159. cc.affineTransformTranslate = function (t, tx, ty) {
  160. return {
  161. a: t.a,
  162. b: t.b,
  163. c: t.c,
  164. d: t.d,
  165. tx: t.tx + t.a * tx + t.c * ty,
  166. ty: t.ty + t.b * tx + t.d * ty
  167. };
  168. };
  169. /**
  170. * Create a new affine transformation with a base transformation matrix and a scale based on it.
  171. * @function
  172. * @param {cc.AffineTransform} t The base affine transform object
  173. * @param {Number} sx The scale on x axis
  174. * @param {Number} sy The scale on y axis
  175. * @return {cc.AffineTransform}
  176. */
  177. cc.affineTransformScale = function (t, sx, sy) {
  178. return {a: t.a * sx, b: t.b * sx, c: t.c * sy, d: t.d * sy, tx: t.tx, ty: t.ty};
  179. };
  180. /**
  181. * Create a new affine transformation with a base transformation matrix and a rotation based on it.
  182. * @function
  183. * @param {cc.AffineTransform} aTransform The base affine transform object
  184. * @param {Number} anAngle The angle to rotate
  185. * @return {cc.AffineTransform}
  186. */
  187. cc.affineTransformRotate = function (aTransform, anAngle) {
  188. var fSin = Math.sin(anAngle);
  189. var fCos = Math.cos(anAngle);
  190. return {a: aTransform.a * fCos + aTransform.c * fSin,
  191. b: aTransform.b * fCos + aTransform.d * fSin,
  192. c: aTransform.c * fCos - aTransform.a * fSin,
  193. d: aTransform.d * fCos - aTransform.b * fSin,
  194. tx: aTransform.tx,
  195. ty: aTransform.ty};
  196. };
  197. /**
  198. * Concatenate a transform matrix to another and return the result:<br/>
  199. * t' = t1 * t2
  200. * @function
  201. * @param {cc.AffineTransform} t1 The first transform object
  202. * @param {cc.AffineTransform} t2 The transform object to concatenate
  203. * @return {cc.AffineTransform} The result of concatenation
  204. */
  205. cc.affineTransformConcat = function (t1, t2) {
  206. return {a: t1.a * t2.a + t1.b * t2.c, //a
  207. b: t1.a * t2.b + t1.b * t2.d, //b
  208. c: t1.c * t2.a + t1.d * t2.c, //c
  209. d: t1.c * t2.b + t1.d * t2.d, //d
  210. tx: t1.tx * t2.a + t1.ty * t2.c + t2.tx, //tx
  211. ty: t1.tx * t2.b + t1.ty * t2.d + t2.ty}; //ty
  212. };
  213. /**
  214. * Return true if an affine transform equals to another, false otherwise.
  215. * @function
  216. * @param {cc.AffineTransform} t1
  217. * @param {cc.AffineTransform} t2
  218. * @return {Boolean}
  219. */
  220. cc.affineTransformEqualToTransform = function (t1, t2) {
  221. 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));
  222. };
  223. /**
  224. * Get the invert transform of an AffineTransform object
  225. * @function
  226. * @param {cc.AffineTransform} t
  227. * @return {cc.AffineTransform} The inverted transform object
  228. */
  229. cc.affineTransformInvert = function (t) {
  230. var determinant = 1 / (t.a * t.d - t.b * t.c);
  231. return {a: determinant * t.d, b: -determinant * t.b, c: -determinant * t.c, d: determinant * t.a,
  232. tx: determinant * (t.c * t.ty - t.d * t.tx), ty: determinant * (t.b * t.tx - t.a * t.ty)};
  233. };