Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/math/vec2.js
4057 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Defines a 2-element vector class that can be used for
9
* coordinate math, useful for animation systems and point manipulation.
10
*
11
* Vec2 objects inherit from goog.math.Coordinate and may be used wherever a
12
* Coordinate is required. Where appropriate, Vec2 functions accept both Vec2
13
* and Coordinate objects as input.
14
*/
15
16
goog.provide('goog.math.Vec2');
17
18
goog.require('goog.math');
19
goog.require('goog.math.Coordinate');
20
21
goog.require('goog.utils');
22
23
24
25
/**
26
* Class for a two-dimensional vector object and assorted functions useful for
27
* manipulating points.
28
*
29
* @param {number} x The x coordinate for the vector.
30
* @param {number} y The y coordinate for the vector.
31
* @struct
32
* @constructor
33
* @extends {goog.math.Coordinate}
34
*/
35
goog.math.Vec2 = function(x, y) {
36
'use strict';
37
/**
38
* X-value
39
* @type {number}
40
*/
41
this.x = x;
42
43
/**
44
* Y-value
45
* @type {number}
46
*/
47
this.y = y;
48
};
49
goog.utils.inherits(goog.math.Vec2, goog.math.Coordinate);
50
51
52
/**
53
* @return {!goog.math.Vec2} A random unit-length vector.
54
*/
55
goog.math.Vec2.randomUnit = function() {
56
'use strict';
57
var angle = Math.random() * Math.PI * 2;
58
return new goog.math.Vec2(Math.cos(angle), Math.sin(angle));
59
};
60
61
62
/**
63
* @return {!goog.math.Vec2} A random vector inside the unit-disc.
64
*/
65
goog.math.Vec2.random = function() {
66
'use strict';
67
var mag = Math.sqrt(Math.random());
68
var angle = Math.random() * Math.PI * 2;
69
70
return new goog.math.Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
71
};
72
73
74
/**
75
* Returns a new Vec2 object from a given coordinate.
76
* @param {!goog.math.Coordinate} a The coordinate.
77
* @return {!goog.math.Vec2} A new vector object.
78
*/
79
goog.math.Vec2.fromCoordinate = function(a) {
80
'use strict';
81
return new goog.math.Vec2(a.x, a.y);
82
};
83
84
85
/**
86
* @return {!goog.math.Vec2} A new vector with the same coordinates as this one.
87
* @override
88
*/
89
goog.math.Vec2.prototype.clone = function() {
90
'use strict';
91
return new goog.math.Vec2(this.x, this.y);
92
};
93
94
95
/**
96
* Returns the magnitude of the vector measured from the origin.
97
* @return {number} The length of the vector.
98
*/
99
goog.math.Vec2.prototype.magnitude = function() {
100
'use strict';
101
return Math.hypot(this.x, this.y);
102
};
103
104
105
/**
106
* Returns the squared magnitude of the vector measured from the origin.
107
* NOTE(brenneman): Leaving out the square root is not a significant
108
* optimization in JavaScript.
109
* @return {number} The length of the vector, squared.
110
*/
111
goog.math.Vec2.prototype.squaredMagnitude = function() {
112
'use strict';
113
return this.x * this.x + this.y * this.y;
114
};
115
116
117
/**
118
* @param {number} sx The scale factor to use for the x dimension.
119
* @param {number=} opt_sy The scale factor to use for the y dimension.
120
* @return {!goog.math.Vec2} This vector after scaling.
121
* @override
122
*/
123
// Since the implementation of Coordinate.scale() returns "this", we
124
// can reuse that implementation here, and just recast the return type.
125
goog.math.Vec2.prototype.scale =
126
/** @type {function(number, number=):!goog.math.Vec2} */
127
(goog.math.Coordinate.prototype.scale);
128
129
130
/**
131
* Reverses the sign of the vector. Equivalent to scaling the vector by -1.
132
* @return {!goog.math.Vec2} The inverted vector.
133
*/
134
goog.math.Vec2.prototype.invert = function() {
135
'use strict';
136
this.x = -this.x;
137
this.y = -this.y;
138
return this;
139
};
140
141
142
/**
143
* Normalizes the current vector to have a magnitude of 1.
144
* @return {!goog.math.Vec2} The normalized vector.
145
*/
146
goog.math.Vec2.prototype.normalize = function() {
147
'use strict';
148
return this.scale(1 / this.magnitude());
149
};
150
151
152
/**
153
* Adds another vector to this vector in-place.
154
* @param {!goog.math.Coordinate} b The vector to add.
155
* @return {!goog.math.Vec2} This vector with `b` added.
156
*/
157
goog.math.Vec2.prototype.add = function(b) {
158
'use strict';
159
this.x += b.x;
160
this.y += b.y;
161
return this;
162
};
163
164
165
/**
166
* Subtracts another vector from this vector in-place.
167
* @param {!goog.math.Coordinate} b The vector to subtract.
168
* @return {!goog.math.Vec2} This vector with `b` subtracted.
169
*/
170
goog.math.Vec2.prototype.subtract = function(b) {
171
'use strict';
172
this.x -= b.x;
173
this.y -= b.y;
174
return this;
175
};
176
177
178
/**
179
* Rotates this vector in-place by a given angle, specified in radians.
180
* @param {number} angle The angle, in radians.
181
* @return {!goog.math.Vec2} This vector rotated `angle` radians.
182
*/
183
goog.math.Vec2.prototype.rotate = function(angle) {
184
'use strict';
185
var cos = Math.cos(angle);
186
var sin = Math.sin(angle);
187
var newX = this.x * cos - this.y * sin;
188
var newY = this.y * cos + this.x * sin;
189
this.x = newX;
190
this.y = newY;
191
return this;
192
};
193
194
195
/**
196
* Rotates a vector by a given angle, specified in radians, relative to a given
197
* axis rotation point. The returned vector is a newly created instance - no
198
* in-place changes are done.
199
* @param {!goog.math.Vec2} v A vector.
200
* @param {!goog.math.Vec2} axisPoint The rotation axis point.
201
* @param {number} angle The angle, in radians.
202
* @return {!goog.math.Vec2} The rotated vector in a newly created instance.
203
*/
204
goog.math.Vec2.rotateAroundPoint = function(v, axisPoint, angle) {
205
'use strict';
206
var res = v.clone();
207
return res.subtract(axisPoint).rotate(angle).add(axisPoint);
208
};
209
210
211
/** @override */
212
goog.math.Vec2.prototype.equals = function(b) {
213
'use strict';
214
if (this === b) {
215
return true;
216
}
217
return b instanceof goog.math.Vec2 && !!b && this.x == b.x && this.y == b.y;
218
};
219
220
221
/**
222
* Returns the distance between two vectors.
223
* @param {!goog.math.Coordinate} a The first vector.
224
* @param {!goog.math.Coordinate} b The second vector.
225
* @return {number} The distance.
226
*/
227
goog.math.Vec2.distance = goog.math.Coordinate.distance;
228
229
230
/**
231
* Returns the squared distance between two vectors.
232
* @param {!goog.math.Coordinate} a The first vector.
233
* @param {!goog.math.Coordinate} b The second vector.
234
* @return {number} The squared distance.
235
*/
236
goog.math.Vec2.squaredDistance = goog.math.Coordinate.squaredDistance;
237
238
239
/**
240
* Compares vectors for equality.
241
* @param {!goog.math.Coordinate} a The first vector.
242
* @param {!goog.math.Coordinate} b The second vector.
243
* @return {boolean} Whether the vectors have the same x and y coordinates.
244
*/
245
goog.math.Vec2.equals = goog.math.Coordinate.equals;
246
247
248
/**
249
* Returns the sum of two vectors as a new Vec2.
250
* @param {!goog.math.Coordinate} a The first vector.
251
* @param {!goog.math.Coordinate} b The second vector.
252
* @return {!goog.math.Vec2} The sum vector.
253
*/
254
goog.math.Vec2.sum = function(a, b) {
255
'use strict';
256
return new goog.math.Vec2(a.x + b.x, a.y + b.y);
257
};
258
259
260
/**
261
* Returns the difference between two vectors as a new Vec2.
262
* @param {!goog.math.Coordinate} a The first vector.
263
* @param {!goog.math.Coordinate} b The second vector.
264
* @return {!goog.math.Vec2} The difference vector.
265
*/
266
goog.math.Vec2.difference = function(a, b) {
267
'use strict';
268
return new goog.math.Vec2(a.x - b.x, a.y - b.y);
269
};
270
271
272
/**
273
* Returns the dot-product of two vectors.
274
* @param {!goog.math.Coordinate} a The first vector.
275
* @param {!goog.math.Coordinate} b The second vector.
276
* @return {number} The dot-product of the two vectors.
277
*/
278
goog.math.Vec2.dot = function(a, b) {
279
'use strict';
280
return a.x * b.x + a.y * b.y;
281
};
282
283
284
/**
285
* Returns the determinant of two vectors.
286
* @param {!goog.math.Vec2} a The first vector.
287
* @param {!goog.math.Vec2} b The second vector.
288
* @return {number} The determinant of the two vectors.
289
*/
290
goog.math.Vec2.determinant = function(a, b) {
291
'use strict';
292
return a.x * b.y - a.y * b.x;
293
};
294
295
296
/**
297
* Returns a new Vec2 that is the linear interpolant between vectors a and b at
298
* scale-value x.
299
* @param {!goog.math.Coordinate} a Vector a.
300
* @param {!goog.math.Coordinate} b Vector b.
301
* @param {number} x The proportion between a and b.
302
* @return {!goog.math.Vec2} The interpolated vector.
303
*/
304
goog.math.Vec2.lerp = function(a, b, x) {
305
'use strict';
306
return new goog.math.Vec2(
307
goog.math.lerp(a.x, b.x, x), goog.math.lerp(a.y, b.y, x));
308
};
309
310
311
/**
312
* Returns a new Vec2 that is a copy of the vector a, but rescaled by a factors
313
* sx and sy in the x and y directions. If only sx is specified, then y is
314
* scaled by the same factor as x.
315
* @param {!goog.math.Coordinate} a Vector a.
316
* @param {number} sx X scale factor.
317
* @param {number=} sy Y scale factor (optional).
318
* @return {!goog.math.Vec2} A new rescaled vector.
319
*/
320
goog.math.Vec2.rescaled = function(a, sx, sy = sx) {
321
return new goog.math.Vec2(a.x * sx, a.y * sy);
322
};
323
324