-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVector2.js
39 lines (29 loc) · 860 Bytes
/
Vector2.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
function Vector2(x = 0, y = 0){
this.x = x;
this.y = y;
}
Vector2.prototype.copy = function(){
return new Vector2(this.x, this.y);
}
Vector2.prototype.add = function(vector){
return new Vector2(this.x + vector.x, this.y + vector.y);
}
Vector2.prototype.addTo = function(vector){
this.x += vector.x;
this.y += vector.y;
}
Vector2.prototype.subtract = function(vector){
return new Vector2(this.x - vector.x, this.y - vector.y);
}
Vector2.prototype.mult = function(scalar){
return new Vector2(this.x * scalar, this.y * scalar);
}
Vector2.prototype.dot = function(vector){
return this.x * vector.x + this.y * vector.y;
}
Vector2.prototype.length = function(){
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
}
Vector2.prototype.distFrom = function(vector){
return this.subtract(vector).length();
}