blob: 3cebf6d2be0e0025f863746490c8550eaa858ccc (
plain)
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
|
export class Vector {
constructor(readonly x: number, readonly y: number, readonly z: number) {}
add(v: Vector): Vector {
return new Vector(this.x + v.x, this.y + v.y, this.z + v.z);
}
addScalar(n: number): Vector {
return new Vector(this.x + n, this.y + n, this.z + n);
}
subtract(v: Vector): Vector {
return new Vector(this.x - v.x, this.y - v.y, this.z - v.z);
}
multiply(n: number): Vector {
return new Vector(this.x * n, this.y * n, this.z * n);
}
dotProduct(v: Vector): number {
return this.x * v.x + this.y * v.y + this.z * v.z;
}
normalise(): Vector {
const vecLength = this.norm();
return new Vector(
this.x / vecLength,
this.y / vecLength,
this.z / vecLength
);
}
norm(): number {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
}
|