-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.js
91 lines (89 loc) · 2.07 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
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
84
85
86
87
88
89
90
91
(function(){
// 二维矢量类
var Vector2 = function(x,y){
this.x = x;
this.y = y;
};
Vector2.prototype = {
copy: function(){
return new Vector2(this.x,this.y);
},
length: function(){
return Math.sqrt(this.x*this.x+this.y+this.y);
},
sqrLength: function(){
return this.x*this.x+this.y*this.y;
},
normalize: function(){
var inv = 1/this.length();
return new Vector2(this.x*inv,this.y*inv);
},
negate: function(){
return new Vector2(-this.x,-this.y);
},
add: function(v){
return new Vector2(this.x+v.x,this.y+v.y);
},
subtract: function(v){
return new Vector2(this.x-v.x,this.y-v.y);
},
multiply: function(f){
return new Vector2(this.x*f,this.y*f);
},
divide: function(f){
var invf = 1/f;
return new Vector2(this.x*invf,this.y*invf);
},
dot: function(v){
return new Vector2(this.x*v.x,this.y*v.y);
},
};
Vector2.zero = new Vector2(0,0);
// 三维矢量类
var Vector3 = function(x,y,z){
this.x = x;
this.y = y;
this.z = z;
};
Vector3.prototype = {
copy: function(){
return new Vector3(this.x,this.y,this.z);
},
length: function(){
return Math.sqrt(this.x*this.x+this.y+this.y+this.z*this.z);
},
sqrLength: function(){
return this.x*this.x+this.y*this.y+this.z*this.z;
},
normalize: function(){
var inv = 1/this.length();
return new Vector3(this.x*inv,this.y*inv,this.z*inv);
},
negate: function(){
return new Vector3(-this.x,-this.y,-this.z);
},
add: function(v){
return new Vector3(this.x+v.x,this.y+v.y,this.z+v.z);
},
subtract: function(v){
return new Vector3(this.x-v.x,this.y-v.y,this.z-v.z);
},
multiply: function(f){
return new Vector3(this.x*f,this.y*f,this.z*f);
},
divide: function(f){
var invf = 1/f;
return new Vector3(this.x*invf,this.y*invf,this.z*invf);
},
dot: function(v){
return new Vector3(this.x*v.x,this.y*v.y,this.z*v.z);
},
cross: function(v){
var X = this.y*v.z-this.z*v.y,
Y = this.z*v.x-this.x*v.z,
Z = this.x*v.y-this.y*v.x;
return new Vector3(X,Y,Z);
},
};
Vector3.zero = new Vector3(0,0,0);
})();