matrix.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * 3x2矩阵操作类
  3. * @exports zrender/tool/matrix
  4. */
  5. /* global Float32Array */
  6. var ArrayCtor = typeof Float32Array === 'undefined' ? Array : Float32Array;
  7. /**
  8. * Create a identity matrix.
  9. * @return {Float32Array|Array.<number>}
  10. */
  11. function create() {
  12. var out = new ArrayCtor(6);
  13. identity(out);
  14. return out;
  15. }
  16. /**
  17. * 设置矩阵为单位矩阵
  18. * @param {Float32Array|Array.<number>} out
  19. */
  20. function identity(out) {
  21. out[0] = 1;
  22. out[1] = 0;
  23. out[2] = 0;
  24. out[3] = 1;
  25. out[4] = 0;
  26. out[5] = 0;
  27. return out;
  28. }
  29. /**
  30. * 复制矩阵
  31. * @param {Float32Array|Array.<number>} out
  32. * @param {Float32Array|Array.<number>} m
  33. */
  34. function copy(out, m) {
  35. out[0] = m[0];
  36. out[1] = m[1];
  37. out[2] = m[2];
  38. out[3] = m[3];
  39. out[4] = m[4];
  40. out[5] = m[5];
  41. return out;
  42. }
  43. /**
  44. * 矩阵相乘
  45. * @param {Float32Array|Array.<number>} out
  46. * @param {Float32Array|Array.<number>} m1
  47. * @param {Float32Array|Array.<number>} m2
  48. */
  49. function mul(out, m1, m2) {
  50. // Consider matrix.mul(m, m2, m);
  51. // where out is the same as m2.
  52. // So use temp variable to escape error.
  53. var out0 = m1[0] * m2[0] + m1[2] * m2[1];
  54. var out1 = m1[1] * m2[0] + m1[3] * m2[1];
  55. var out2 = m1[0] * m2[2] + m1[2] * m2[3];
  56. var out3 = m1[1] * m2[2] + m1[3] * m2[3];
  57. var out4 = m1[0] * m2[4] + m1[2] * m2[5] + m1[4];
  58. var out5 = m1[1] * m2[4] + m1[3] * m2[5] + m1[5];
  59. out[0] = out0;
  60. out[1] = out1;
  61. out[2] = out2;
  62. out[3] = out3;
  63. out[4] = out4;
  64. out[5] = out5;
  65. return out;
  66. }
  67. /**
  68. * 平移变换
  69. * @param {Float32Array|Array.<number>} out
  70. * @param {Float32Array|Array.<number>} a
  71. * @param {Float32Array|Array.<number>} v
  72. */
  73. function translate(out, a, v) {
  74. out[0] = a[0];
  75. out[1] = a[1];
  76. out[2] = a[2];
  77. out[3] = a[3];
  78. out[4] = a[4] + v[0];
  79. out[5] = a[5] + v[1];
  80. return out;
  81. }
  82. /**
  83. * 旋转变换
  84. * @param {Float32Array|Array.<number>} out
  85. * @param {Float32Array|Array.<number>} a
  86. * @param {number} rad
  87. */
  88. function rotate(out, a, rad) {
  89. var aa = a[0];
  90. var ac = a[2];
  91. var atx = a[4];
  92. var ab = a[1];
  93. var ad = a[3];
  94. var aty = a[5];
  95. var st = Math.sin(rad);
  96. var ct = Math.cos(rad);
  97. out[0] = aa * ct + ab * st;
  98. out[1] = -aa * st + ab * ct;
  99. out[2] = ac * ct + ad * st;
  100. out[3] = -ac * st + ct * ad;
  101. out[4] = ct * atx + st * aty;
  102. out[5] = ct * aty - st * atx;
  103. return out;
  104. }
  105. /**
  106. * 缩放变换
  107. * @param {Float32Array|Array.<number>} out
  108. * @param {Float32Array|Array.<number>} a
  109. * @param {Float32Array|Array.<number>} v
  110. */
  111. function scale(out, a, v) {
  112. var vx = v[0];
  113. var vy = v[1];
  114. out[0] = a[0] * vx;
  115. out[1] = a[1] * vy;
  116. out[2] = a[2] * vx;
  117. out[3] = a[3] * vy;
  118. out[4] = a[4] * vx;
  119. out[5] = a[5] * vy;
  120. return out;
  121. }
  122. /**
  123. * 求逆矩阵
  124. * @param {Float32Array|Array.<number>} out
  125. * @param {Float32Array|Array.<number>} a
  126. */
  127. function invert(out, a) {
  128. var aa = a[0];
  129. var ac = a[2];
  130. var atx = a[4];
  131. var ab = a[1];
  132. var ad = a[3];
  133. var aty = a[5];
  134. var det = aa * ad - ab * ac;
  135. if (!det) {
  136. return null;
  137. }
  138. det = 1.0 / det;
  139. out[0] = ad * det;
  140. out[1] = -ab * det;
  141. out[2] = -ac * det;
  142. out[3] = aa * det;
  143. out[4] = (ac * aty - ad * atx) * det;
  144. out[5] = (ab * atx - aa * aty) * det;
  145. return out;
  146. }
  147. /**
  148. * Clone a new matrix.
  149. * @param {Float32Array|Array.<number>} a
  150. */
  151. function clone(a) {
  152. var b = create();
  153. copy(b, a);
  154. return b;
  155. }
  156. exports.create = create;
  157. exports.identity = identity;
  158. exports.copy = copy;
  159. exports.mul = mul;
  160. exports.translate = translate;
  161. exports.rotate = rotate;
  162. exports.scale = scale;
  163. exports.invert = invert;
  164. exports.clone = clone;