coordinate.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2006 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. * @fileoverview A utility class for representing two-dimensional positions.
  16. */
  17. goog.provide('goog.math.Coordinate');
  18. goog.require('goog.math');
  19. /**
  20. * Class for representing coordinates and positions.
  21. * @param {number=} opt_x Left, defaults to 0.
  22. * @param {number=} opt_y Top, defaults to 0.
  23. * @struct
  24. * @constructor
  25. */
  26. goog.math.Coordinate = function(opt_x, opt_y) {
  27. /**
  28. * X-value
  29. * @type {number}
  30. */
  31. this.x = goog.isDef(opt_x) ? opt_x : 0;
  32. /**
  33. * Y-value
  34. * @type {number}
  35. */
  36. this.y = goog.isDef(opt_y) ? opt_y : 0;
  37. };
  38. /**
  39. * Returns a new copy of the coordinate.
  40. * @return {!goog.math.Coordinate} A clone of this coordinate.
  41. */
  42. goog.math.Coordinate.prototype.clone = function() {
  43. return new goog.math.Coordinate(this.x, this.y);
  44. };
  45. if (goog.DEBUG) {
  46. /**
  47. * Returns a nice string representing the coordinate.
  48. * @return {string} In the form (50, 73).
  49. * @override
  50. */
  51. goog.math.Coordinate.prototype.toString = function() {
  52. return '(' + this.x + ', ' + this.y + ')';
  53. };
  54. }
  55. /**
  56. * Returns whether the specified value is equal to this coordinate.
  57. * @param {*} other Some other value.
  58. * @return {boolean} Whether the specified value is equal to this coordinate.
  59. */
  60. goog.math.Coordinate.prototype.equals = function(other) {
  61. return other instanceof goog.math.Coordinate &&
  62. goog.math.Coordinate.equals(this, other);
  63. };
  64. /**
  65. * Compares coordinates for equality.
  66. * @param {goog.math.Coordinate} a A Coordinate.
  67. * @param {goog.math.Coordinate} b A Coordinate.
  68. * @return {boolean} True iff the coordinates are equal, or if both are null.
  69. */
  70. goog.math.Coordinate.equals = function(a, b) {
  71. if (a == b) {
  72. return true;
  73. }
  74. if (!a || !b) {
  75. return false;
  76. }
  77. return a.x == b.x && a.y == b.y;
  78. };
  79. /**
  80. * Returns the distance between two coordinates.
  81. * @param {!goog.math.Coordinate} a A Coordinate.
  82. * @param {!goog.math.Coordinate} b A Coordinate.
  83. * @return {number} The distance between {@code a} and {@code b}.
  84. */
  85. goog.math.Coordinate.distance = function(a, b) {
  86. var dx = a.x - b.x;
  87. var dy = a.y - b.y;
  88. return Math.sqrt(dx * dx + dy * dy);
  89. };
  90. /**
  91. * Returns the magnitude of a coordinate.
  92. * @param {!goog.math.Coordinate} a A Coordinate.
  93. * @return {number} The distance between the origin and {@code a}.
  94. */
  95. goog.math.Coordinate.magnitude = function(a) {
  96. return Math.sqrt(a.x * a.x + a.y * a.y);
  97. };
  98. /**
  99. * Returns the angle from the origin to a coordinate.
  100. * @param {!goog.math.Coordinate} a A Coordinate.
  101. * @return {number} The angle, in degrees, clockwise from the positive X
  102. * axis to {@code a}.
  103. */
  104. goog.math.Coordinate.azimuth = function(a) {
  105. return goog.math.angle(0, 0, a.x, a.y);
  106. };
  107. /**
  108. * Returns the squared distance between two coordinates. Squared distances can
  109. * be used for comparisons when the actual value is not required.
  110. *
  111. * Performance note: eliminating the square root is an optimization often used
  112. * in lower-level languages, but the speed difference is not nearly as
  113. * pronounced in JavaScript (only a few percent.)
  114. *
  115. * @param {!goog.math.Coordinate} a A Coordinate.
  116. * @param {!goog.math.Coordinate} b A Coordinate.
  117. * @return {number} The squared distance between {@code a} and {@code b}.
  118. */
  119. goog.math.Coordinate.squaredDistance = function(a, b) {
  120. var dx = a.x - b.x;
  121. var dy = a.y - b.y;
  122. return dx * dx + dy * dy;
  123. };
  124. /**
  125. * Returns the difference between two coordinates as a new
  126. * goog.math.Coordinate.
  127. * @param {!goog.math.Coordinate} a A Coordinate.
  128. * @param {!goog.math.Coordinate} b A Coordinate.
  129. * @return {!goog.math.Coordinate} A Coordinate representing the difference
  130. * between {@code a} and {@code b}.
  131. */
  132. goog.math.Coordinate.difference = function(a, b) {
  133. return new goog.math.Coordinate(a.x - b.x, a.y - b.y);
  134. };
  135. /**
  136. * Returns the sum of two coordinates as a new goog.math.Coordinate.
  137. * @param {!goog.math.Coordinate} a A Coordinate.
  138. * @param {!goog.math.Coordinate} b A Coordinate.
  139. * @return {!goog.math.Coordinate} A Coordinate representing the sum of the two
  140. * coordinates.
  141. */
  142. goog.math.Coordinate.sum = function(a, b) {
  143. return new goog.math.Coordinate(a.x + b.x, a.y + b.y);
  144. };
  145. /**
  146. * Rounds the x and y fields to the next larger integer values.
  147. * @return {!goog.math.Coordinate} This coordinate with ceil'd fields.
  148. */
  149. goog.math.Coordinate.prototype.ceil = function() {
  150. this.x = Math.ceil(this.x);
  151. this.y = Math.ceil(this.y);
  152. return this;
  153. };
  154. /**
  155. * Rounds the x and y fields to the next smaller integer values.
  156. * @return {!goog.math.Coordinate} This coordinate with floored fields.
  157. */
  158. goog.math.Coordinate.prototype.floor = function() {
  159. this.x = Math.floor(this.x);
  160. this.y = Math.floor(this.y);
  161. return this;
  162. };
  163. /**
  164. * Rounds the x and y fields to the nearest integer values.
  165. * @return {!goog.math.Coordinate} This coordinate with rounded fields.
  166. */
  167. goog.math.Coordinate.prototype.round = function() {
  168. this.x = Math.round(this.x);
  169. this.y = Math.round(this.y);
  170. return this;
  171. };
  172. /**
  173. * Translates this box by the given offsets. If a {@code goog.math.Coordinate}
  174. * is given, then the x and y values are translated by the coordinate's x and y.
  175. * Otherwise, x and y are translated by {@code tx} and {@code opt_ty}
  176. * respectively.
  177. * @param {number|goog.math.Coordinate} tx The value to translate x by or the
  178. * the coordinate to translate this coordinate by.
  179. * @param {number=} opt_ty The value to translate y by.
  180. * @return {!goog.math.Coordinate} This coordinate after translating.
  181. */
  182. goog.math.Coordinate.prototype.translate = function(tx, opt_ty) {
  183. if (tx instanceof goog.math.Coordinate) {
  184. this.x += tx.x;
  185. this.y += tx.y;
  186. } else {
  187. this.x += Number(tx);
  188. if (goog.isNumber(opt_ty)) {
  189. this.y += opt_ty;
  190. }
  191. }
  192. return this;
  193. };
  194. /**
  195. * Scales this coordinate by the given scale factors. The x and y values are
  196. * scaled by {@code sx} and {@code opt_sy} respectively. If {@code opt_sy}
  197. * is not given, then {@code sx} is used for both x and y.
  198. * @param {number} sx The scale factor to use for the x dimension.
  199. * @param {number=} opt_sy The scale factor to use for the y dimension.
  200. * @return {!goog.math.Coordinate} This coordinate after scaling.
  201. */
  202. goog.math.Coordinate.prototype.scale = function(sx, opt_sy) {
  203. var sy = goog.isNumber(opt_sy) ? opt_sy : sx;
  204. this.x *= sx;
  205. this.y *= sy;
  206. return this;
  207. };
  208. /**
  209. * Rotates this coordinate clockwise about the origin (or, optionally, the given
  210. * center) by the given angle, in radians.
  211. * @param {number} radians The angle by which to rotate this coordinate
  212. * clockwise about the given center, in radians.
  213. * @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults
  214. * to (0, 0) if not given.
  215. */
  216. goog.math.Coordinate.prototype.rotateRadians = function(radians, opt_center) {
  217. var center = opt_center || new goog.math.Coordinate(0, 0);
  218. var x = this.x;
  219. var y = this.y;
  220. var cos = Math.cos(radians);
  221. var sin = Math.sin(radians);
  222. this.x = (x - center.x) * cos - (y - center.y) * sin + center.x;
  223. this.y = (x - center.x) * sin + (y - center.y) * cos + center.y;
  224. };
  225. /**
  226. * Rotates this coordinate clockwise about the origin (or, optionally, the given
  227. * center) by the given angle, in degrees.
  228. * @param {number} degrees The angle by which to rotate this coordinate
  229. * clockwise about the given center, in degrees.
  230. * @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults
  231. * to (0, 0) if not given.
  232. */
  233. goog.math.Coordinate.prototype.rotateDegrees = function(degrees, opt_center) {
  234. this.rotateRadians(goog.math.toRadians(degrees), opt_center);
  235. };