/
vector.js
109 lines (105 loc) · 2.17 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
define(['cclass'], function(cclass) {
var VectorClass = {
constructor: function Vector(x,y) {
this.x = x;
this.y = y;
},
set: function(x,y) {
this.x = x;
this.y = y;
return this;
},
add: function(x,y) {
this.x += x;
this.y += y;
return this;
},
substract: function(x,y) {
this.x -= x;
this.y -= y;
return this;
},
multiply: function(f) {
this.x *= f;
this.y *= f;
return this;
},
divide: function(f) {
this.x /= f;
this.y /= f;
return this;
},
length: function() {
return Math.sqrt(this.x*this.x+this.y*this.y);
},
distanceTo: function(x,y) {
var dx = this.x-x;
dx*=dx;
var dy = this.y-y;
dy*=dy;
//console.log(dy);
return Math.sqrt(dx+dy);
},
normalize: function() {
var l = this.length();
if (l == 0.0) { throw "Normalizing 0!" }
this.x /= l;
this.y /= l;
return this;
},
normalizeOr: function(x,y) {
var l = this.length();
if (l === 0.0) { this.x = x; this.y = y; return this; }
this.x /= l;
this.y /= l;
return this;
},
normalizeOrZero: function() {
return this.normalizeOr(0,0);
},
dot: function(x,y) {
return this.x * x + this.y * y;
},
negate: function() {
this.x = -this.x;
this.y = -this.y;
return this;
},
normalRight: function() {
var tmp = this.x;
this.x = -this.y;
this.y = tmp;
return this;
},
normalLeft: function() {
var tmp = this.x;
this.x = this.y;
this.y = -tmp;
return this;
},
equals: function(x,y) {
return this.x === x && this.y === y;
},
toString: function() {
return 'Vector(' + this.x + ',' + this.y + ')';
},
rotate: function(r) {
var l = this.length();
var r = Math.atan2(this.y, this.x)+r;
this.x = Math.cos(r)*l;
this.y = Math.sin(r)*l;
}
};
// Add helper vector-functions.
['set','add','substract','multiply','divide','normalizeOr','dot','equals','distanceTo'].forEach(function(name) {
var f = VectorClass[name];
VectorClass[name+'V'] = function(v) {
return f.call(this, v.x, v.y);
};
});
var Vector = cclass(VectorClass);
Vector.zero = new Vector(0,0);
Vector.xaxis = new Vector(1,0);
Vector.yaxis = new Vector(0,1);
return Vector;
});