-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.js
133 lines (129 loc) · 3.18 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
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.prototype = {
set: function(x, y) {
this.x = x;
this.y = y;
return this;
},
magnitude: function() {
return Math.sqrt(this.dot(this));
},
angle: function() {
return Math.atan2(this.x, this.y);
},
normalized: function() {
return this.over(this.magnitude());
},
normalize: function() {
return this.overEquals(this.magnitude());
},
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) {
return this.minus(that).magnitude();
},
angleTo: function(that) {
return Math.acos(this.dot(that) / (this.magnitude() * that.magnitude()))
},
lerp: function(that, t) {
return new Vector(
that.x * t + this.x * (1 - t),
that.y * t + this.y * (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 + ')';
},
clone: function() {
return new Vector(this.x, this.y);
}
};
Vector.zero = function() { return new Vector(0, 0) };
Vector.i = function() { return new Vector(1, 0) };
Vector.j = function() { return new Vector(0, 1) };
Vector.NaN = function() { return new Vector(Number.NaN, Number.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