1.parseInt的坑
parseInt是把字符串转化为整数,有时候我们直接这样使用。
parseInt("12.23");
parseInt("0.00008");
parseInt(0.0000008);
但是,当很小的数时候,比如parseInt(0.0000008);
会得到8,这肯定不是我们要的结果,所以需要对数优先判断下是否小于1。
function myparseInt(x){
var k=parseFloat(x);
if(k<1){
return 0;
}
return parseInt(x);
}
2.Matrix矩阵
function Matrix(a, b, c, d, x, y) {
this.a = (a != null) ? a : 1;
this.b = b || 0;
this.c = c || 0;
this.d = (d != null) ? d : 1;
this.x = x || 0;
this.y = y || 0;
}
Matrix.temp = new Matrix();
Matrix.prototype.toString = function() {
return "matrix(" + this.a + "," + this.b + "," + this.c + "," + this.d + "," + this.x + "," + this.y + ")";
};
Matrix.prototype.equals = function(m) {
if(this.a === m.a && this.b === m.b && this.c === m.c && this.d === m.d && this.x === m.x && this.y === m.y) {
return true;
}
return false;
};
Matrix.prototype.identity = function() {
this.a = 1;
this.b = 0;
this.c = 0;
this.d = 1;
this.x = 0;
this.y = 0;
};
Matrix.prototype.clone = function() {
return new Matrix(
this.a,
this.b,
this.c,
this.d,
this.x,
this.y
);
};
Matrix.prototype.copyFrom = function(m) {
this.a = m.a;
this.b = m.b;
this.c = m.c;
this.d = m.d;
this.x = m.x;
this.y = m.y;
};
Matrix.prototype.rotate = function(angle) {
var u = Math.cos(angle);
var v = Math.sin(angle);
var temp = this.a;
this.a = u * this.a - v * this.b;
this.b = v * temp + u * this.b;
temp = this.c;
this.c = u * this.c - v * this.d;
this.d = v * temp + u * this.d;
temp = this.x;
this.x = u * this.x - v * this.y;
this.y = v * temp + u * this.y;
};
Matrix.prototype.translate = function(x, y) {
this.x += x;
this.y += y;
};
Matrix.prototype.concat = function(m) {
var a = this.a * m.a;
var b = 0;
var c = 0;
var d = this.d * m.d;
var x = this.x * m.a + m.x;
var y = this.y * m.d + m.y;
if(this.b !== 0 || this.c !== 0 || m.b !== 0 || m.c !== 0) {
a += this.b * m.c;
d += this.c * m.b;
b += this.a * m.b + this.b * m.d;
c += this.c * m.a + this.d * m.c;
x += this.y * m.c;
y += this.x * m.b;
}
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.x = x;
this.y = y;
};
Matrix.prototype.invert = function() {
if(this.b === 0 && this.c === 0 && this.a !== 0 && this.d !== 0) {
this.a = 1 / this.a;
this.d = 1 / this.d;
this.b = 0;
this.c = 0;
this.x = -this.a * this.x;
this.y = -this.d * this.y;
} else {
var det = this.a * this.d - this.b * this.c;
if(det === 0) {
this.identity();
return;
}
det = 1 / det;
var temp = this.a;
this.a = this.d * det;
this.b = -this.b * det;
this.c = -this.c * det;
this.d = temp * det;
temp = this.y;
this.y = -(this.b * this.x + this.d * this.y);
this.x = -(this.a * this.x + this.c * temp);
}
};
Matrix.prototype.getRotationX = function() {
return Math.atan2(this.b, this.a);
};
Matrix.prototype.getRotationY = function() {
return Math.atan2(this.c, this.d);
};
Matrix.prototype.getTransformedX = function(x, y) {
return this.x + this.a * x + this.c * y;
};
Matrix.prototype.getTransformedY = function(x, y) {
return this.y + this.d * y + this.b * x;
};
Matrix.prototype.scale = function(x, y) {
this.a *= x;
this.b *= y;
this.c *= x;
this.d *= y;
this.x *= x;
this.y *= y;
};
Matrix.prototype.resolveFloat = function() {
var mat = this.clone();
var r = {};
r.x = mat.x;
r.y = mat.y;
// mat.x=0;
// mat.y=0;
var hd = mat.getRotationX();
r.radian = hd;
var du = hd * 180 / Math.PI;
if(du < 0) {
du = du + 360;
}
r.rotation = du;
mat.rotate(-hd);
r.scaleX = mat.a;
r.scaleY = mat.d;
return r;
};
Matrix矩阵表示:
| a | c | x|
| b | d | y|
| 0 | 0 | 1 |
单位矩阵时 a b c d 为 1,0,0,1
矩阵满足
- 乘法结合律: (AB)C=A(BC)
- 乘法左分配律:(A+B)C=AC+BC
- 乘法右分配律:C(A+B)=CA+CB
- 对数乘的结合性k(AB)=(kA)B=A(kB**)
- 转置 (AB)T=BTAT
在html显示对象使用矩阵相乘
假如 AB=ab矩阵相等,求其中任意一个矩阵.
ABBT=abBT //同时乘一个BT
A=abBT;
ATAB=ATab; ///同时乘一个AT
B=ATab;
同理 a b .所以如下
A=abBT;
B=ATab;
a=ABbT;
b=aTAB;
其他同理。