2020-07-23 11:00:46 +08:00
|
|
|
|
module es {
|
2020-07-24 16:57:26 +08:00
|
|
|
|
export var matrixPool = [];
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
|
/**
|
2020-07-23 11:00:46 +08:00
|
|
|
|
* 表示右手3 * 3的浮点矩阵,可以存储平移、缩放和旋转信息。
|
2020-06-08 11:49:45 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
export class Matrix2D extends egret.Matrix {
|
|
|
|
|
|
public get m11(): number {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this.a;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public set m11(value: number) {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.a = value;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public get m12(): number {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this.b;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public set m12(value: number) {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.b = value;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public get m21(): number {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this.c;
|
|
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public set m21(value: number) {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.c = value;
|
|
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public get m22(): number {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this.d;
|
|
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public set m22(value: number) {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.d = value;
|
|
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
public get m31(): number {
|
|
|
|
|
|
return this.tx;
|
|
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public set m31(value: number) {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.tx = value;
|
|
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public get m32(): number {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this.ty;
|
|
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public set m32(value: number) {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.ty = value;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-24 16:57:26 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 从对象池中取出或创建一个新的Matrix对象。
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static create(): Matrix2D {
|
2020-07-24 16:57:26 +08:00
|
|
|
|
let matrix = matrixPool.pop();
|
|
|
|
|
|
if (!matrix)
|
|
|
|
|
|
matrix = new Matrix2D();
|
|
|
|
|
|
return matrix;
|
2020-07-23 13:25:10 +08:00
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public identity(): Matrix2D {
|
2020-07-24 16:57:26 +08:00
|
|
|
|
this.a = this.d = 1;
|
|
|
|
|
|
this.b = this.c = this.tx = this.ty = 0;
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
public translate(dx: number, dy: number): Matrix2D {
|
2020-07-24 16:57:26 +08:00
|
|
|
|
this.tx += dx;
|
|
|
|
|
|
this.ty += dy;
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
public scale(sx: number, sy: number): Matrix2D {
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (sx !== 1) {
|
2020-07-24 16:57:26 +08:00
|
|
|
|
this.a *= sx;
|
|
|
|
|
|
this.c *= sx;
|
|
|
|
|
|
this.tx *= sx;
|
|
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (sy !== 1) {
|
2020-07-24 16:57:26 +08:00
|
|
|
|
this.b *= sy;
|
|
|
|
|
|
this.d *= sy;
|
|
|
|
|
|
this.ty *= sy;
|
|
|
|
|
|
}
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
public rotate(angle: number): Matrix2D {
|
2020-07-24 16:57:26 +08:00
|
|
|
|
angle = +angle;
|
|
|
|
|
|
if (angle !== 0) {
|
|
|
|
|
|
angle = angle / DEG_TO_RAD;
|
|
|
|
|
|
let u = Math.cos(angle);
|
|
|
|
|
|
let v = Math.sin(angle);
|
|
|
|
|
|
let ta = this.a;
|
|
|
|
|
|
let tb = this.b;
|
|
|
|
|
|
let tc = this.c;
|
|
|
|
|
|
let td = this.d;
|
|
|
|
|
|
let ttx = this.tx;
|
|
|
|
|
|
let tty = this.ty;
|
|
|
|
|
|
this.a = ta * u - tb * v;
|
|
|
|
|
|
this.b = ta * v + tb * u;
|
|
|
|
|
|
this.c = tc * u - td * v;
|
|
|
|
|
|
this.d = tc * v + td * u;
|
|
|
|
|
|
this.tx = ttx * u - tty * v;
|
|
|
|
|
|
this.ty = ttx * v + tty * u;
|
|
|
|
|
|
}
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
public invert(): Matrix2D {
|
2020-07-24 16:57:26 +08:00
|
|
|
|
this.$invertInto(this);
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 创建一个新的matrix, 它包含两个矩阵的和。
|
2020-07-23 13:25:10 +08:00
|
|
|
|
* @param matrix
|
2020-07-23 11:00:46 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public add(matrix: Matrix2D): Matrix2D {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m11 += matrix.m11;
|
|
|
|
|
|
this.m12 += matrix.m12;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m21 += matrix.m21;
|
|
|
|
|
|
this.m22 += matrix.m22;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m31 += matrix.m31;
|
|
|
|
|
|
this.m32 += matrix.m32;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-19 00:38:37 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
public substract(matrix: Matrix2D): Matrix2D {
|
|
|
|
|
|
this.m11 -= matrix.m11;
|
|
|
|
|
|
this.m12 -= matrix.m12;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m21 -= matrix.m21;
|
|
|
|
|
|
this.m22 -= matrix.m22;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m31 -= matrix.m31;
|
|
|
|
|
|
this.m32 -= matrix.m32;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public divide(matrix: Matrix2D): Matrix2D {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m11 /= matrix.m11;
|
|
|
|
|
|
this.m12 /= matrix.m12;
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m21 /= matrix.m21;
|
|
|
|
|
|
this.m22 /= matrix.m22;
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m31 /= matrix.m31;
|
|
|
|
|
|
this.m32 /= matrix.m32;
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public multiply(matrix: Matrix2D): Matrix2D {
|
|
|
|
|
|
let m11 = (this.m11 * matrix.m11) + (this.m12 * matrix.m21);
|
|
|
|
|
|
let m12 = (this.m11 * matrix.m12) + (this.m12 * matrix.m22);
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
let m21 = (this.m21 * matrix.m11) + (this.m22 * matrix.m21);
|
|
|
|
|
|
let m22 = (this.m21 * matrix.m12) + (this.m22 * matrix.m22);
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
let m31 = (this.m31 * matrix.m11) + (this.m32 * matrix.m21) + matrix.m31;
|
|
|
|
|
|
let m32 = (this.m31 * matrix.m12) + (this.m32 * matrix.m22) + matrix.m32;
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m11 = m11;
|
|
|
|
|
|
this.m12 = m12;
|
2020-07-08 18:12:17 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m21 = m21;
|
|
|
|
|
|
this.m22 = m22;
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this.m31 = m31;
|
|
|
|
|
|
this.m32 = m32;
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public determinant() {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
return this.m11 * this.m22 - this.m12 * this.m21;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-07-24 16:57:26 +08:00
|
|
|
|
|
|
|
|
|
|
public release(matrix: Matrix2D) {
|
|
|
|
|
|
if (!matrix)
|
|
|
|
|
|
return;
|
|
|
|
|
|
matrixPool.push(matrix);
|
|
|
|
|
|
}
|
2020-06-29 15:41:02 +08:00
|
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|