-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMath.js
324 lines (307 loc) · 8.14 KB
/
Math.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
export class Line {
constructor(a = new Vector(), b = new Vector()) {
this.start = a
this.end = b
}
getLength() {
return Math.sqrt(
this.getHorizontalLength() ** 2 + this.getVerticalLength() ** 2
)
}
getHorizontalLength() {
return Math.abs(this.start.x - this.end.x)
}
getVerticalLength() {
return Math.abs(this.start.y - this.end.y)
}
addVec(vec) {
return new Line(this.start.add_vec(vec), this.end.add_vec(vec))
}
splitAtX(x) {
let k = (this.start.y - this.end.y) / (this.start.x - this.end.x)
let b = this.start.y - k * this.start.x
let y = k * x + b
if (this.start.x < this.end.x)
return {
left: new Line(
new Vector(this.start.x, +this.start.y.toFixed(2)),
new Vector(x, +y.toFixed(2))
),
right: new Line(new Vector(x, y), new Vector(this.end.x, this.end.y)),
}
return {
left: new Line(new Vector(this.end.x, this.end.y), new Vector(x, y)),
right: new Line(new Vector(x, y), new Vector(this.start.x, this.start.y)),
}
}
toString() {
return `Line: ${this.start.toStringFixed(2)} -> ${this.end.toStringFixed(
2
)}`
}
}
export class Vector {
constructor(x = 0.0, y = 0.0) {
this.x = x
this.y = y
}
shiftInPlace() {
this.x >>= 0
this.y >>= 0
return this
}
scale(factor) {
let result = new Vector(this.x, this.y)
result.x *= factor
result.y *= factor
return result
}
add_vec(vector) {
return new Vector(this.x + vector.x, this.y + vector.y)
}
sub_vec(vector) {
return new Vector(this.x - vector.x, this.y - vector.y)
}
div_vec(vector) {
return new Vector(this.x / vector.x, this.y / vector.y)
}
vectorize() {
let distance =
this.x && this.y ? Math.abs(this.x / 2) + Math.abs(this.y / 2) : 0
let hipon = (this.x ** 2 + this.y ** 2) ** 0.5
let scale = distance / hipon || 1
return new Vector(this.x * scale, this.y * scale)
}
multiply(vector) {
return new Vector(this.x * vector.x, this.y * vector.y)
}
hasZeroAxis() {
return this.x == 0 || this.y == 0
}
isZero() {
return this.x == 0 && this.y == 0
}
toString() {
return `(${this.x}, ${this.y})`
}
toStringFixed(fixed = 2) {
return `(${+this.x.toFixed(fixed)}, ${+this.y.toFixed(fixed)})`
}
copy() {
return new Vector(this.x, this.y)
}
min() {
return this.x < this.y ? this.x : this.y
}
max() {
return this.x > this.y ? this.x : this.y
}
has(number) {
return this.x == number || this.y == number
}
static parseString(str, sep = ",") {
let values = str
.replaceAll("\n", sep)
.split(sep)
.map((value) => Number(value.trim()))
return new Vector(values[0], values[1]).shiftInPlace()
}
reverse() {
return new Vector(this.y, this.x)
}
average() {
return (this.x + this.y) / 2
}
/**
* rotate clockwise
* @param degrees
* @returns {Vector}
*/
rotate(degrees){
const radians = degrees * (Math.PI/180)
const x = Math.cos(radians) * this.x - Math.sin(radians) * this.y
const y = Math.sin(radians) * this.x + Math.cos(radians) * this.y
return new Vector(x,y)
}
round(prec){
return new Vector(+this.x.toFixed(prec),+this.y.toFixed(prec))
}
}
export class Vector4d {
constructor(x = 0.0, y = 0.0, z = 0.0, a = 0.0) {
this.x = x
this.y = y
this.z = z
this.a = a
}
shiftInPlace() {
this.x >>= 0
this.y >>= 0
this.z >>= 0
this.a >>= 0
return this
}
scale(factor) {
let result = this.copy()
result.x *= factor
result.y *= factor
result.a *= factor
result.z *= factor
return result
}
add_vec(vector) {
return new Vector4d(
this.x + vector.x,
this.y + vector.y,
this.z + vector.z,
this.a + vector.a
)
}
sub_vec(vector) {
return new Vector4d(
this.y - vector.y,
this.z - vector.z,
this.x - vector.x,
this.a - vector.a
)
}
div_vec(vector) {
return new Vector4d(
this.x / vector.x,
this.y / vector.y,
this.z / vector.z,
this.a / vector.a
)
}
multiply(vector) {
return new Vector4d(
this.x * vector.x,
this.y * vector.y,
this.z * vector.z,
this.a * vector.a
)
}
hasZeroAxis() {
return this.x == 0 || this.y == 0 || (this.a == 0) | (this.z == 0)
}
isZero() {
return this.x == 0 && this.y == 0 && this.a == 0 && this.z == 0
}
toString() {
return `(${this.x}, ${this.y}, ${this.z}, ${this.a})`
}
toStringFixed(fixed = 2) {
return `(${+this.x.toFixed(fixed)}, ${+this.y.toFixed(
fixed
)}), ${+this.z.toFixed(fixed)}), ${+this.a.toFixed(fixed)})`
}
copy() {
return new Vector4d(this.x, this.y, this.z, this.a)
}
min() {
let min = this.x < this.y ? this.x : this.y
min = min < this.z ? min : this.z
min = min < this.a ? min : this.a
return min
}
max() {
let max = this.x > this.y ? this.x : this.y
max = max > this.z ? max : this.z
max = max > this.a ? max : this.a
return max
}
has(number) {
return (
this.x == number ||
this.y == number ||
this.z == number ||
this.a == number
)
}
}
export class M3x3 {
constructor() {
this.matrix = [1, 0, 0, 0, 1, 0, 0, 0, 1]
}
multiply(m) {
var output = new M3x3()
output.matrix = [
this.matrix[M3x3.M00] * m.matrix[M3x3.M00] +
this.matrix[M3x3.M10] * m.matrix[M3x3.M01] +
this.matrix[M3x3.M20] * m.matrix[M3x3.M02],
this.matrix[M3x3.M01] * m.matrix[M3x3.M00] +
this.matrix[M3x3.M11] * m.matrix[M3x3.M01] +
this.matrix[M3x3.M21] * m.matrix[M3x3.M02],
this.matrix[M3x3.M02] * m.matrix[M3x3.M00] +
this.matrix[M3x3.M12] * m.matrix[M3x3.M01] +
this.matrix[M3x3.M22] * m.matrix[M3x3.M02],
this.matrix[M3x3.M00] * m.matrix[M3x3.M10] +
this.matrix[M3x3.M10] * m.matrix[M3x3.M11] +
this.matrix[M3x3.M20] * m.matrix[M3x3.M12],
this.matrix[M3x3.M01] * m.matrix[M3x3.M10] +
this.matrix[M3x3.M11] * m.matrix[M3x3.M11] +
this.matrix[M3x3.M21] * m.matrix[M3x3.M12],
this.matrix[M3x3.M02] * m.matrix[M3x3.M10] +
this.matrix[M3x3.M12] * m.matrix[M3x3.M11] +
this.matrix[M3x3.M22] * m.matrix[M3x3.M12],
this.matrix[M3x3.M00] * m.matrix[M3x3.M20] +
this.matrix[M3x3.M10] * m.matrix[M3x3.M21] +
this.matrix[M3x3.M20] * m.matrix[M3x3.M22],
this.matrix[M3x3.M01] * m.matrix[M3x3.M20] +
this.matrix[M3x3.M11] * m.matrix[M3x3.M21] +
this.matrix[M3x3.M21] * m.matrix[M3x3.M22],
this.matrix[M3x3.M02] * m.matrix[M3x3.M20] +
this.matrix[M3x3.M12] * m.matrix[M3x3.M21] +
this.matrix[M3x3.M22] * m.matrix[M3x3.M22],
]
return output
}
transition(x, y) {
var output = new M3x3()
output.matrix = [
this.matrix[M3x3.M00],
this.matrix[M3x3.M01],
this.matrix[M3x3.M02],
this.matrix[M3x3.M10],
this.matrix[M3x3.M11],
this.matrix[M3x3.M12],
x * this.matrix[M3x3.M00] +
y * this.matrix[M3x3.M10] +
this.matrix[M3x3.M20],
x * this.matrix[M3x3.M01] +
y * this.matrix[M3x3.M11] +
this.matrix[M3x3.M21],
x * this.matrix[M3x3.M02] +
y * this.matrix[M3x3.M12] +
this.matrix[M3x3.M22],
]
return output
}
scale(x, y) {
var output = new M3x3()
output.matrix = [
this.matrix[M3x3.M00] * x,
this.matrix[M3x3.M01] * x,
this.matrix[M3x3.M02] * x,
this.matrix[M3x3.M10] * y,
this.matrix[M3x3.M11] * y,
this.matrix[M3x3.M12] * y,
this.matrix[M3x3.M20],
this.matrix[M3x3.M21],
this.matrix[M3x3.M22],
]
return output
}
getFloatArray() {
return new Float32Array(this.matrix)
}
}
M3x3.M00 = 0
M3x3.M01 = 1
M3x3.M02 = 2
M3x3.M10 = 3
M3x3.M11 = 4
M3x3.M12 = 5
M3x3.M20 = 6
M3x3.M21 = 7
M3x3.M22 = 8