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