Path: blob/trunk/third_party/closure/goog/math/coordinate.js
2868 views
// Copyright 2006 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 two-dimensional positions.16*/171819goog.provide('goog.math.Coordinate');2021goog.require('goog.math');22232425/**26* Class for representing coordinates and positions.27* @param {number=} opt_x Left, defaults to 0.28* @param {number=} opt_y Top, defaults to 0.29* @struct30* @constructor31*/32goog.math.Coordinate = function(opt_x, opt_y) {33/**34* X-value35* @type {number}36*/37this.x = goog.isDef(opt_x) ? opt_x : 0;3839/**40* Y-value41* @type {number}42*/43this.y = goog.isDef(opt_y) ? opt_y : 0;44};454647/**48* Returns a new copy of the coordinate.49* @return {!goog.math.Coordinate} A clone of this coordinate.50*/51goog.math.Coordinate.prototype.clone = function() {52return new goog.math.Coordinate(this.x, this.y);53};545556if (goog.DEBUG) {57/**58* Returns a nice string representing the coordinate.59* @return {string} In the form (50, 73).60* @override61*/62goog.math.Coordinate.prototype.toString = function() {63return '(' + this.x + ', ' + this.y + ')';64};65}666768/**69* Returns whether the specified value is equal to this coordinate.70* @param {*} other Some other value.71* @return {boolean} Whether the specified value is equal to this coordinate.72*/73goog.math.Coordinate.prototype.equals = function(other) {74return other instanceof goog.math.Coordinate &&75goog.math.Coordinate.equals(this, other);76};777879/**80* Compares coordinates for equality.81* @param {goog.math.Coordinate} a A Coordinate.82* @param {goog.math.Coordinate} b A Coordinate.83* @return {boolean} True iff the coordinates are equal, or if both are null.84*/85goog.math.Coordinate.equals = function(a, b) {86if (a == b) {87return true;88}89if (!a || !b) {90return false;91}92return a.x == b.x && a.y == b.y;93};949596/**97* Returns the distance between two coordinates.98* @param {!goog.math.Coordinate} a A Coordinate.99* @param {!goog.math.Coordinate} b A Coordinate.100* @return {number} The distance between {@code a} and {@code b}.101*/102goog.math.Coordinate.distance = function(a, b) {103var dx = a.x - b.x;104var dy = a.y - b.y;105return Math.sqrt(dx * dx + dy * dy);106};107108109/**110* Returns the magnitude of a coordinate.111* @param {!goog.math.Coordinate} a A Coordinate.112* @return {number} The distance between the origin and {@code a}.113*/114goog.math.Coordinate.magnitude = function(a) {115return Math.sqrt(a.x * a.x + a.y * a.y);116};117118119/**120* Returns the angle from the origin to a coordinate.121* @param {!goog.math.Coordinate} a A Coordinate.122* @return {number} The angle, in degrees, clockwise from the positive X123* axis to {@code a}.124*/125goog.math.Coordinate.azimuth = function(a) {126return goog.math.angle(0, 0, a.x, a.y);127};128129130/**131* Returns the squared distance between two coordinates. Squared distances can132* be used for comparisons when the actual value is not required.133*134* Performance note: eliminating the square root is an optimization often used135* in lower-level languages, but the speed difference is not nearly as136* pronounced in JavaScript (only a few percent.)137*138* @param {!goog.math.Coordinate} a A Coordinate.139* @param {!goog.math.Coordinate} b A Coordinate.140* @return {number} The squared distance between {@code a} and {@code b}.141*/142goog.math.Coordinate.squaredDistance = function(a, b) {143var dx = a.x - b.x;144var dy = a.y - b.y;145return dx * dx + dy * dy;146};147148149/**150* Returns the difference between two coordinates as a new151* goog.math.Coordinate.152* @param {!goog.math.Coordinate} a A Coordinate.153* @param {!goog.math.Coordinate} b A Coordinate.154* @return {!goog.math.Coordinate} A Coordinate representing the difference155* between {@code a} and {@code b}.156*/157goog.math.Coordinate.difference = function(a, b) {158return new goog.math.Coordinate(a.x - b.x, a.y - b.y);159};160161162/**163* Returns the sum of two coordinates as a new goog.math.Coordinate.164* @param {!goog.math.Coordinate} a A Coordinate.165* @param {!goog.math.Coordinate} b A Coordinate.166* @return {!goog.math.Coordinate} A Coordinate representing the sum of the two167* coordinates.168*/169goog.math.Coordinate.sum = function(a, b) {170return new goog.math.Coordinate(a.x + b.x, a.y + b.y);171};172173174/**175* Rounds the x and y fields to the next larger integer values.176* @return {!goog.math.Coordinate} This coordinate with ceil'd fields.177*/178goog.math.Coordinate.prototype.ceil = function() {179this.x = Math.ceil(this.x);180this.y = Math.ceil(this.y);181return this;182};183184185/**186* Rounds the x and y fields to the next smaller integer values.187* @return {!goog.math.Coordinate} This coordinate with floored fields.188*/189goog.math.Coordinate.prototype.floor = function() {190this.x = Math.floor(this.x);191this.y = Math.floor(this.y);192return this;193};194195196/**197* Rounds the x and y fields to the nearest integer values.198* @return {!goog.math.Coordinate} This coordinate with rounded fields.199*/200goog.math.Coordinate.prototype.round = function() {201this.x = Math.round(this.x);202this.y = Math.round(this.y);203return this;204};205206207/**208* Translates this box by the given offsets. If a {@code goog.math.Coordinate}209* is given, then the x and y values are translated by the coordinate's x and y.210* Otherwise, x and y are translated by {@code tx} and {@code opt_ty}211* respectively.212* @param {number|goog.math.Coordinate} tx The value to translate x by or the213* the coordinate to translate this coordinate by.214* @param {number=} opt_ty The value to translate y by.215* @return {!goog.math.Coordinate} This coordinate after translating.216*/217goog.math.Coordinate.prototype.translate = function(tx, opt_ty) {218if (tx instanceof goog.math.Coordinate) {219this.x += tx.x;220this.y += tx.y;221} else {222this.x += Number(tx);223if (goog.isNumber(opt_ty)) {224this.y += opt_ty;225}226}227return this;228};229230231/**232* Scales this coordinate by the given scale factors. The x and y values are233* scaled by {@code sx} and {@code opt_sy} respectively. If {@code opt_sy}234* is not given, then {@code sx} is used for both x and y.235* @param {number} sx The scale factor to use for the x dimension.236* @param {number=} opt_sy The scale factor to use for the y dimension.237* @return {!goog.math.Coordinate} This coordinate after scaling.238*/239goog.math.Coordinate.prototype.scale = function(sx, opt_sy) {240var sy = goog.isNumber(opt_sy) ? opt_sy : sx;241this.x *= sx;242this.y *= sy;243return this;244};245246247/**248* Rotates this coordinate clockwise about the origin (or, optionally, the given249* center) by the given angle, in radians.250* @param {number} radians The angle by which to rotate this coordinate251* clockwise about the given center, in radians.252* @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults253* to (0, 0) if not given.254*/255goog.math.Coordinate.prototype.rotateRadians = function(radians, opt_center) {256var center = opt_center || new goog.math.Coordinate(0, 0);257258var x = this.x;259var y = this.y;260var cos = Math.cos(radians);261var sin = Math.sin(radians);262263this.x = (x - center.x) * cos - (y - center.y) * sin + center.x;264this.y = (x - center.x) * sin + (y - center.y) * cos + center.y;265};266267268/**269* Rotates this coordinate clockwise about the origin (or, optionally, the given270* center) by the given angle, in degrees.271* @param {number} degrees The angle by which to rotate this coordinate272* clockwise about the given center, in degrees.273* @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults274* to (0, 0) if not given.275*/276goog.math.Coordinate.prototype.rotateDegrees = function(degrees, opt_center) {277this.rotateRadians(goog.math.toRadians(degrees), opt_center);278};279280281