box.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 a numeric box.
  16. */
  17. goog.provide('goog.math.Box');
  18. goog.require('goog.asserts');
  19. goog.require('goog.math.Coordinate');
  20. /**
  21. * Class for representing a box. A box is specified as a top, right, bottom,
  22. * and left. A box is useful for representing margins and padding.
  23. *
  24. * This class assumes 'screen coordinates': larger Y coordinates are further
  25. * from the top of the screen.
  26. *
  27. * @param {number} top Top.
  28. * @param {number} right Right.
  29. * @param {number} bottom Bottom.
  30. * @param {number} left Left.
  31. * @struct
  32. * @constructor
  33. */
  34. goog.math.Box = function(top, right, bottom, left) {
  35. /**
  36. * Top
  37. * @type {number}
  38. */
  39. this.top = top;
  40. /**
  41. * Right
  42. * @type {number}
  43. */
  44. this.right = right;
  45. /**
  46. * Bottom
  47. * @type {number}
  48. */
  49. this.bottom = bottom;
  50. /**
  51. * Left
  52. * @type {number}
  53. */
  54. this.left = left;
  55. };
  56. /**
  57. * Creates a Box by bounding a collection of goog.math.Coordinate objects
  58. * @param {...goog.math.Coordinate} var_args Coordinates to be included inside
  59. * the box.
  60. * @return {!goog.math.Box} A Box containing all the specified Coordinates.
  61. */
  62. goog.math.Box.boundingBox = function(var_args) {
  63. var box = new goog.math.Box(
  64. arguments[0].y, arguments[0].x, arguments[0].y, arguments[0].x);
  65. for (var i = 1; i < arguments.length; i++) {
  66. box.expandToIncludeCoordinate(arguments[i]);
  67. }
  68. return box;
  69. };
  70. /**
  71. * @return {number} width The width of this Box.
  72. */
  73. goog.math.Box.prototype.getWidth = function() {
  74. return this.right - this.left;
  75. };
  76. /**
  77. * @return {number} height The height of this Box.
  78. */
  79. goog.math.Box.prototype.getHeight = function() {
  80. return this.bottom - this.top;
  81. };
  82. /**
  83. * Creates a copy of the box with the same dimensions.
  84. * @return {!goog.math.Box} A clone of this Box.
  85. */
  86. goog.math.Box.prototype.clone = function() {
  87. return new goog.math.Box(this.top, this.right, this.bottom, this.left);
  88. };
  89. if (goog.DEBUG) {
  90. /**
  91. * Returns a nice string representing the box.
  92. * @return {string} In the form (50t, 73r, 24b, 13l).
  93. * @override
  94. */
  95. goog.math.Box.prototype.toString = function() {
  96. return '(' + this.top + 't, ' + this.right + 'r, ' + this.bottom + 'b, ' +
  97. this.left + 'l)';
  98. };
  99. }
  100. /**
  101. * Returns whether the box contains a coordinate or another box.
  102. *
  103. * @param {goog.math.Coordinate|goog.math.Box} other A Coordinate or a Box.
  104. * @return {boolean} Whether the box contains the coordinate or other box.
  105. */
  106. goog.math.Box.prototype.contains = function(other) {
  107. return goog.math.Box.contains(this, other);
  108. };
  109. /**
  110. * Expands box with the given margins.
  111. *
  112. * @param {number|goog.math.Box} top Top margin or box with all margins.
  113. * @param {number=} opt_right Right margin.
  114. * @param {number=} opt_bottom Bottom margin.
  115. * @param {number=} opt_left Left margin.
  116. * @return {!goog.math.Box} A reference to this Box.
  117. */
  118. goog.math.Box.prototype.expand = function(
  119. top, opt_right, opt_bottom, opt_left) {
  120. if (goog.isObject(top)) {
  121. this.top -= top.top;
  122. this.right += top.right;
  123. this.bottom += top.bottom;
  124. this.left -= top.left;
  125. } else {
  126. this.top -= /** @type {number} */ (top);
  127. this.right += Number(opt_right);
  128. this.bottom += Number(opt_bottom);
  129. this.left -= Number(opt_left);
  130. }
  131. return this;
  132. };
  133. /**
  134. * Expand this box to include another box.
  135. * NOTE(user): This is used in code that needs to be very fast, please don't
  136. * add functionality to this function at the expense of speed (variable
  137. * arguments, accepting multiple argument types, etc).
  138. * @param {goog.math.Box} box The box to include in this one.
  139. */
  140. goog.math.Box.prototype.expandToInclude = function(box) {
  141. this.left = Math.min(this.left, box.left);
  142. this.top = Math.min(this.top, box.top);
  143. this.right = Math.max(this.right, box.right);
  144. this.bottom = Math.max(this.bottom, box.bottom);
  145. };
  146. /**
  147. * Expand this box to include the coordinate.
  148. * @param {!goog.math.Coordinate} coord The coordinate to be included
  149. * inside the box.
  150. */
  151. goog.math.Box.prototype.expandToIncludeCoordinate = function(coord) {
  152. this.top = Math.min(this.top, coord.y);
  153. this.right = Math.max(this.right, coord.x);
  154. this.bottom = Math.max(this.bottom, coord.y);
  155. this.left = Math.min(this.left, coord.x);
  156. };
  157. /**
  158. * Compares boxes for equality.
  159. * @param {goog.math.Box} a A Box.
  160. * @param {goog.math.Box} b A Box.
  161. * @return {boolean} True iff the boxes are equal, or if both are null.
  162. */
  163. goog.math.Box.equals = function(a, b) {
  164. if (a == b) {
  165. return true;
  166. }
  167. if (!a || !b) {
  168. return false;
  169. }
  170. return a.top == b.top && a.right == b.right && a.bottom == b.bottom &&
  171. a.left == b.left;
  172. };
  173. /**
  174. * Returns whether a box contains a coordinate or another box.
  175. *
  176. * @param {goog.math.Box} box A Box.
  177. * @param {goog.math.Coordinate|goog.math.Box} other A Coordinate or a Box.
  178. * @return {boolean} Whether the box contains the coordinate or other box.
  179. */
  180. goog.math.Box.contains = function(box, other) {
  181. if (!box || !other) {
  182. return false;
  183. }
  184. if (other instanceof goog.math.Box) {
  185. return other.left >= box.left && other.right <= box.right &&
  186. other.top >= box.top && other.bottom <= box.bottom;
  187. }
  188. // other is a Coordinate.
  189. return other.x >= box.left && other.x <= box.right && other.y >= box.top &&
  190. other.y <= box.bottom;
  191. };
  192. /**
  193. * Returns the relative x position of a coordinate compared to a box. Returns
  194. * zero if the coordinate is inside the box.
  195. *
  196. * @param {goog.math.Box} box A Box.
  197. * @param {goog.math.Coordinate} coord A Coordinate.
  198. * @return {number} The x position of {@code coord} relative to the nearest
  199. * side of {@code box}, or zero if {@code coord} is inside {@code box}.
  200. */
  201. goog.math.Box.relativePositionX = function(box, coord) {
  202. if (coord.x < box.left) {
  203. return coord.x - box.left;
  204. } else if (coord.x > box.right) {
  205. return coord.x - box.right;
  206. }
  207. return 0;
  208. };
  209. /**
  210. * Returns the relative y position of a coordinate compared to a box. Returns
  211. * zero if the coordinate is inside the box.
  212. *
  213. * @param {goog.math.Box} box A Box.
  214. * @param {goog.math.Coordinate} coord A Coordinate.
  215. * @return {number} The y position of {@code coord} relative to the nearest
  216. * side of {@code box}, or zero if {@code coord} is inside {@code box}.
  217. */
  218. goog.math.Box.relativePositionY = function(box, coord) {
  219. if (coord.y < box.top) {
  220. return coord.y - box.top;
  221. } else if (coord.y > box.bottom) {
  222. return coord.y - box.bottom;
  223. }
  224. return 0;
  225. };
  226. /**
  227. * Returns the distance between a coordinate and the nearest corner/side of a
  228. * box. Returns zero if the coordinate is inside the box.
  229. *
  230. * @param {goog.math.Box} box A Box.
  231. * @param {goog.math.Coordinate} coord A Coordinate.
  232. * @return {number} The distance between {@code coord} and the nearest
  233. * corner/side of {@code box}, or zero if {@code coord} is inside
  234. * {@code box}.
  235. */
  236. goog.math.Box.distance = function(box, coord) {
  237. var x = goog.math.Box.relativePositionX(box, coord);
  238. var y = goog.math.Box.relativePositionY(box, coord);
  239. return Math.sqrt(x * x + y * y);
  240. };
  241. /**
  242. * Returns whether two boxes intersect.
  243. *
  244. * @param {goog.math.Box} a A Box.
  245. * @param {goog.math.Box} b A second Box.
  246. * @return {boolean} Whether the boxes intersect.
  247. */
  248. goog.math.Box.intersects = function(a, b) {
  249. return (
  250. a.left <= b.right && b.left <= a.right && a.top <= b.bottom &&
  251. b.top <= a.bottom);
  252. };
  253. /**
  254. * Returns whether two boxes would intersect with additional padding.
  255. *
  256. * @param {goog.math.Box} a A Box.
  257. * @param {goog.math.Box} b A second Box.
  258. * @param {number} padding The additional padding.
  259. * @return {boolean} Whether the boxes intersect.
  260. */
  261. goog.math.Box.intersectsWithPadding = function(a, b, padding) {
  262. return (
  263. a.left <= b.right + padding && b.left <= a.right + padding &&
  264. a.top <= b.bottom + padding && b.top <= a.bottom + padding);
  265. };
  266. /**
  267. * Rounds the fields to the next larger integer values.
  268. *
  269. * @return {!goog.math.Box} This box with ceil'd fields.
  270. */
  271. goog.math.Box.prototype.ceil = function() {
  272. this.top = Math.ceil(this.top);
  273. this.right = Math.ceil(this.right);
  274. this.bottom = Math.ceil(this.bottom);
  275. this.left = Math.ceil(this.left);
  276. return this;
  277. };
  278. /**
  279. * Rounds the fields to the next smaller integer values.
  280. *
  281. * @return {!goog.math.Box} This box with floored fields.
  282. */
  283. goog.math.Box.prototype.floor = function() {
  284. this.top = Math.floor(this.top);
  285. this.right = Math.floor(this.right);
  286. this.bottom = Math.floor(this.bottom);
  287. this.left = Math.floor(this.left);
  288. return this;
  289. };
  290. /**
  291. * Rounds the fields to nearest integer values.
  292. *
  293. * @return {!goog.math.Box} This box with rounded fields.
  294. */
  295. goog.math.Box.prototype.round = function() {
  296. this.top = Math.round(this.top);
  297. this.right = Math.round(this.right);
  298. this.bottom = Math.round(this.bottom);
  299. this.left = Math.round(this.left);
  300. return this;
  301. };
  302. /**
  303. * Translates this box by the given offsets. If a {@code goog.math.Coordinate}
  304. * is given, then the left and right values are translated by the coordinate's
  305. * x value and the top and bottom values are translated by the coordinate's y
  306. * value. Otherwise, {@code tx} and {@code opt_ty} are used to translate the x
  307. * and y dimension values.
  308. *
  309. * @param {number|goog.math.Coordinate} tx The value to translate the x
  310. * dimension values by or the the coordinate to translate this box by.
  311. * @param {number=} opt_ty The value to translate y dimension values by.
  312. * @return {!goog.math.Box} This box after translating.
  313. */
  314. goog.math.Box.prototype.translate = function(tx, opt_ty) {
  315. if (tx instanceof goog.math.Coordinate) {
  316. this.left += tx.x;
  317. this.right += tx.x;
  318. this.top += tx.y;
  319. this.bottom += tx.y;
  320. } else {
  321. goog.asserts.assertNumber(tx);
  322. this.left += tx;
  323. this.right += tx;
  324. if (goog.isNumber(opt_ty)) {
  325. this.top += opt_ty;
  326. this.bottom += opt_ty;
  327. }
  328. }
  329. return this;
  330. };
  331. /**
  332. * Scales this coordinate by the given scale factors. The x and y dimension
  333. * values are scaled by {@code sx} and {@code opt_sy} respectively.
  334. * If {@code opt_sy} is not given, then {@code sx} is used for both x and y.
  335. *
  336. * @param {number} sx The scale factor to use for the x dimension.
  337. * @param {number=} opt_sy The scale factor to use for the y dimension.
  338. * @return {!goog.math.Box} This box after scaling.
  339. */
  340. goog.math.Box.prototype.scale = function(sx, opt_sy) {
  341. var sy = goog.isNumber(opt_sy) ? opt_sy : sx;
  342. this.left *= sx;
  343. this.right *= sx;
  344. this.top *= sy;
  345. this.bottom *= sy;
  346. return this;
  347. };