-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_vector.js
78 lines (74 loc) · 1.38 KB
/
fast_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var FastVector = function(x,y){
this.x = x;
this.y = y;
};
FastVector.prototype = {
add: function (B,internal) {
var nx, ny;
if (typeof(B)=='number'){
nx = this.x+B;
ny = this.y+B;
}else{
nx = this.x+B.x;
ny = this.y+B.y;
}
return new FastVector(nx,ny);
},
add_: function(B) {
if (typeof(B)=='number'){
this.x+=B; this.y+=B;
}else{
this.x+=B.x; this.y+=B.y;
}
return this;
},
dot: function(B) {
return ((this.x*B.x)+(this.y*B.y));
},
length: function() {
return Math.sqrt((this.x*this.x)+(this.y*this.y));
},
multiply: function(B) {
var nx, ny;
if (typeof(B)=='number'){
nx = this.x*B; ny = this.y*B;
}else{
nx = this.x*B.x; ny = this.y*B.y;
}
return new FastVector(nx,ny);
},
multiply_: function(B) {
if (typeof(B)=='number'){
this.x*=B; this.y*=B;
}else{
this.x*=B.x; this.y*=B.y;
}
return this;
},
squaredLength: function(args) {
return (this.x*this.x)+(this.y*this.y);
},
sum: function(){
return this.x+this.y;
},
subtract: function(B) {
var nx, ny;
if (typeof(B) == 'number'){
nx = this.x-B; ny = this.y-B;
}else{
nx = this.x-B.x; ny = this.y-B.y;
}
return new FastVector(nx,ny);
},
subtract_: function(B) {
if (typeof(B) == 'number'){
this.x-=B; this.y-=B;
}else{
this.x-=B.x; this.y-=B.y;
}
return this;
},
toString: function() {
return "["+this.x+","+this.y+"]";
}
};