-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.ts
68 lines (68 loc) · 1.5 KB
/
vec.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
class Vec {
public x: number;
public y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
neg() {
return Vec.mult(this, -1);
}
add(v: Vec) {
this.x += v.x;
this.y += v.y;
return this;
}
sub(v: Vec) {
this.x -= v.x;
this.y -= v.y;
return this;
}
mult(scale: number) {
this.x *= scale;
this.y *= scale;
return this;
}
div(scale: number) {
this.x /= scale;
this.y /= scale;
return this;
}
dot(v: Vec) {
return (v.x * this.x) + (v.y * this.y);
}
sqrMag() {
return this.dot(this);
}
mag() {
return Math.sqrt(this.sqrMag());
}
normalized() {
const mag = this.mag();
return new Vec(this.x / mag, this.y / mag);
}
static add(u: Vec, v: Vec) {
return new Vec(u.x + v.x, u.y + v.y);
}
static sub(u: Vec, v: Vec) {
return new Vec(u.x - v.x, u.y - v.y);
}
static mult(v: Vec, scale: number) {
return new Vec(v.x * scale, v.y * scale);
}
static div(v: Vec, scale: number) {
return new Vec(v.x / scale, v.y / scale);
}
static dist(u: Vec, v: Vec) {
return Vec.sub(u, v).mag();
}
static sqrDist(u: Vec, v: Vec) {
return Vec.sub(u, v).sqrMag();
}
static dot(u: Vec, v: Vec) {
return u.dot(v);
}
static cross(u: Vec, v: Vec) {
return u.x * v.y - u.y * v.x
}
}