class Matrix2D {
static readonly scaleMatrix: ThreeNum[] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
static readonly translationMatrix: ThreeNum[] = [[1, 0, 1], [0, 1, 1], [0, 0, 1]];
static readonly rotationMatrix: ThreeNum[] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
static det2(matrix: detMatrixData) {
return matrix[0]._x * matrix[1]._y - matrix[0]._y * matrix[1]._x;
}
static getTranslationMatrix(x: number, y: number) {
let translationMatrix: ThreeNum[] = Matrix2D.translationMatrix.map((dot: ThreeNum) => [...dot])
translationMatrix[0][2] = x;
translationMatrix[1][2] = y;
return translationMatrix
}
static translation(x: number, y: number, dot: ThreeNum) {
let translationMatrix = Matrix2D.getTranslationMatrix(x, y)
return Matrix2D.matrixTrans(dot, translationMatrix)
}
static getScaleMatrix(scope: number) {
let scaleMatrix: ThreeNum[] = Matrix2D.scaleMatrix.map((dot: ThreeNum) => [...dot])
scaleMatrix[0][0] = scaleMatrix[0][0] * scope;
scaleMatrix[1][1] = scaleMatrix[1][1] * scope;
return scaleMatrix
}
static scale(scope: number, dot: ThreeNum) {
let scaleMatrix: ThreeNum[] = Matrix2D.getScaleMatrix(scope)
return Matrix2D.matrixTrans(dot, scaleMatrix)
}
static getRotationMatrix(angle: number) {
let rotationMatrix: ThreeNum[] = Matrix2D.rotationMatrix.map((dot: ThreeNum) => [...dot])
rotationMatrix[0][0] = Math.round(Math.cos(angle) * 100000) / 100000
rotationMatrix[0][1] = -Math.round(Math.sin(angle) * 100000) / 100000
rotationMatrix[1][0] = Math.round(Math.sin(angle) * 100000) / 100000
rotationMatrix[1][1] = Math.round(Math.cos(angle) * 100000) / 100000
console.log('rotationMatrix!!!', rotationMatrix)
return rotationMatrix
}
static rotation(angle: number, dot: ThreeNum, clockwise: boolean = true) {
let rotationMatrix: ThreeNum[] = Matrix2D.getRotationMatrix(angle)
return Matrix2D.matrixTrans(dot, rotationMatrix)
}
static matrixTrans(dot: ThreeNum, matrix: ThreeNum[]): ThreeNum {
let res: ThreeNum = [0, 0, 0]
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
// console.log(matrix[i][j] * dot[j])
res[i] += matrix[i][j] * dot[j]
}
}
return res
}
static matrixMul(matrix1: number[][], matrix2: number[][]) {
let res = new Array(matrix1.length);
for (var i = 0; i < matrix1.length; i++) {
res[i] = new Array(matrix2[0].length);
for (var j = 0; j < matrix2[0].length; j++) {
res[i][j] = 0;
for (var k = 0; k < matrix2.length; k++) {
res[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return res;
}
}
平移,旋转,缩放三种矩阵变换
最新推荐文章于 2023-07-25 19:15:00 发布