Path: blob/trunk/third_party/closure/goog/math/vec2.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Defines a 2-element vector class that can be used for16* coordinate math, useful for animation systems and point manipulation.17*18* Vec2 objects inherit from goog.math.Coordinate and may be used wherever a19* Coordinate is required. Where appropriate, Vec2 functions accept both Vec220* and Coordinate objects as input.21*22* @author [email protected] (Shawn Brenneman)23*/2425goog.provide('goog.math.Vec2');2627goog.require('goog.math');28goog.require('goog.math.Coordinate');29303132/**33* Class for a two-dimensional vector object and assorted functions useful for34* manipulating points.35*36* @param {number} x The x coordinate for the vector.37* @param {number} y The y coordinate for the vector.38* @struct39* @constructor40* @extends {goog.math.Coordinate}41*/42goog.math.Vec2 = function(x, y) {43/**44* X-value45* @type {number}46*/47this.x = x;4849/**50* Y-value51* @type {number}52*/53this.y = y;54};55goog.inherits(goog.math.Vec2, goog.math.Coordinate);565758/**59* @return {!goog.math.Vec2} A random unit-length vector.60*/61goog.math.Vec2.randomUnit = function() {62var angle = Math.random() * Math.PI * 2;63return new goog.math.Vec2(Math.cos(angle), Math.sin(angle));64};656667/**68* @return {!goog.math.Vec2} A random vector inside the unit-disc.69*/70goog.math.Vec2.random = function() {71var mag = Math.sqrt(Math.random());72var angle = Math.random() * Math.PI * 2;7374return new goog.math.Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);75};767778/**79* Returns a new Vec2 object from a given coordinate.80* @param {!goog.math.Coordinate} a The coordinate.81* @return {!goog.math.Vec2} A new vector object.82*/83goog.math.Vec2.fromCoordinate = function(a) {84return new goog.math.Vec2(a.x, a.y);85};868788/**89* @return {!goog.math.Vec2} A new vector with the same coordinates as this one.90* @override91*/92goog.math.Vec2.prototype.clone = function() {93return new goog.math.Vec2(this.x, this.y);94};959697/**98* Returns the magnitude of the vector measured from the origin.99* @return {number} The length of the vector.100*/101goog.math.Vec2.prototype.magnitude = function() {102return Math.sqrt(this.x * this.x + this.y * this.y);103};104105106/**107* Returns the squared magnitude of the vector measured from the origin.108* NOTE(brenneman): Leaving out the square root is not a significant109* optimization in JavaScript.110* @return {number} The length of the vector, squared.111*/112goog.math.Vec2.prototype.squaredMagnitude = function() {113return this.x * this.x + this.y * this.y;114};115116117/**118* @return {!goog.math.Vec2} This coordinate after scaling.119* @override120*/121goog.math.Vec2.prototype.scale =122/** @type {function(number, number=):!goog.math.Vec2} */123(goog.math.Coordinate.prototype.scale);124125126/**127* Reverses the sign of the vector. Equivalent to scaling the vector by -1.128* @return {!goog.math.Vec2} The inverted vector.129*/130goog.math.Vec2.prototype.invert = function() {131this.x = -this.x;132this.y = -this.y;133return this;134};135136137/**138* Normalizes the current vector to have a magnitude of 1.139* @return {!goog.math.Vec2} The normalized vector.140*/141goog.math.Vec2.prototype.normalize = function() {142return this.scale(1 / this.magnitude());143};144145146/**147* Adds another vector to this vector in-place.148* @param {!goog.math.Coordinate} b The vector to add.149* @return {!goog.math.Vec2} This vector with {@code b} added.150*/151goog.math.Vec2.prototype.add = function(b) {152this.x += b.x;153this.y += b.y;154return this;155};156157158/**159* Subtracts another vector from this vector in-place.160* @param {!goog.math.Coordinate} b The vector to subtract.161* @return {!goog.math.Vec2} This vector with {@code b} subtracted.162*/163goog.math.Vec2.prototype.subtract = function(b) {164this.x -= b.x;165this.y -= b.y;166return this;167};168169170/**171* Rotates this vector in-place by a given angle, specified in radians.172* @param {number} angle The angle, in radians.173* @return {!goog.math.Vec2} This vector rotated {@code angle} radians.174*/175goog.math.Vec2.prototype.rotate = function(angle) {176var cos = Math.cos(angle);177var sin = Math.sin(angle);178var newX = this.x * cos - this.y * sin;179var newY = this.y * cos + this.x * sin;180this.x = newX;181this.y = newY;182return this;183};184185186/**187* Rotates a vector by a given angle, specified in radians, relative to a given188* axis rotation point. The returned vector is a newly created instance - no189* in-place changes are done.190* @param {!goog.math.Vec2} v A vector.191* @param {!goog.math.Vec2} axisPoint The rotation axis point.192* @param {number} angle The angle, in radians.193* @return {!goog.math.Vec2} The rotated vector in a newly created instance.194*/195goog.math.Vec2.rotateAroundPoint = function(v, axisPoint, angle) {196var res = v.clone();197return res.subtract(axisPoint).rotate(angle).add(axisPoint);198};199200201/** @override */202goog.math.Vec2.prototype.equals = function(b) {203if (this == b) {204return true;205}206return b instanceof goog.math.Vec2 && !!b && this.x == b.x && this.y == b.y;207};208209210/**211* Returns the distance between two vectors.212* @param {!goog.math.Coordinate} a The first vector.213* @param {!goog.math.Coordinate} b The second vector.214* @return {number} The distance.215*/216goog.math.Vec2.distance = goog.math.Coordinate.distance;217218219/**220* Returns the squared distance between two vectors.221* @param {!goog.math.Coordinate} a The first vector.222* @param {!goog.math.Coordinate} b The second vector.223* @return {number} The squared distance.224*/225goog.math.Vec2.squaredDistance = goog.math.Coordinate.squaredDistance;226227228/**229* Compares vectors for equality.230* @param {!goog.math.Coordinate} a The first vector.231* @param {!goog.math.Coordinate} b The second vector.232* @return {boolean} Whether the vectors have the same x and y coordinates.233*/234goog.math.Vec2.equals = goog.math.Coordinate.equals;235236237/**238* Returns the sum of two vectors as a new Vec2.239* @param {!goog.math.Coordinate} a The first vector.240* @param {!goog.math.Coordinate} b The second vector.241* @return {!goog.math.Vec2} The sum vector.242*/243goog.math.Vec2.sum = function(a, b) {244return new goog.math.Vec2(a.x + b.x, a.y + b.y);245};246247248/**249* Returns the difference between 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 difference vector.253*/254goog.math.Vec2.difference = function(a, b) {255return new goog.math.Vec2(a.x - b.x, a.y - b.y);256};257258259/**260* Returns the dot-product of two vectors.261* @param {!goog.math.Coordinate} a The first vector.262* @param {!goog.math.Coordinate} b The second vector.263* @return {number} The dot-product of the two vectors.264*/265goog.math.Vec2.dot = function(a, b) {266return a.x * b.x + a.y * b.y;267};268269270/**271* Returns the determinant of two vectors.272* @param {!goog.math.Vec2} a The first vector.273* @param {!goog.math.Vec2} b The second vector.274* @return {number} The determinant of the two vectors.275*/276goog.math.Vec2.determinant = function(a, b) {277return a.x * b.y - a.y * b.x;278};279280281/**282* Returns a new Vec2 that is the linear interpolant between vectors a and b at283* scale-value x.284* @param {!goog.math.Coordinate} a Vector a.285* @param {!goog.math.Coordinate} b Vector b.286* @param {number} x The proportion between a and b.287* @return {!goog.math.Vec2} The interpolated vector.288*/289goog.math.Vec2.lerp = function(a, b, x) {290return new goog.math.Vec2(291goog.math.lerp(a.x, b.x, x), goog.math.lerp(a.y, b.y, x));292};293294295