-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.js
91 lines (80 loc) · 2.04 KB
/
matrix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
/**
* Creates a new matrix.
*
* @class A 3x3 matrix.
* @param {Number[][]} coef
* @returns {Matrix}
*/
function Matrix(coef) {
/** The coefficients of the matrix. @type {Number[][]} */
this.coef = coef || [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
}
Matrix.createIdentityMatrix = function() {
return new Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]);
};
Matrix.createTranslationMatrix = function(x, y) {
return new Matrix([
[1, 0, x],
[0, 1, y],
[0, 0, 1]
]);
};
Matrix.createRotationMatrix = function(theta) {
var cosTheta = Math.cos(theta), sinTheta = Math.sin(theta);
return new Matrix([
[cosTheta, sinTheta, 0],
[-sinTheta, cosTheta, 0],
[0, 0, 1]
]);
};
Matrix.createScalingMatrix = function(sx, sy) {
return new Matrix([
[sx, 0, 0],
[0, sy, 0],
[0, 0, 1]
]);
};
Matrix.prototype.multiply = function(otherMatrix) {
var newMatrix = new Matrix(), i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
newMatrix.coef[i][j] =
this.coef[i][0] * otherMatrix.coef[0][j] +
this.coef[i][1] * otherMatrix.coef[1][j] +
this.coef[i][2] * otherMatrix.coef[2][j];
}
}
return newMatrix;
};
Matrix.prototype.translate = function(x, y) {
return this.multiply(Matrix.createTranslationMatrix(x, y));
};
Matrix.prototype.rotate = function(theta) {
return this.multiply(Matrix.createRotationMatrix(theta));
};
Matrix.prototype.scale = function(sx, sy) {
return this.multiply(Matrix.createScalingMatrix(sx, sy));
};
Matrix.prototype.transform = function(point) {
return new Point(
this.coef[0][0] * point.x + this.coef[0][1] * point.y + this.coef[0][2],
this.coef[1][0] * point.x + this.coef[1][1] * point.y + this.coef[1][2]);
};
Matrix.prototype.clone = function() {
var newMatrix = new Matrix(), i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
newMatrix.coef[i][j] = this.coef[i][j];
}
}
return newMatrix;
};