forked from Grant-Nelson/ThreeDart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix2.dart
More file actions
162 lines (138 loc) · 5.21 KB
/
Copy pathMatrix2.dart
File metadata and controls
162 lines (138 loc) · 5.21 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
part of ThreeDart.Math;
/// A math structure for storing and manipulating a Matrix 2x2.
class Matrix2 {
double _m11, _m21;
double _m12, _m22;
/// Constructs a new [Matrix2] with the given initial values.
Matrix2(double m11, double m21,
double m12, double m22) {
this.set(m11, m21,
m12, m22);
}
/// Constructs a 2x2 identity matrix.
factory Matrix2.identity() =>
new Matrix2(1.0, 0.0,
0.0, 1.0);
/// Constructs a 2x2 scalar matrix.
///
/// [sx] scales the x axis and [sy] scales the y axis.
factory Matrix2.scale(double sx, double sy) =>
new Matrix2(sx, 0.0,
0.0, sy);
/// Constructs a 2x2 rotation matrix.
///
/// The given [angle] is in radians.
/// This matrix rotates counter-clockwise around a virtual Z axis.
factory Matrix2.rotate(double angle) {
double c = math.cos(angle);
double s = math.sin(angle);
return new Matrix2(c, -s,
s, c);
}
/// Constructs a 2x2 matrix from a trimmed 3x3 matrix.
///
/// The 3rd row and column are ignored from [mat].
factory Matrix2.fromMatrix3(Matrix3 mat) =>
new Matrix2(mat._m11, mat._m21,
mat._m12, mat._m22);
/// Constructs a 2x2 matrix from a trimmed 4x4 matrix.
///
/// The 3rd and 4th row and column are ignored from [mat].
factory Matrix2.fromMatrix4(Matrix4 mat) =>
new Matrix2(mat._m11, mat._m21,
mat._m12, mat._m22);
/// Constructs a new [Matrix2] instance given a list of 4 doubles.
/// By default the list is in row major order.
factory Matrix2.fromList(List<double> values, [bool columnMajor = false]) {
assert(values.length == 4);
if (columnMajor) {
return new Matrix2(values[0], values[2],
values[1], values[3]);
} else {
return new Matrix2(values[0], values[1],
values[2], values[3]);
}
}
/// Sets the [Matrix2] with the given values.
void set(double m11, double m21,
double m12, double m22) {
this._m11 = m11; this._m21 = m21;
this._m12 = m12; this._m22 = m22;
}
/// Gets the list of 4 doubles for the matrix.
/// By default the list is in row major order.
List<double> toList([bool columnMajor = false]) {
if (columnMajor) {
return [this._m11, this._m12,
this._m21, this._m22];
} else {
return [this._m11, this._m21,
this._m12, this._m22];
}
}
/// The 1st row and 1st column of the matrix, XX.
double get m11 => this._m11;
set m11(double m11) => this._m11 = m11;
/// The 1st row and 2nd column of the matrix, XY.
double get m21 => this._m21;
set m21(double m21) => this._m21 = m21;
/// The 2nd row and 1st column of the matrix, YX.
double get m12 => this._m12;
set m12(double m12) => this._m12 = m12;
/// The 2nd row and 2nd column of the matrix, YY.
double get m22 => this._m22;
set m22(double m22) => this._m22 = m22;
/// Gets the determinant of this matrix.
double det() =>
this._m11 * this._m22 - this._m21 * this._m12;
/// Gets a copy of this matrix.
Matrix2 copy() =>
new Matrix2(this._m11, this._m21,
this._m12, this._m22);
/// Gets the transposition of this matrix.
Matrix2 transpose() =>
new Matrix2(this._m11, this._m12,
this._m21, this._m22);
/// Gets the inverse of this matrix.
Matrix2 inverse() {
double det = this.det();
if (Comparer.equals(det, 0.0)) return new Matrix2.identity();
double q = 1.0 / det;
return new Matrix2( this._m22 * q, -this._m12 * q,
-this._m21 * q, this._m11 * q);
}
/// Multiplies this matrix by the [other] matrix.
Matrix2 operator *(Matrix2 other) => new Matrix2(
this._m11 * other._m11 + this._m21 * other._m12,
this._m11 * other._m21 + this._m21 * other._m22,
this._m12 * other._m11 + this._m22 * other._m12,
this._m12 * other._m21 + this._m22 * other._m22);
/// Transposes the given [vec] with this matrix.
Vector2 transVec2(Vector2 vec) => new Vector2(
this._m11 * vec.dx + this._m21 * vec.dy,
this._m12 * vec.dx + this._m22 * vec.dy);
/// Transposes the given [pnt] with this matrix.
Point2 transPnt2(Point2 pnt) => new Point2(
this._m11 * pnt.x + this._m21 * pnt.y,
this._m12 * pnt.x + this._m22 * pnt.y);
/// Determines if the given [other] variable is a [Matrix2] equal to this metrix.
///
/// The equality of the doubles is tested with the current [Comparer] method.
bool operator ==(var other) {
if (identical(this, other)) return true;
if (other is! Matrix2) return false;
Matrix2 mat = other as Matrix2;
if (!Comparer.equals(mat._m11, this._m11)) return false;
if (!Comparer.equals(mat._m21, this._m21)) return false;
if (!Comparer.equals(mat._m12, this._m12)) return false;
if (!Comparer.equals(mat._m22, this._m22)) return false;
return true;
}
/// Gets the string for this matrix.
String toString([String indent = "", int fraction = 3, int whole = 0]) {
List<String> col1 = formatColumn([this._m11, this._m12], fraction, whole);
List<String> col2 = formatColumn([this._m21, this._m22], fraction, whole);
return '[${col1[0]}, ${col2[0]},\n' +
'$indent ${col1[1]}, ${col2[1]}]';
}
}