lib/goog/math/box.js

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