-
Notifications
You must be signed in to change notification settings - Fork 2
/
Vector.js
51 lines (35 loc) · 1.06 KB
/
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
43
44
45
46
47
48
49
50
51
function Vector2(x, y) {
// in innerboards/sec
this.x = x;
this.y = y;
}
Vector2.prototype = {
add: function(v) {
return new Vector(this.x+v.x, this.y+that.y);
},
subtract: function(v) {
return new Vector(this.x-v.x, this.y-that.y);
},
dot: function(v) {
return new Vector(this.x*v.x+this.y*that.y);
},
cross: function(v) {
return this.x*v.y + this.y*v.x;
},
multiply: function(v) {
if (v instanceof Vector) { return this.dot(that.v); }
else { return this.scale(v); }
},
scale: function(n) {
return new Vector(this.x * n, this.y * n);
},
copy: function() {
return new Vector(this.x, this.y)
},
negative: function() {
return new Vector(-this.x, -this.y);
},
length: function() {
return MATH.sqrt(this.cross(this))
}
};