-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathVector.js
151 lines (149 loc) · 3.08 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
/**
* Vector.js v1.0.0
* @author Anurag Hazra
* @borrows p5.Vector
* @param {number} x
* @param {number} y
*/
function Vector(x, y) {
this.x = x || 0;
this.y = y || 0;
}
// Static Functions
Vector.dist = function (v1, v2) {
return v1.dist(v2);
}
Vector.distSq = function (v1, v2) {
return v1.distSq(v2);
}
Vector.sub = function (v1, v2) {
return new Vector(v1.x - v2.x, v1.y - v2.y);
};
Vector.add = function (v1, v2) {
return new Vector(v1.x + v2.x, v1.y + v2.y);
};
Vector.fromAngle = function (angle) {
let v = new Vector(0, 0);
v.x = Math.cos(angle);
v.y = Math.sin(angle);
return v;
}
Vector.random2D = function (v) {
return Vector.fromAngle(Math.random() * Math.PI * 180);
}
Vector.prototype = {
add: function (x, y) {
if (arguments.length === 1) {
this.x += x.x;
this.y += x.y;
} else if (arguments.length === 2) {
this.x += x;
this.y += y;
}
return this;
},
sub: function (x, y) {
if (arguments.length === 1) {
this.x -= x.x;
this.y -= x.y;
} else if (arguments.length === 2) {
this.x -= x;
this.y -= y;
}
return this;
},
mult: function (v) {
if (typeof v === 'number') {
this.x *= v;
this.y *= v;
} else {
this.x *= v.x;
this.y *= v.y;
}
return this;
},
div: function (v) {
if (typeof v === 'number') {
this.x /= v;
this.y /= v;
} else {
this.x /= v.x;
this.y /= v.y;
}
return this;
},
setAngle: function(angle) {
var len = this.mag();
this.x = Math.cos(angle) * len;
this.y = Math.sin(angle) * len;
},
mag: function () {
return Math.sqrt(this.x * this.x + this.y * this.y);
},
magSq: function () {
return (this.x * this.x + this.y * this.y);
},
setXY : function (x, y) {
this.x = x;
this.y = y;
return this;
},
setMag: function (value) {
this.normalize();
this.mult(value);
return this;
},
normalize: function () {
let m = this.mag();
if (m > 0) {
this.div(m);
}
return this;
},
limit: function (max) {
if (this.mag() > max) {
this.normalize();
this.mult(max);
}
return this;
},
heading: function () {
return (-Math.atan2(-this.y, this.x));
},
dist: function (v) {
let dx = this.x - v.x;
let dy = this.y - v.y;
return Math.sqrt(dx * dx + dy * dy);
},
distSq: function (v) {
let dx = this.x - v.x;
let dy = this.y - v.y;
return (dx * dx + dy * dy);
},
copy: function () {
return new Vector(this.x, this.y);
},
negative: function () {
this.x = -this.x;
this.y = -this.y;
return this;
},
array: function () {
return [this.x, this.y];
},
toString: function () {
return "[" + this.x + ", " + this.y + ", " + this.z + "]";
},
project : function (v) {
var coeff = ((this.x * v.x) + (this.y * v.y)) / ((v.x * v.x) + (v.y * v.y));
this.x = coeff * v.x;
this.y = coeff * v.y;
return this;
},
rotate : function (a) {
var b = this.heading() + a;
var c = this.mag();
this.x = Math.cos(b) * c;
this.y = Math.sin(b) * c;
}
}