290 lines
7.8 KiB
JavaScript
290 lines
7.8 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright The Closure Library Authors.
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview A utility class for representing two-dimensional positions.
|
|
*/
|
|
|
|
|
|
goog.provide('goog.math.Coordinate');
|
|
|
|
goog.require('goog.math');
|
|
|
|
|
|
|
|
/**
|
|
* Class for representing coordinates and positions.
|
|
* @param {number=} opt_x Left, defaults to 0.
|
|
* @param {number=} opt_y Top, defaults to 0.
|
|
* @struct
|
|
* @constructor
|
|
*/
|
|
goog.math.Coordinate = function(opt_x, opt_y) {
|
|
'use strict';
|
|
/**
|
|
* X-value
|
|
* @type {number}
|
|
*/
|
|
this.x = (opt_x !== undefined) ? opt_x : 0;
|
|
|
|
/**
|
|
* Y-value
|
|
* @type {number}
|
|
*/
|
|
this.y = (opt_y !== undefined) ? opt_y : 0;
|
|
};
|
|
|
|
|
|
/**
|
|
* Returns a new copy of the coordinate.
|
|
* @return {!goog.math.Coordinate} A clone of this coordinate.
|
|
*/
|
|
goog.math.Coordinate.prototype.clone = function() {
|
|
'use strict';
|
|
return new goog.math.Coordinate(this.x, this.y);
|
|
};
|
|
|
|
|
|
if (goog.DEBUG) {
|
|
/**
|
|
* Returns a nice string representing the coordinate.
|
|
* @return {string} In the form (50, 73).
|
|
* @override
|
|
*/
|
|
goog.math.Coordinate.prototype.toString = function() {
|
|
'use strict';
|
|
return '(' + this.x + ', ' + this.y + ')';
|
|
};
|
|
}
|
|
|
|
|
|
/**
|
|
* Returns whether the specified value is equal to this coordinate.
|
|
* @param {*} other Some other value.
|
|
* @return {boolean} Whether the specified value is equal to this coordinate.
|
|
*/
|
|
goog.math.Coordinate.prototype.equals = function(other) {
|
|
'use strict';
|
|
return other instanceof goog.math.Coordinate &&
|
|
goog.math.Coordinate.equals(this, other);
|
|
};
|
|
|
|
|
|
/**
|
|
* Compares coordinates for equality.
|
|
* @param {goog.math.Coordinate} a A Coordinate.
|
|
* @param {goog.math.Coordinate} b A Coordinate.
|
|
* @return {boolean} True iff the coordinates are equal, or if both are null.
|
|
*/
|
|
goog.math.Coordinate.equals = function(a, b) {
|
|
'use strict';
|
|
if (a == b) {
|
|
return true;
|
|
}
|
|
if (!a || !b) {
|
|
return false;
|
|
}
|
|
return a.x == b.x && a.y == b.y;
|
|
};
|
|
|
|
|
|
/**
|
|
* Returns the distance between two coordinates.
|
|
* @param {!goog.math.Coordinate} a A Coordinate.
|
|
* @param {!goog.math.Coordinate} b A Coordinate.
|
|
* @return {number} The distance between `a` and `b`.
|
|
*/
|
|
goog.math.Coordinate.distance = function(a, b) {
|
|
'use strict';
|
|
var dx = a.x - b.x;
|
|
var dy = a.y - b.y;
|
|
return Math.sqrt(dx * dx + dy * dy);
|
|
};
|
|
|
|
|
|
/**
|
|
* Returns the magnitude of a coordinate.
|
|
* @param {!goog.math.Coordinate} a A Coordinate.
|
|
* @return {number} The distance between the origin and `a`.
|
|
*/
|
|
goog.math.Coordinate.magnitude = function(a) {
|
|
'use strict';
|
|
return Math.sqrt(a.x * a.x + a.y * a.y);
|
|
};
|
|
|
|
|
|
/**
|
|
* Returns the angle from the origin to a coordinate.
|
|
* @param {!goog.math.Coordinate} a A Coordinate.
|
|
* @return {number} The angle, in degrees, clockwise from the positive X
|
|
* axis to `a`.
|
|
*/
|
|
goog.math.Coordinate.azimuth = function(a) {
|
|
'use strict';
|
|
return goog.math.angle(0, 0, a.x, a.y);
|
|
};
|
|
|
|
|
|
/**
|
|
* Returns the squared distance between two coordinates. Squared distances can
|
|
* be used for comparisons when the actual value is not required.
|
|
*
|
|
* Performance note: eliminating the square root is an optimization often used
|
|
* in lower-level languages, but the speed difference is not nearly as
|
|
* pronounced in JavaScript (only a few percent.)
|
|
*
|
|
* @param {!goog.math.Coordinate} a A Coordinate.
|
|
* @param {!goog.math.Coordinate} b A Coordinate.
|
|
* @return {number} The squared distance between `a` and `b`.
|
|
*/
|
|
goog.math.Coordinate.squaredDistance = function(a, b) {
|
|
'use strict';
|
|
var dx = a.x - b.x;
|
|
var dy = a.y - b.y;
|
|
return dx * dx + dy * dy;
|
|
};
|
|
|
|
|
|
/**
|
|
* Returns the difference between two coordinates as a new
|
|
* goog.math.Coordinate.
|
|
* @param {!goog.math.Coordinate} a A Coordinate.
|
|
* @param {!goog.math.Coordinate} b A Coordinate.
|
|
* @return {!goog.math.Coordinate} A Coordinate representing the difference
|
|
* between `a` and `b`.
|
|
*/
|
|
goog.math.Coordinate.difference = function(a, b) {
|
|
'use strict';
|
|
return new goog.math.Coordinate(a.x - b.x, a.y - b.y);
|
|
};
|
|
|
|
|
|
/**
|
|
* Returns the sum of two coordinates as a new goog.math.Coordinate.
|
|
* @param {!goog.math.Coordinate} a A Coordinate.
|
|
* @param {!goog.math.Coordinate} b A Coordinate.
|
|
* @return {!goog.math.Coordinate} A Coordinate representing the sum of the two
|
|
* coordinates.
|
|
*/
|
|
goog.math.Coordinate.sum = function(a, b) {
|
|
'use strict';
|
|
return new goog.math.Coordinate(a.x + b.x, a.y + b.y);
|
|
};
|
|
|
|
|
|
/**
|
|
* Rounds the x and y fields to the next larger integer values.
|
|
* @return {!goog.math.Coordinate} This coordinate with ceil'd fields.
|
|
*/
|
|
goog.math.Coordinate.prototype.ceil = function() {
|
|
'use strict';
|
|
this.x = Math.ceil(this.x);
|
|
this.y = Math.ceil(this.y);
|
|
return this;
|
|
};
|
|
|
|
|
|
/**
|
|
* Rounds the x and y fields to the next smaller integer values.
|
|
* @return {!goog.math.Coordinate} This coordinate with floored fields.
|
|
*/
|
|
goog.math.Coordinate.prototype.floor = function() {
|
|
'use strict';
|
|
this.x = Math.floor(this.x);
|
|
this.y = Math.floor(this.y);
|
|
return this;
|
|
};
|
|
|
|
|
|
/**
|
|
* Rounds the x and y fields to the nearest integer values.
|
|
* @return {!goog.math.Coordinate} This coordinate with rounded fields.
|
|
*/
|
|
goog.math.Coordinate.prototype.round = function() {
|
|
'use strict';
|
|
this.x = Math.round(this.x);
|
|
this.y = Math.round(this.y);
|
|
return this;
|
|
};
|
|
|
|
|
|
/**
|
|
* Translates this box by the given offsets. If a `goog.math.Coordinate`
|
|
* is given, then the x and y values are translated by the coordinate's x and y.
|
|
* Otherwise, x and y are translated by `tx` and `opt_ty`
|
|
* respectively.
|
|
* @param {number|goog.math.Coordinate} tx The value to translate x by or the
|
|
* the coordinate to translate this coordinate by.
|
|
* @param {number=} opt_ty The value to translate y by.
|
|
* @return {!goog.math.Coordinate} This coordinate after translating.
|
|
*/
|
|
goog.math.Coordinate.prototype.translate = function(tx, opt_ty) {
|
|
'use strict';
|
|
if (tx instanceof goog.math.Coordinate) {
|
|
this.x += tx.x;
|
|
this.y += tx.y;
|
|
} else {
|
|
this.x += Number(tx);
|
|
if (typeof opt_ty === 'number') {
|
|
this.y += opt_ty;
|
|
}
|
|
}
|
|
return this;
|
|
};
|
|
|
|
|
|
/**
|
|
* Scales this coordinate by the given scale factors. The x and y values are
|
|
* scaled by `sx` and `opt_sy` respectively. If `opt_sy`
|
|
* is not given, then `sx` is used for both x and y.
|
|
* @param {number} sx The scale factor to use for the x dimension.
|
|
* @param {number=} opt_sy The scale factor to use for the y dimension.
|
|
* @return {!goog.math.Coordinate} This coordinate after scaling.
|
|
*/
|
|
goog.math.Coordinate.prototype.scale = function(sx, opt_sy) {
|
|
'use strict';
|
|
var sy = (typeof opt_sy === 'number') ? opt_sy : sx;
|
|
this.x *= sx;
|
|
this.y *= sy;
|
|
return this;
|
|
};
|
|
|
|
|
|
/**
|
|
* Rotates this coordinate clockwise about the origin (or, optionally, the given
|
|
* center) by the given angle, in radians.
|
|
* @param {number} radians The angle by which to rotate this coordinate
|
|
* clockwise about the given center, in radians.
|
|
* @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults
|
|
* to (0, 0) if not given.
|
|
*/
|
|
goog.math.Coordinate.prototype.rotateRadians = function(radians, opt_center) {
|
|
'use strict';
|
|
var center = opt_center || new goog.math.Coordinate(0, 0);
|
|
|
|
var x = this.x;
|
|
var y = this.y;
|
|
var cos = Math.cos(radians);
|
|
var sin = Math.sin(radians);
|
|
|
|
this.x = (x - center.x) * cos - (y - center.y) * sin + center.x;
|
|
this.y = (x - center.x) * sin + (y - center.y) * cos + center.y;
|
|
};
|
|
|
|
|
|
/**
|
|
* Rotates this coordinate clockwise about the origin (or, optionally, the given
|
|
* center) by the given angle, in degrees.
|
|
* @param {number} degrees The angle by which to rotate this coordinate
|
|
* clockwise about the given center, in degrees.
|
|
* @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults
|
|
* to (0, 0) if not given.
|
|
*/
|
|
goog.math.Coordinate.prototype.rotateDegrees = function(degrees, opt_center) {
|
|
'use strict';
|
|
this.rotateRadians(goog.math.toRadians(degrees), opt_center);
|
|
};
|