-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vec2.js
213 lines (189 loc) · 3.97 KB
/
Vec2.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Create a new Vec2
*
* @constructor
* @param {number} x - X coordinate
* @param {number} y - Y coordinate
*/
function Vec2(x, y) {
this.x = x;
this.y = y;
}
/**
* @return {string} Stringification of Vec2
*/
Vec2.prototype.toString = function() {
return '{' + this.x + ',' + this.y + '}';
};
/**
* Add to another Vec2
*
* @param {Vec2} other
* @return {Vec2} Result
*/
Vec2.prototype.add = function(other) {
return new Vec2(this.x + other.x, this.y + other.y);
};
/**
* Multiply by a constant
*
* @param {number} scale
* @return {Vec2} Result
*/
Vec2.prototype.mul = function(scale) {
if (typeof(scale) !== 'number' || isNaN(scale)) {
throw new Error('nope');
}
return new Vec2(scale * this.x, scale * this.y);
};
/**
* Add to another Vec2
*
* @param {Vec2} other
* @return {Vec2} Result
*/
Vec2.prototype.sub = function(other) {
return new Vec2(this.x - other.x, this.y - other.y);
};
/**
* Divide by a constant
*
* @param {number} scale
* @return {Vec2} Result
*/
Vec2.prototype.div = function(scale) {
if (scale === 0) {
throw new Error('nah');
}
return this.mul(1 / scale);
};
/**
* @return {number} Magnitude of the vector squared
*/
Vec2.prototype.magSq = function() {
return this.x * this.x + this.y * this.y;
};
/**
* @return {number} Magnitude of the vector
*/
Vec2.prototype.mag = function() {
return Math.sqrt(this.magSq());
};
/**
* @param {number} newMag Desired magnitude
* @return {Vec2} Vector with magnitude `newMag`
*/
Vec2.prototype.setMag = function(newMag) {
if (this.magSq() === 0) {
return this;
}
return this.normalize().mul(newMag);
};
/**
* @return {Vec2} Vector with magnitude 1
*/
Vec2.prototype.normalize = function() {
return this.div(this.mag());
};
/**
* @param {number} maxMag Maximum allowed magnitude
* @return {Vec2} Vector with at most magnitude `maxMag`
*/
Vec2.prototype.limit = function(maxMag) {
if (this.magSq() < maxMag * maxMag) {
return this;
}
return this.setMag(maxMag);
};
/**
* @return {number} Heading (angle) of vector
*/
Vec2.prototype.heading = function() {
return Math.atan2(this.y, this.x);
};
/**
* @param {Vec2} other
* @return {number} Dot product with other vec
*/
Vec2.prototype.dot = function(other) {
return this.x * other.x + this.y * other.y;
};
/**
* @param {Vec2} other
* @return {number} Cross product with other vec
*/
Vec2.prototype.cross = function(other) {
return this.x * other.y - this.y * other.x;
};
/**
* @param {Vec2} other
* @return {boolean} Int approximation of coordinates equal
*/
Vec2.prototype.intEq = function(other) {
return Math.round(this.x) === Math.round(other.x) &&
Math.round(this.y) === Math.round(other.y);
};
var Direction = {
RIGHT: new Vec2(1, 0),
LEFT: new Vec2(-1, 0),
UP: new Vec2(0, 1),
DOWN: new Vec2(0, -1)
};
/**
* All directions
*/
Direction.ALL = [
Direction.RIGHT,
Direction.LEFT,
Direction.UP,
Direction.DOWN
];
/**
* Attach directions to Vec2
*/
Vec2.Direction = Direction;
/**
* Get the integer-valued cardinal direction between two points
*
* @param {Vec2} startPos
* @param {Vec2} endPos
* @return {Vec2?} direction
*/
Vec2.getDirectionBetween = function(startPos, endPos) {
var dx = endPos.x - startPos.x;
var dy = endPos.y - startPos.y;
if (dx > 0.01) {
return Direction.RIGHT;
}
if (dx < -0.01) {
return Direction.LEFT;
}
if (dy > 0.01) {
return Direction.UP;
}
if (dy < -0.01) {
return Direction.DOWN;
}
return null;
};
/**
* Get the cardinal direction opposite another
*
* @param {Vec2} direction
* @return {Vec2} opposite direction
*/
Vec2.getOppositeDirection = function(direction) {
if (direction === Direction.LEFT) {
return Direction.RIGHT;
}
if (direction === Direction.RIGHT) {
return Direction.LEFT;
}
if (direction === Direction.UP) {
return Direction.DOWN;
}
if (direction === Direction.DOWN) {
return Direction.UP;
}
throw new Error('Non-direction passed to getOppositeDirection');
};