blob: 230dad955b7436b59b1bdf5cfe3f0b06d96e8cbe (
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
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
|
export default class Vec2 {
constructor(x, y) {
this.x = x;
this.y = y;
}
static zero() {
return new Vec2(0, 0);
}
static fromHeading(radians) {
return new Vec2(Math.cos(radians), Math.sin(radians));
}
static fromHeadingLength(radians, length) {
return Vec2.fromHeading(radians).scale(length);
}
length() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
}
heading() {
return Math.atan2(this.y, this.x);
}
scale(factor) {
this.x *= factor;
this.y *= factor;
return this;
}
scaled(factor) {
return new Vec2(this.x * factor, this.y * factor);
}
ofLength(newLength) {
return this.normalized().scale(newLength);
}
add(v2) {
return new Vec2(this.x + v2.x, this.y + v2.y);
}
sub(v2) {
return new Vec2(this.x - v2.x, this.y - v2.y);
}
normalize() {
var len = this.length();
this.x = this.x / len;
this.y = this.y / len;
return this;
}
normalized() {
var len = this.length();
return new Vec2(this.x / len, this.y / len);
}
}
|