-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvector.js
166 lines (160 loc) · 4.01 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict";
function Vector(x, y) {
this.x = x;
this.y = y;
}
Vector.fromPolarCoords = function(r, theta) {
if(theta === undefined)
theta = r, r = 1;
return new Vector(r*Math.cos(theta), r*Math.sin(theta));
}
Vector.fromString = function(string) {
var components = string.split(',');
var x = parseFloat(components[0].replace(/[^\d\.-]/g, ''));
var y = parseFloat(components[1].replace(/[^\d\.-]/g, ''));
if(isNaN(x) || isNaN(y))
return null;
else
return new Vector(x, y);
}
Vector.ify = function(data) {
if(data instanceof Object && 'x' in data && 'y' in data)
return new Vector(data.x, data.y);
else if(typeof data == "string") {
return Vector.fromString(data);
}
}
Vector.prototype = {
set: function(x, y) {
this.x = x;
this.y = y;
return this;
},
isFinite: function() {
return isFinite(this.x) && isFinite(this.y);
},
get length() {
return Math.sqrt(this.lengthSquared);
},
set length(x) {
this.overEquals(x/this.length);
},
get lengthSquared() {
return this.dot(this);
},
set lengthSquared(x) {
this.overEquals(Math.sqrt(x/this.lengthSquared));
},
angle: function() {
return Math.atan2(this.x, this.y);
},
forceMaxLength: function(f) {
var l = this.length;
if(!isFinite(l)) this.set(0, 0);
else if(f < l) this.overEquals(f / l);
},
normalized: function() {
return this.over(this.length);
},
normalize: function() {
return this.overEquals(this.length);
},
plus: function(that) {
return new Vector(this.x + that.x, this.y + that.y);
},
plusEquals: function(that) {
this.x += that.x;
this.y += that.y;
return this;
},
minus: function(that) {
return new Vector(this.x - that.x, this.y - that.y);
},
minusEquals: function(that) {
this.x -= that.x;
this.y -= that.y;
return this;
},
times: function(factor) {
if(factor instanceof Vector)
return new Vector(this.x * factor.x, this.y * factor.y);
else
return new Vector(this.x * factor, this.y * factor);
},
timesEquals: function(factor) {
if(factor instanceof Vector) {
this.x *= factor.x;
this.y *= factor.y;
} else {
this.x *= factor;
this.y *= factor;
}
return this;
},
over: function(factor) {
return new Vector(this.x / factor, this.y / factor);
},
overEquals: function(factor) {
if(factor instanceof Vector) {
this.x /= factor.x;
this.y /= factor.y;
} else {
this.x /= factor;
this.y /= factor;
}
return this;
},
negated: function() {
return new Vector(-this.x, -this.y);
},
negate: function() {
this.x = -this.x;
this.y = -this.y;
return this;
},
dot: function(that) {
return this.x * that.x + this.y * that.y;
},
distanceTo: function(that, squared) {
var dx = this.x - that.x;
var dy = this.y - that.y;
var d = dx*dx + dy*dy;
return squared ? d : Math.sqrt(d);
},
angleTo: function(that) {
return Math.acos(this.dot(that) / Math.sqrt(this.lengthSquared * that.lengthSquared))
},
lerp: function(that, t) {
return that.times(t).plus(this.times(1 - t));
},
perp: function() {
return new Vector(-this.y, this.x);
},
toDiagonalMatrix: function() {
return new Matrix(this.x, 0, 0, this.y);
},
toString: function() {
return '(' + this.x + ',' + this.y + ')';
},
toFixed: function(precision) {
return '(' + this.x.toFixed(precision) + ',' + this.y.toFixed(precision) + ')';
},
clone: function() {
return new Vector(this.x, this.y);
}
};
Object.defineProperties(Vector, {
zero: {get: function() { return new Vector(0, 0) } },
i: {get: function() { return new Vector(1, 0) } },
j: {get: function() { return new Vector(0, 1) } },
NaN: {get: function() { return new Vector(NaN, NaN) } }
});
Vector.prototype['+'] = Vector.prototype.plus;
Vector.prototype['-'] = Vector.prototype.minus;
Vector.prototype['*'] = Vector.prototype.times;
Vector.prototype['/'] = Vector.prototype.over;
Vector.prototype['+='] = Vector.prototype.plusEquals;
Vector.prototype['-='] = Vector.prototype.minusEquals;
Vector.prototype['*='] = Vector.prototype.timesEquals;
Vector.prototype['/='] = Vector.prototype.overEquals;
module.exports = Vector;