-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2D.js
64 lines (61 loc) · 1.43 KB
/
Vector2D.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
class Vector2D {
constructor(x, y) {
this.x = x;
this.y = y;
}
Add(vec) {
this.x += vec.x;
this.y += vec.y;
}
Subtract(vec) {
this.x -= vec.x;
this.y -= vec.y;
}
Multiply(vec) {
this.x *= vec.x;
this.y *= vec.y;
}
Divide(vec) {
this.x /= vec.x;
this.y /= vec.y;
}
Copy() {
return new Vector2D(this.x, this.y);
}
static get Zero() {
return new Vector2D(0, 0);
}
static Equals(a, b) {
return (a.x == b.x) && (a.y == b.y);
}
static Add(vec1, vec2) {
return new Vector2D(vec1.x + vec2.x, vec1.y + vec2.y);
}
static Subtract(vec1, vec2) {
return new Vector2D(vec1.x - vec2.x, vec1.y - vec2.y);
}
static Multiply(vec1, vec2) {
return new Vector2D(vec1.x * vec2.x, vec1.y * vec2.y);
}
static Divide(vec1, vec2) {
return new Vector2D(vec1.x / vec2.x, vec1.y / vec2.y);
}
static Ceil(vec) {
return new Vector2D(Math.ceil(vec.x), Math.ceil(vec.y));
}
static Floor(vec) {
return new Vector2D(Math.floor(vec.x), Math.floor(vec.y));
}
static Magnitude(vec) {
return Math.sqrt((vec.x * vec.x) + (vec.y * vec.y));
}
static Normalize(vec) {
let magnitude = Vector2D.Magnitude(vec);
return new Vector2D(vec.x / magnitude, vec.y / magnitude);
}
static Distance(a, b) {
let dx = b.x - a.x;
let dy = b.y - a.y;
return Math.sqrt((dx * dx) + (dy * dy));
}
}