Path: blob/trunk/third_party/closure/goog/math/coordinate3.js
2868 views
// Copyright 2008 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 A utility class for representing three-dimensional points.16*17* Based heavily on coordinate.js by:18*/1920goog.provide('goog.math.Coordinate3');21222324/**25* Class for representing coordinates and positions in 3 dimensions.26*27* @param {number=} opt_x X coordinate, defaults to 0.28* @param {number=} opt_y Y coordinate, defaults to 0.29* @param {number=} opt_z Z coordinate, defaults to 0.30* @struct31* @constructor32*/33goog.math.Coordinate3 = function(opt_x, opt_y, opt_z) {34/**35* X-value36* @type {number}37*/38this.x = goog.isDef(opt_x) ? opt_x : 0;3940/**41* Y-value42* @type {number}43*/44this.y = goog.isDef(opt_y) ? opt_y : 0;4546/**47* Z-value48* @type {number}49*/50this.z = goog.isDef(opt_z) ? opt_z : 0;51};525354/**55* Returns a new copy of the coordinate.56*57* @return {!goog.math.Coordinate3} A clone of this coordinate.58*/59goog.math.Coordinate3.prototype.clone = function() {60return new goog.math.Coordinate3(this.x, this.y, this.z);61};626364if (goog.DEBUG) {65/**66* Returns a nice string representing the coordinate.67*68* @return {string} In the form (50, 73, 31).69* @override70*/71goog.math.Coordinate3.prototype.toString = function() {72return '(' + this.x + ', ' + this.y + ', ' + this.z + ')';73};74}757677/**78* Compares coordinates for equality.79*80* @param {goog.math.Coordinate3} a A Coordinate3.81* @param {goog.math.Coordinate3} b A Coordinate3.82* @return {boolean} True iff the coordinates are equal, or if both are null.83*/84goog.math.Coordinate3.equals = function(a, b) {85if (a == b) {86return true;87}88if (!a || !b) {89return false;90}91return a.x == b.x && a.y == b.y && a.z == b.z;92};939495/**96* Returns the distance between two coordinates.97*98* @param {goog.math.Coordinate3} a A Coordinate3.99* @param {goog.math.Coordinate3} b A Coordinate3.100* @return {number} The distance between {@code a} and {@code b}.101*/102goog.math.Coordinate3.distance = function(a, b) {103var dx = a.x - b.x;104var dy = a.y - b.y;105var dz = a.z - b.z;106return Math.sqrt(dx * dx + dy * dy + dz * dz);107};108109110/**111* Returns the squared distance between two coordinates. Squared distances can112* be used for comparisons when the actual value is not required.113*114* Performance note: eliminating the square root is an optimization often used115* in lower-level languages, but the speed difference is not nearly as116* pronounced in JavaScript (only a few percent.)117*118* @param {goog.math.Coordinate3} a A Coordinate3.119* @param {goog.math.Coordinate3} b A Coordinate3.120* @return {number} The squared distance between {@code a} and {@code b}.121*/122goog.math.Coordinate3.squaredDistance = function(a, b) {123var dx = a.x - b.x;124var dy = a.y - b.y;125var dz = a.z - b.z;126return dx * dx + dy * dy + dz * dz;127};128129130/**131* Returns the difference between two coordinates as a new132* goog.math.Coordinate3.133*134* @param {goog.math.Coordinate3} a A Coordinate3.135* @param {goog.math.Coordinate3} b A Coordinate3.136* @return {!goog.math.Coordinate3} A Coordinate3 representing the difference137* between {@code a} and {@code b}.138*/139goog.math.Coordinate3.difference = function(a, b) {140return new goog.math.Coordinate3(a.x - b.x, a.y - b.y, a.z - b.z);141};142143144/**145* Returns the contents of this coordinate as a 3 value Array.146*147* @return {!Array<number>} A new array.148*/149goog.math.Coordinate3.prototype.toArray = function() {150return [this.x, this.y, this.z];151};152153154/**155* Converts a three element array into a Coordinate3 object. If the value156* passed in is not an array, not array-like, or not of the right length, an157* error is thrown.158*159* @param {Array<number>} a Array of numbers to become a coordinate.160* @return {!goog.math.Coordinate3} A new coordinate from the array values.161* @throws {Error} When the oject passed in is not valid.162*/163goog.math.Coordinate3.fromArray = function(a) {164if (a.length <= 3) {165return new goog.math.Coordinate3(a[0], a[1], a[2]);166}167168throw Error('Conversion from an array requires an array of length 3');169};170171172