Math.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Math.js
  2. // For Mathematics functions (include Vector and Matrix)
  3. // FZ, Copyright (c) 2010 Zlongames
  4. (function()
  5. {
  6. FZ.Math= {
  7. // Linear interpolation
  8. lerp: function (ratio, a, b)
  9. {
  10. ratio = ratio<0?0:ratio>1?1:ratio;
  11. return b*ratio+(1-ratio)*a;
  12. },
  13. // Random from a to b
  14. random: function (a,b)
  15. {
  16. return FZ.Math.lerp(Math.random(), a, b);
  17. },
  18. clamp: function (x, Min,Max)
  19. {
  20. return x<Min?Min:x>Max?Max:x;
  21. },
  22. round: function(num, precision)
  23. {
  24. precision = Math.pow(10, precision || 0);
  25. return Math.round(num * precision) / precision;
  26. },
  27. Vector2: FZ.newClass(
  28. {
  29. x:0,
  30. y:0,
  31. init: function ( _x, _y ) { if (_y !== undefined) {this.x=_x?_x:0; this.y=_y?_y:0;} },
  32. clone: function (){
  33. return new FZ.Math.Vector2(this.x, this.y);
  34. },
  35. add: function (v2,t)
  36. {
  37. if (t === undefined)
  38. t = new FZ.Math.Vector2();
  39. t.x = this.x + v2.x;
  40. t.y = this.y + v2.y;
  41. return t;
  42. },
  43. sub: function (v2,t)
  44. {
  45. if (t === undefined)
  46. t = new FZ.Math.Vector2();
  47. t.x = this.x - v2.x;
  48. t.y = this.y - v2.y;
  49. return t;
  50. },
  51. normalize: function(t) {
  52. if (t === undefined)
  53. t = new FZ.Math.Vector2();
  54. var r = 1.0/this.length();
  55. t.x = this.x*r;
  56. t.y = this.y*r;
  57. return t;
  58. },
  59. scale: function(s,t) {
  60. if (t === undefined)
  61. t = new FZ.Math.Vector2();
  62. t.x = this.x*s;
  63. t.y = this.y*s;
  64. return t;
  65. },
  66. applyTransform:function(_matrix, t)
  67. {
  68. if (t === undefined)
  69. t = new FZ.Math.Vector2();
  70. tx = _matrix.m11*this.x + _matrix.m21*this.y + _matrix.dx;
  71. ty = _matrix.m12*this.x + _matrix.m22*this.y + _matrix.dy;
  72. t.x=tx; t.y=ty;
  73. return t;
  74. },
  75. dot: function(v2){ return this.x*v2.x + this.y*v2.y;},
  76. length: function(){ return Math.sqrt( this.x*this.x + this.y*this.y);},
  77. lengthSquared: function(){ return this.x*this.x + this.y*this.y;}
  78. // cross: x
  79. }),
  80. /*
  81. * Matrix:
  82. * m11 m21 dx
  83. * m12 m22 dy
  84. * 0 0 1
  85. */
  86. Matrix3: FZ.newClass(
  87. {
  88. m11:1, m21: 0, dx: 0,
  89. m12:0, m22: 1, dy: 0,
  90. set: function(m11, m12, m21, m22, dx, dy) {
  91. this.m11 = m11; this.m21 = m21; this.dx = dx;
  92. this.m12 = m12; this.m22 = m22; this.dy = dy;
  93. return this;
  94. },
  95. clone: function() {
  96. var theClone = new FZ.Math.Matrix3();
  97. theClone.m11 = this.m11;
  98. theClone.m21 = this.m21;
  99. theClone.m12 = this.m12;
  100. theClone.m22 = this.m22;
  101. theClone.dx = this.dx;
  102. theClone.dy = this.dy;
  103. return theClone;
  104. },
  105. identity: function()
  106. {
  107. this.m11 = 1; this.m21 = 0; this.dx = 0;
  108. this.m12 = 0; this.m22 = 1; this.dy = 0;
  109. return this;
  110. },
  111. mul: function (m2, t)
  112. {
  113. if (t === undefined)
  114. t = new FZ.Math.Matrix3();
  115. var tm11 = this.m11*m2.m11 + this.m21*m2.m12;
  116. var tm21 = this.m11*m2.m21 + this.m21*m2.m22;
  117. var tm12 = this.m12*m2.m11 + this.m22*m2.m12;
  118. var tm22 = this.m12*m2.m21 + this.m22*m2.m22;
  119. var tdx = this.dx+ this.m11*m2.dx + this.m21*m2.dx ;
  120. var tdy = this.dy+ this.m12*m2.dx + this.m22*m2.dy ;
  121. t.m11 = tm11;
  122. t.m21 = tm21;
  123. t.m12 = tm12;
  124. t.m22 = tm22;
  125. t.dx = tdx ;
  126. t.dy = tdy ;
  127. return t;
  128. },
  129. makeRotate: function(_angle)
  130. {
  131. var s=Math.sin(_angle), c=Math.cos(_angle);
  132. this.m11 = c;
  133. this.m21 = -s;
  134. this.m12 = s;
  135. this.m22 = c;
  136. return this;
  137. },
  138. rotate: function(_angle)
  139. {
  140. var s=Math.sin(_angle), c=Math.cos(_angle);
  141. var tm11 = this.m11*c + this.m21*s;
  142. var tm21 =-this.m11*s + this.m21*c;
  143. var tm12 = this.m12*c + this.m22*s;
  144. var tm22 =-this.m12*s + this.m22*c;
  145. this.m11 = tm11;
  146. this.m21 = tm21;
  147. this.m12 = tm12;
  148. this.m22 = tm22;
  149. return this;
  150. },
  151. makeScale1: function(s)
  152. {
  153. return this.makeScale2(s,s);
  154. },
  155. scale1: function (s)
  156. {
  157. return this.scale2(s,s);
  158. },
  159. makeScale2: function (s1, s2)
  160. {
  161. this.m11 = s1;
  162. this.m22 = s2;
  163. this.m12 = 0;
  164. this.m21 = 0;
  165. return this;
  166. },
  167. scale2: function (s1,s2)
  168. {
  169. this.m11 *= s1;
  170. this.m21 *= s2;
  171. this.m12 *= s1;
  172. this.m22 *= s2;
  173. return this;
  174. },
  175. makeTranslate:function(x,y)
  176. {
  177. this.dx = x;
  178. this.dy = y;
  179. return this;
  180. },
  181. translate: function(x,y)
  182. {
  183. this.dx += this.m11* x + this.m21 * y;
  184. this.dy += this.m12* x + this.m22 * y;
  185. return this;
  186. },
  187. inverseAffine:function(t)
  188. {
  189. if (t === undefined)
  190. t = new FZ.Math.Matrix3();
  191. var invDet = 1 / (this.m11*this.m22 - this.m21*this.m12);
  192. var t11 = this.m22*invDet;
  193. var t21 = this.m12*invDet;
  194. var t12 = this.m21*invDet;
  195. var t22 = this.m11*invDet;
  196. var tdx = -(t11*this.dx + t21*this.dy);
  197. var tdy = -(t12*this.dx + t22*this.dy);
  198. t.m11 = t11;
  199. t.m21 = t21;
  200. t.m12 = t12;
  201. t.m22 = t22;
  202. t.dx = tdx;
  203. t.dy = tdy;
  204. return t;
  205. }
  206. })
  207. };
  208. })();