-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.ts
84 lines (84 loc) · 2.18 KB
/
vector.ts
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
export default class Vector {
coord!: number[]
constructor(...coord: number[]) {
if (!(this instanceof Vector)) return new Vector(...coord)
this.coord = coord
while (coord.length > 0 && coord[coord.length - 1] === 0) coord.pop()
}
[Symbol.iterator]() {
return this.coord[Symbol.iterator]()
}
static polar(r: number = 0, ...ang: number[]) {
const coord: number[] = []
let cos = r
for (let i = ang.length - 1; i >= 0; i--) {
coord.unshift(cos * Math.sin(ang[i]))
cos *= Math.cos(ang[i])
}
coord.unshift(cos)
return new Vector(...coord)
}
polar(): number[] {
const ret: number[] = []
let a = 0
for (let i = 1; i < this.coord.length; i++) {
const y = this.coord[i]
if (i === 1) {
const x = this.coord[i - 1]
ret.push(Math.atan2(y, x))
a = x * x
} else {
ret.push(Math.atan2(y, Math.sqrt(a)))
}
a += y * y
}
ret.unshift(Math.sqrt(a))
return ret
}
equal(v2: Vector) {
return this.dist(v2) === 0
}
add(...vect: Vector[]): Vector {
vect.push(this)
const coord = []
const len = Math.max(...vect.map(v => v.coord.length))
for (let i = 0; i < len; i++)
coord[i] = vect.map(v => v.coord[i] || 0).reduce((a, b) => a + b)
return new Vector(...coord)
}
sub(v2: Vector): Vector {
return this.add(v2.inv())
}
mul(a: number): Vector {
return new Vector(...this.coord.map(c => c * a))
}
div(a: number): Vector {
return this.mul(1 / a)
}
inv(): Vector {
return this.mul(-1)
}
mix(v: Vector, a = 0.5): Vector {
return this.mul(1 - a).add(v.mul(a))
}
len(): number
len(a: number): Vector
len(a?: number): number | Vector {
if (a == null) return Math.hypot(...this.coord)
return this.mul(a / this.len())
}
norm(): Vector {
return this.len(1)
}
dist(v: Vector): number {
return this.sub(v).len()
}
dot(...vect: Vector[]): number {
vect.push(this)
const coord = []
const len = Math.max(...vect.map(v => v.coord.length))
for (let i = 0; i < len; i++)
coord[i] = vect.map(v => v.coord[i] || 0).reduce((a, b) => a * b)
return coord.reduce((a, b) => a + b)
}
}