-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector-tools.js
214 lines (178 loc) · 4.83 KB
/
vector-tools.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
export const UP = [0, 1, 0];
export const FORWARD = [0, 0, 1];
export const RIGHT = [1, 0, 0];
export function transpose(matrix) {
const result = [];
for(let row = 0; row < matrix.length; row++){
const newRow = [];
for(let col = 0; col < matrix[row].length; col++){
newRow.push(matrix[col][row]);
}
result.push(newRow);
}
return result;
}
export function getVectorMagnitude(vec) {
return Math.sqrt(vec.reduce((sum, x) => sum + x ** 2, 0));
}
export function addVector(a, b) {
return a.map((x, i) => x + b[i]);
}
export function subtractVector(a, b) {
return a.map((x, i) => x - b[i]);
}
export function scaleVector(vec, s) {
return vec.map(x => x * s);
}
export function divideVector(vec, s) {
return vec.map(x => x / s);
}
export function normalizeVector(vec) {
return divideVector(vec, getVectorMagnitude(vec));
}
//3x3
export function crossVector(a, b) {
return [
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0]
];
}
//vectors must have same length!
export function dotVector(a, b) {
return a.reduce((sum, _, i) => sum + (a[i] * b[i]), 0);
}
export function invertVector(vec) {
return vec.map(x => -x);
}
export function reflectVector(vec, normal) {
return [
vec[0] - 2 * dotVector(vec, normal) * normal[0],
vec[1] - 2 * dotVector(vec, normal) * normal[1],
vec[2] - 2 * dotVector(vec, normal) * normal[2],
];
}
export function getDeterminantSubmatrix(matrix, row, col){
const result = [];
for(let i = 0; i < matrix.length; i++){
const newRow = [];
if(i === row) continue;
for(let j = 0; j < matrix[i].length; j++){
if(j === col) continue;
newRow.push(matrix[i][j]);
}
result.push(newRow);
}
return result;
}
export function getDeterminant(matrix){
let result = 0;
if (matrix.length === 2 && matrix[0].length === 2) return (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
for(let i = 0; i < matrix[0].length; i++){
if(i % 2 === 0){
result += matrix[0][i] * getDeterminant(getDeterminantSubmatrix(matrix, 0, i));
} else {
result -= matrix[0][i] * getDeterminant(getDeterminantSubmatrix(matrix, 0, i));
}
}
return result;
}
export function getCofactor(matrix, row, col){
const determinant = getDeterminant(getDeterminantSubmatrix(matrix, row, col));
return (row + col) % 2 === 1
? -determinant
: determinant;
}
export function getCofactorMatrix(matrix){
const result = [];
for(let row = 0; row < matrix.length; row++){
const newRow = [];
for (let col = 0; col < matrix[row].length; col++) {
newRow.push(getCofactor(matrix, row, col));
}
result.push(newRow);
}
return result;
}
export function getAdjugate(matrix){
return transpose(getCofactorMatrix(matrix));
}
export function getInverse(matrix){
return scaleMatrix(getAdjugate(matrix), 1 / getDeterminant(matrix));
}
export function mapMatrix(matrix, func){
const result = [];
for (let row = 0; row < matrix.length; row++) {
const newRow = [];
for (let col = 0; col < matrix[row].length; col++) {
newRow.push(func(matrix[row][col], row, col));
}
result.push(newRow);
}
return result;
}
export function addMatrix(a, b){
return mapMatrix(a, (x, r, c) => x + b[r][c]);
}
export function subtractMatrix(a, b) {
return mapMatrix(a, (x, r, c) => x - b[r][c]);
}
export function scaleMatrix(matrix, s) {
return mapMatrix(matrix, (x, r, c) => x * s);
}
export function divideMatrix(matrix, s) {
return mapMatrix(matrix, (x, r, c) => x / s);
}
export function trimMatrix(matrix, height, width) {
const result = [];
for (let row = 0; row < height; row++) {
const newRow = [];
for (let col = 0; col < width; col++) {
newRow.push(matrix[row][col]);
}
result.push(newRow);
}
return result;
}
export function getColumn(matrix, col){
const result = [];
for(let row = 0; row < matrix.length; row++){
result.push(matrix[row][col]);
}
return result;
}
//A's rows must equal B's columns, no check is given
export function multiplyMatrix(a, b) {
const result = [];
for (let row = 0; row < a.length; row++) {
const newRow = [];
for (let col = 0; col < b[row].length; col++) {
newRow.push(dotVector(a[row], getColumn(b, col)));
}
result.push(newRow);
}
return result;
}
export function asMatrix(array, height, width) {
const result = [];
for (let row = 0; row < height; row++) {
const newRow = [];
for (let col = 0; col < width; col++) {
newRow.push(array[row * width + col]);
}
result.push(newRow);
}
return result;
}
export function multiplyMatrixVector(vector, matrix){
if(vector.length != matrix.length || vector.length != matrix[0].length) throw new Error('Invalid matrix dimensions');
const resultVector = new Array(vector.length);
for(let row = 0; row < matrix.length; row++){
let result = 0;
for(let col = 0; col < matrix[row].length; col++){
result += vector[col] * matrix[row][col];
}
resultVector[row] = result;
}
return resultVector;
}