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