-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_node.js
84 lines (81 loc) · 1.49 KB
/
vector_node.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
79
80
81
82
83
function vector(x,y){
this.x=x;
this.y=y;
this.set=function(x,y){
this.x=x;
this.y=y;
}
this.get=function(){
return new vector(x,y);
}
this.mag=function(){
return Math.sqrt(x*x+y*y);
}
this.angle=function(){
return Math.atan2(y,x);
}
this.add=function(other){
this.x+=other.x;
this.y+=other.y;
return this;
}
this.sub=function(other){
this.x-=other.x;
this.y-=other.y;
return this;
}
this.mult=function(n){
this.x*=n;
this.y*=n;
return this;
}
this.div=function(n){
this.x/=n;
this.y/=n;
}
this.dist=function(other){
var dx=this.x-other.x;
var dy=this.y-other.y;
return Math.sqrt(dx*dx+dy*dy);
}
this.norm=function(){
var m=thisi.mag();
if(m!=0&&m!=1){
this.div(m);
}
return this;
}
this.limit=function(min, max){
if (this.mag()>max){
this.norm();
this.mult(max);
} else if (this.mag<min){
this.norm();
this.mult(min);
}
}
this.vectorAdd = function(v1,v2){
return new vector(v1.x+v2.x,v1.y+v2.y);
}
this.vectorSub = function(v1,v2){
return new vector(v1.x-v2.x,v1.y-v2.y);
}
this.vectorDist = function(v1,v2){
var dx=v1.x-v2.x;
var dy=v1.y-v2.y;
return Math.sqrt(dx*dx+dy*dy);
}
//return this;
}
/*module.exports.vectorAdd = function(v1,v2){
return new vector(v1.x+v2.x,v1.y+v2.y);
}
module.exports.vectorSub = function(v1,v2){
return new vector(v1.x-v2.x,v1.y-v2.y);
}
module.exports.vectorDist = function(v1,v2){
var dx=v1.x-v2.x;
var dy=v1.y-v2.y;
return Math.sqrt(dx*dx+dy*dy);
}*/
module.exports=vector;