| 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 vector after scaling. |
| 120 |
* @override |
| 121 |
*/ |
| 122 |
// Since the implementation of Coordinate.scale() returns "this", we |
| 123 |
// can reuse that implementation here, and just recast the return type. |
| 124 |
goog.math.Vec2.prototype.scale = |
| 125 |
/** @type {function(number, number=):!goog.math.Vec2} */ |
| 126 |
(goog.math.Coordinate.prototype.scale); |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Reverses the sign of the vector. Equivalent to scaling the vector by -1. |
| 131 |
* @return {!goog.math.Vec2} The inverted vector. |
| 132 |
*/ |
| 133 |
goog.math.Vec2.prototype.invert = function() { |
| 134 |
this.x = -this.x; |
| 135 |
this.y = -this.y; |
| 136 |
return this; |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* Normalizes the current vector to have a magnitude of 1. |
| 142 |
* @return {!goog.math.Vec2} The normalized vector. |
| 143 |
*/ |
| 144 |
goog.math.Vec2.prototype.normalize = function() { |
| 145 |
return this.scale(1 / this.magnitude()); |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
/** |
| 150 |
* Adds another vector to this vector in-place. |
| 151 |
* @param {!goog.math.Coordinate} b The vector to add. |
| 152 |
* @return {!goog.math.Vec2} This vector with {@code b} added. |
| 153 |
*/ |
| 154 |
goog.math.Vec2.prototype.add = function(b) { |
| 155 |
this.x += b.x; |
| 156 |
this.y += b.y; |
| 157 |
return this; |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
* Subtracts another vector from this vector in-place. |
| 163 |
* @param {!goog.math.Coordinate} b The vector to subtract. |
| 164 |
* @return {!goog.math.Vec2} This vector with {@code b} subtracted. |
| 165 |
*/ |
| 166 |
goog.math.Vec2.prototype.subtract = function(b) { |
| 167 |
this.x -= b.x; |
| 168 |
this.y -= b.y; |
| 169 |
return this; |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
/** |
| 174 |
* Rotates this vector in-place by a given angle, specified in radians. |
| 175 |
* @param {number} angle The angle, in radians. |
| 176 |
* @return {!goog.math.Vec2} This vector rotated {@code angle} radians. |
| 177 |
*/ |
| 178 |
goog.math.Vec2.prototype.rotate = function(angle) { |
| 179 |
var cos = Math.cos(angle); |
| 180 |
var sin = Math.sin(angle); |
| 181 |
var newX = this.x * cos - this.y * sin; |
| 182 |
var newY = this.y * cos + this.x * sin; |
| 183 |
this.x = newX; |
| 184 |
this.y = newY; |
| 185 |
return this; |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* Rotates a vector by a given angle, specified in radians, relative to a given |
| 191 |
* axis rotation point. The returned vector is a newly created instance - no |
| 192 |
* in-place changes are done. |
| 193 |
* @param {!goog.math.Vec2} v A vector. |
| 194 |
* @param {!goog.math.Vec2} axisPoint The rotation axis point. |
| 195 |
* @param {number} angle The angle, in radians. |
| 196 |
* @return {!goog.math.Vec2} The rotated vector in a newly created instance. |
| 197 |
*/ |
| 198 |
goog.math.Vec2.rotateAroundPoint = function(v, axisPoint, angle) { |
| 199 |
var res = v.clone(); |
| 200 |
return res.subtract(axisPoint).rotate(angle).add(axisPoint); |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
/** @override */ |
| 205 |
goog.math.Vec2.prototype.equals = function(b) { |
| 206 |
if (this == b) { |
| 207 |
return true; |
| 208 |
} |
| 209 |
return b instanceof goog.math.Vec2 && !!b && this.x == b.x && this.y == b.y; |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
/** |
| 214 |
* Returns the distance between two vectors. |
| 215 |
* @param {!goog.math.Coordinate} a The first vector. |
| 216 |
* @param {!goog.math.Coordinate} b The second vector. |
| 217 |
* @return {number} The distance. |
| 218 |
*/ |
| 219 |
goog.math.Vec2.distance = goog.math.Coordinate.distance; |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Returns the squared distance between two vectors. |
| 224 |
* @param {!goog.math.Coordinate} a The first vector. |
| 225 |
* @param {!goog.math.Coordinate} b The second vector. |
| 226 |
* @return {number} The squared distance. |
| 227 |
*/ |
| 228 |
goog.math.Vec2.squaredDistance = goog.math.Coordinate.squaredDistance; |
| 229 |
|
| 230 |
|
| 231 |
/** |
| 232 |
* Compares vectors for equality. |
| 233 |
* @param {!goog.math.Coordinate} a The first vector. |
| 234 |
* @param {!goog.math.Coordinate} b The second vector. |
| 235 |
* @return {boolean} Whether the vectors have the same x and y coordinates. |
| 236 |
*/ |
| 237 |
goog.math.Vec2.equals = goog.math.Coordinate.equals; |
| 238 |
|
| 239 |
|
| 240 |
/** |
| 241 |
* Returns the sum of two vectors as a new Vec2. |
| 242 |
* @param {!goog.math.Coordinate} a The first vector. |
| 243 |
* @param {!goog.math.Coordinate} b The second vector. |
| 244 |
* @return {!goog.math.Vec2} The sum vector. |
| 245 |
*/ |
| 246 |
goog.math.Vec2.sum = function(a, b) { |
| 247 |
return new goog.math.Vec2(a.x + b.x, a.y + b.y); |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Returns the difference between two vectors as a new Vec2. |
| 253 |
* @param {!goog.math.Coordinate} a The first vector. |
| 254 |
* @param {!goog.math.Coordinate} b The second vector. |
| 255 |
* @return {!goog.math.Vec2} The difference vector. |
| 256 |
*/ |
| 257 |
goog.math.Vec2.difference = function(a, b) { |
| 258 |
return new goog.math.Vec2(a.x - b.x, a.y - b.y); |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Returns the dot-product of two vectors. |
| 264 |
* @param {!goog.math.Coordinate} a The first vector. |
| 265 |
* @param {!goog.math.Coordinate} b The second vector. |
| 266 |
* @return {number} The dot-product of the two vectors. |
| 267 |
*/ |
| 268 |
goog.math.Vec2.dot = function(a, b) { |
| 269 |
return a.x * b.x + a.y * b.y; |
| 270 |
}; |
| 271 |
|
| 272 |
|
| 273 |
/** |
| 274 |
* Returns the determinant of two vectors. |
| 275 |
* @param {!goog.math.Vec2} a The first vector. |
| 276 |
* @param {!goog.math.Vec2} b The second vector. |
| 277 |
* @return {number} The determinant of the two vectors. |
| 278 |
*/ |
| 279 |
goog.math.Vec2.determinant = function(a, b) { |
| 280 |
return a.x * b.y - a.y * b.x; |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* Returns a new Vec2 that is the linear interpolant between vectors a and b at |
| 286 |
* scale-value x. |
| 287 |
* @param {!goog.math.Coordinate} a Vector a. |
| 288 |
* @param {!goog.math.Coordinate} b Vector b. |
| 289 |
* @param {number} x The proportion between a and b. |
| 290 |
* @return {!goog.math.Vec2} The interpolated vector. |
| 291 |
*/ |
| 292 |
goog.math.Vec2.lerp = function(a, b, x) { |
| 293 |
return new goog.math.Vec2( |
| 294 |
goog.math.lerp(a.x, b.x, x), goog.math.lerp(a.y, b.y, x)); |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
/** |
| 299 |
* Returns a new Vec2 that is a copy of the vector a, but rescaled by a factors |
| 300 |
* sx and sy in the x and y directions. If only sx is specified, then y is |
| 301 |
* scaled by the same factor as x. |
| 302 |
* @param {!goog.math.Coordinate} a Vector a. |
| 303 |
* @param {number} sx X scale factor. |
| 304 |
* @param {number=} sy Y scale factor (optional). |
| 305 |
* @return {!goog.math.Vec2} A new rescaled vector. |
| 306 |
*/ |
| 307 |
goog.math.Vec2.rescaled = function(a, sx, sy = sx) { |
| 308 |
return new goog.math.Vec2(a.x * sx, a.y * sy); |
| 309 |
}; |