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