CCAffineTransform.js 7.9 KB

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