Debian Codegraph @

closure-library/closure/goog/vec/vec2.js

raw
1 // Copyright 2012 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 Definition of 2 element vectors. This follows the same design
17 * patterns as Vec3 and Vec4.
18 *
19 */
20
21 goog.provide('goog.vec.Vec2');
22
23 /** @suppress {extraRequire} */
24 goog.require('goog.vec');
25
26
27 /** @typedef {goog.vec.Float32} */ goog.vec.Vec2.Float32;
28 /** @typedef {goog.vec.Float64} */ goog.vec.Vec2.Float64;
29 /** @typedef {goog.vec.Number} */ goog.vec.Vec2.Number;
30 /** @typedef {goog.vec.AnyType} */ goog.vec.Vec2.AnyType;
31
32
33 /**
34 * Creates a 2 element vector of Float32. The array is initialized to zero.
35 *
36 * @return {!goog.vec.Vec2.Float32} The new 2 element array.
37 */
38 goog.vec.Vec2.createFloat32 = function() {
39 return new Float32Array(2);
40 };
41
42
43 /**
44 * Creates a 2 element vector of Float64. The array is initialized to zero.
45 *
46 * @return {!goog.vec.Vec2.Float64} The new 2 element array.
47 */
48 goog.vec.Vec2.createFloat64 = function() {
49 return new Float64Array(2);
50 };
51
52
53 /**
54 * Creates a 2 element vector of Number. The array is initialized to zero.
55 *
56 * @return {!goog.vec.Vec2.Number} The new 2 element array.
57 */
58 goog.vec.Vec2.createNumber = function() {
59 var a = new Array(2);
60 goog.vec.Vec2.setFromValues(a, 0, 0);
61 return a;
62 };
63
64
65 /**
66 * Creates a new 2 element FLoat32 vector initialized with the value from the
67 * given array.
68 *
69 * @param {goog.vec.Vec2.AnyType} vec The source 2 element array.
70 * @return {!goog.vec.Vec2.Float32} The new 2 element array.
71 */
72 goog.vec.Vec2.createFloat32FromArray = function(vec) {
73 var newVec = goog.vec.Vec2.createFloat32();
74 goog.vec.Vec2.setFromArray(newVec, vec);
75 return newVec;
76 };
77
78
79 /**
80 * Creates a new 2 element Float32 vector initialized with the supplied values.
81 *
82 * @param {number} vec0 The value for element at index 0.
83 * @param {number} vec1 The value for element at index 1.
84 * @return {!goog.vec.Vec2.Float32} The new vector.
85 */
86 goog.vec.Vec2.createFloat32FromValues = function(vec0, vec1) {
87 var a = goog.vec.Vec2.createFloat32();
88 goog.vec.Vec2.setFromValues(a, vec0, vec1);
89 return a;
90 };
91
92
93 /**
94 * Creates a clone of the given 2 element Float32 vector.
95 *
96 * @param {goog.vec.Vec2.Float32} vec The source 2 element vector.
97 * @return {!goog.vec.Vec2.Float32} The new cloned vector.
98 */
99 goog.vec.Vec2.cloneFloat32 = goog.vec.Vec2.createFloat32FromArray;
100
101
102 /**
103 * Creates a new 2 element Float64 vector initialized with the value from the
104 * given array.
105 *
106 * @param {goog.vec.Vec2.AnyType} vec The source 2 element array.
107 * @return {!goog.vec.Vec2.Float64} The new 2 element array.
108 */
109 goog.vec.Vec2.createFloat64FromArray = function(vec) {
110 var newVec = goog.vec.Vec2.createFloat64();
111 goog.vec.Vec2.setFromArray(newVec, vec);
112 return newVec;
113 };
114
115
116 /**
117 * Creates a new 2 element Float64 vector initialized with the supplied values.
118 *
119 * @param {number} vec0 The value for element at index 0.
120 * @param {number} vec1 The value for element at index 1.
121 * @return {!goog.vec.Vec2.Float64} The new vector.
122 */
123 goog.vec.Vec2.createFloat64FromValues = function(vec0, vec1) {
124 var vec = goog.vec.Vec2.createFloat64();
125 goog.vec.Vec2.setFromValues(vec, vec0, vec1);
126 return vec;
127 };
128
129
130 /**
131 * Creates a clone of the given 2 element vector.
132 *
133 * @param {goog.vec.Vec2.Float64} vec The source 2 element vector.
134 * @return {!goog.vec.Vec2.Float64} The new cloned vector.
135 */
136 goog.vec.Vec2.cloneFloat64 = goog.vec.Vec2.createFloat64FromArray;
137
138
139 /**
140 * Initializes the vector with the given values.
141 *
142 * @param {goog.vec.Vec2.AnyType} vec The vector to receive the values.
143 * @param {number} vec0 The value for element at index 0.
144 * @param {number} vec1 The value for element at index 1.
145 * @return {!goog.vec.Vec2.AnyType} Return vec so that operations can be
146 * chained together.
147 */
148 goog.vec.Vec2.setFromValues = function(vec, vec0, vec1) {
149 vec[0] = vec0;
150 vec[1] = vec1;
151 return vec;
152 };
153
154
155 /**
156 * Initializes the vector with the given array of values.
157 *
158 * @param {goog.vec.Vec2.AnyType} vec The vector to receive the
159 * values.
160 * @param {goog.vec.Vec2.AnyType} values The array of values.
161 * @return {!goog.vec.Vec2.AnyType} Return vec so that operations can be
162 * chained together.
163 */
164 goog.vec.Vec2.setFromArray = function(vec, values) {
165 vec[0] = values[0];
166 vec[1] = values[1];
167 return vec;
168 };
169
170
171 /**
172 * Performs a component-wise addition of vec0 and vec1 together storing the
173 * result into resultVec.
174 *
175 * @param {goog.vec.Vec2.AnyType} vec0 The first addend.
176 * @param {goog.vec.Vec2.AnyType} vec1 The second addend.
177 * @param {goog.vec.Vec2.AnyType} resultVec The vector to
178 * receive the result. May be vec0 or vec1.
179 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
180 * chained together.
181 */
182 goog.vec.Vec2.add = function(vec0, vec1, resultVec) {
183 resultVec[0] = vec0[0] + vec1[0];
184 resultVec[1] = vec0[1] + vec1[1];
185 return resultVec;
186 };
187
188
189 /**
190 * Performs a component-wise subtraction of vec1 from vec0 storing the
191 * result into resultVec.
192 *
193 * @param {goog.vec.Vec2.AnyType} vec0 The minuend.
194 * @param {goog.vec.Vec2.AnyType} vec1 The subtrahend.
195 * @param {goog.vec.Vec2.AnyType} resultVec The vector to
196 * receive the result. May be vec0 or vec1.
197 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
198 * chained together.
199 */
200 goog.vec.Vec2.subtract = function(vec0, vec1, resultVec) {
201 resultVec[0] = vec0[0] - vec1[0];
202 resultVec[1] = vec0[1] - vec1[1];
203 return resultVec;
204 };
205
206
207 /**
208 * Negates vec0, storing the result into resultVec.
209 *
210 * @param {goog.vec.Vec2.AnyType} vec0 The vector to negate.
211 * @param {goog.vec.Vec2.AnyType} resultVec The vector to
212 * receive the result. May be vec0.
213 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
214 * chained together.
215 */
216 goog.vec.Vec2.negate = function(vec0, resultVec) {
217 resultVec[0] = -vec0[0];
218 resultVec[1] = -vec0[1];
219 return resultVec;
220 };
221
222
223 /**
224 * Takes the absolute value of each component of vec0 storing the result in
225 * resultVec.
226 *
227 * @param {goog.vec.Vec2.AnyType} vec0 The source vector.
228 * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the result.
229 * May be vec0.
230 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
231 * chained together.
232 */
233 goog.vec.Vec2.abs = function(vec0, resultVec) {
234 resultVec[0] = Math.abs(vec0[0]);
235 resultVec[1] = Math.abs(vec0[1]);
236 return resultVec;
237 };
238
239
240 /**
241 * Multiplies each component of vec0 with scalar storing the product into
242 * resultVec.
243 *
244 * @param {goog.vec.Vec2.AnyType} vec0 The source vector.
245 * @param {number} scalar The value to multiply with each component of vec0.
246 * @param {goog.vec.Vec2.AnyType} resultVec The vector to
247 * receive the result. May be vec0.
248 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
249 * chained together.
250 */
251 goog.vec.Vec2.scale = function(vec0, scalar, resultVec) {
252 resultVec[0] = vec0[0] * scalar;
253 resultVec[1] = vec0[1] * scalar;
254 return resultVec;
255 };
256
257
258 /**
259 * Returns the magnitudeSquared of the given vector.
260 *
261 * @param {goog.vec.Vec2.AnyType} vec0 The vector.
262 * @return {number} The magnitude of the vector.
263 */
264 goog.vec.Vec2.magnitudeSquared = function(vec0) {
265 var x = vec0[0], y = vec0[1];
266 return x * x + y * y;
267 };
268
269
270 /**
271 * Returns the magnitude of the given vector.
272 *
273 * @param {goog.vec.Vec2.AnyType} vec0 The vector.
274 * @return {number} The magnitude of the vector.
275 */
276 goog.vec.Vec2.magnitude = function(vec0) {
277 var x = vec0[0], y = vec0[1];
278 return Math.sqrt(x * x + y * y);
279 };
280
281
282 /**
283 * Normalizes the given vector storing the result into resultVec.
284 *
285 * @param {goog.vec.Vec2.AnyType} vec0 The vector to normalize.
286 * @param {goog.vec.Vec2.AnyType} resultVec The vector to
287 * receive the result. May be vec0.
288 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
289 * chained together.
290 */
291 goog.vec.Vec2.normalize = function(vec0, resultVec) {
292 var ilen = 1 / goog.vec.Vec2.magnitude(vec0);
293 resultVec[0] = vec0[0] * ilen;
294 resultVec[1] = vec0[1] * ilen;
295 return resultVec;
296 };
297
298
299 /**
300 * Returns the scalar product of vectors vec0 and vec1.
301 *
302 * @param {goog.vec.Vec2.AnyType} vec0 The first vector.
303 * @param {goog.vec.Vec2.AnyType} vec1 The second vector.
304 * @return {number} The scalar product.
305 */
306 goog.vec.Vec2.dot = function(vec0, vec1) {
307 return vec0[0] * vec1[0] + vec0[1] * vec1[1];
308 };
309
310
311 /**
312 * Returns the squared distance between two points.
313 *
314 * @param {goog.vec.Vec2.AnyType} vec0 First point.
315 * @param {goog.vec.Vec2.AnyType} vec1 Second point.
316 * @return {number} The squared distance between the points.
317 */
318 goog.vec.Vec2.distanceSquared = function(vec0, vec1) {
319 var x = vec0[0] - vec1[0];
320 var y = vec0[1] - vec1[1];
321 return x * x + y * y;
322 };
323
324
325 /**
326 * Returns the distance between two points.
327 *
328 * @param {goog.vec.Vec2.AnyType} vec0 First point.
329 * @param {goog.vec.Vec2.AnyType} vec1 Second point.
330 * @return {number} The distance between the points.
331 */
332 goog.vec.Vec2.distance = function(vec0, vec1) {
333 return Math.sqrt(goog.vec.Vec2.distanceSquared(vec0, vec1));
334 };
335
336
337 /**
338 * Returns a unit vector pointing from one point to another.
339 * If the input points are equal then the result will be all zeros.
340 *
341 * @param {goog.vec.Vec2.AnyType} vec0 Origin point.
342 * @param {goog.vec.Vec2.AnyType} vec1 Target point.
343 * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
344 * results (may be vec0 or vec1).
345 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
346 * chained together.
347 */
348 goog.vec.Vec2.direction = function(vec0, vec1, resultVec) {
349 var x = vec1[0] - vec0[0];
350 var y = vec1[1] - vec0[1];
351 var d = Math.sqrt(x * x + y * y);
352 if (d) {
353 d = 1 / d;
354 resultVec[0] = x * d;
355 resultVec[1] = y * d;
356 } else {
357 resultVec[0] = resultVec[1] = 0;
358 }
359 return resultVec;
360 };
361
362
363 /**
364 * Linearly interpolate from vec0 to vec1 according to f. The value of f should
365 * be in the range [0..1] otherwise the results are undefined.
366 *
367 * @param {goog.vec.Vec2.AnyType} vec0 The first vector.
368 * @param {goog.vec.Vec2.AnyType} vec1 The second vector.
369 * @param {number} f The interpolation factor.
370 * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
371 * results (may be vec0 or vec1).
372 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
373 * chained together.
374 */
375 goog.vec.Vec2.lerp = function(vec0, vec1, f, resultVec) {
376 var x = vec0[0], y = vec0[1];
377 resultVec[0] = (vec1[0] - x) * f + x;
378 resultVec[1] = (vec1[1] - y) * f + y;
379 return resultVec;
380 };
381
382
383 /**
384 * Compares the components of vec0 with the components of another vector or
385 * scalar, storing the larger values in resultVec.
386 *
387 * @param {goog.vec.Vec2.AnyType} vec0 The source vector.
388 * @param {goog.vec.Vec2.AnyType|number} limit The limit vector or scalar.
389 * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
390 * results (may be vec0 or limit).
391 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
392 * chained together.
393 */
394 goog.vec.Vec2.max = function(vec0, limit, resultVec) {
395 if (goog.isNumber(limit)) {
396 resultVec[0] = Math.max(vec0[0], limit);
397 resultVec[1] = Math.max(vec0[1], limit);
398 } else {
399 resultVec[0] = Math.max(vec0[0], limit[0]);
400 resultVec[1] = Math.max(vec0[1], limit[1]);
401 }
402 return resultVec;
403 };
404
405
406 /**
407 * Compares the components of vec0 with the components of another vector or
408 * scalar, storing the smaller values in resultVec.
409 *
410 * @param {goog.vec.Vec2.AnyType} vec0 The source vector.
411 * @param {goog.vec.Vec2.AnyType|number} limit The limit vector or scalar.
412 * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
413 * results (may be vec0 or limit).
414 * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
415 * chained together.
416 */
417 goog.vec.Vec2.min = function(vec0, limit, resultVec) {
418 if (goog.isNumber(limit)) {
419 resultVec[0] = Math.min(vec0[0], limit);
420 resultVec[1] = Math.min(vec0[1], limit);
421 } else {
422 resultVec[0] = Math.min(vec0[0], limit[0]);
423 resultVec[1] = Math.min(vec0[1], limit[1]);
424 }
425 return resultVec;
426 };
427
428
429 /**
430 * Returns true if the components of vec0 are equal to the components of vec1.
431 *
432 * @param {goog.vec.Vec2.AnyType} vec0 The first vector.
433 * @param {goog.vec.Vec2.AnyType} vec1 The second vector.
434 * @return {boolean} True if the vectors are equal, false otherwise.
435 */
436 goog.vec.Vec2.equals = function(vec0, vec1) {
437 return vec0.length == vec1.length && vec0[0] == vec1[0] && vec0[1] == vec1[1];
438 };