-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vector.js
42 lines (33 loc) · 837 Bytes
/
Vector.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
function Vector(x,y) {
this.x = x;
this.y = y;
}
Vector.prototype.print = function() {
console.log("< "+this.x+", "+this.y+" >");
};
Vector.prototype.add = function(vec) {
return new Vector(this.x+vec.x,this.y+vec.y);
};
Vector.prototype.addxy = function(x,y) {
return new Vector(this.x+x,this.y+y);
};
Vector.prototype.sub = function(vec) {
return new Vector(this.x-vec.x,this.y-vec.y);
};
Vector.prototype.subxy = function(x,y) {
return new Vector(this.x-x,this.y-y);
};
Vector.prototype.dot = function(vec) {
return this.x*vec.x + this.y*vec.y;
};
Vector.prototype.dotxy = function(x,y) {
return this.x*x + this.y*y;
};
Vector.prototype.mag = function() {
return Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2));
};
Vector.prototype.normalize = function() {
var mag = this.mag();
this.x /= mag;
this.y /= mag;
};