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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
// 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 rectangles. Some of these
* functions should be migrated over to non-nullable params.
*/
goog.provide('goog.math.Rect');
goog.require('goog.asserts');
goog.require('goog.math.Box');
goog.require('goog.math.Coordinate');
goog.require('goog.math.IRect');
goog.require('goog.math.Size');
/**
* Class for representing rectangular regions.
* @param {number} x Left.
* @param {number} y Top.
* @param {number} w Width.
* @param {number} h Height.
* @struct
* @constructor
* @implements {goog.math.IRect}
*/
goog.math.Rect = function(x, y, w, h) {
/** @type {number} */
this.left = x;
/** @type {number} */
this.top = y;
/** @type {number} */
this.width = w;
/** @type {number} */
this.height = h;
};
/**
* @return {!goog.math.Rect} A new copy of this Rectangle.
*/
goog.math.Rect.prototype.clone = function() {
return new goog.math.Rect(this.left, this.top, this.width, this.height);
};
/**
* Returns a new Box object with the same position and dimensions as this
* rectangle.
* @return {!goog.math.Box} A new Box representation of this Rectangle.
*/
goog.math.Rect.prototype.toBox = function() {
var right = this.left + this.width;
var bottom = this.top + this.height;
return new goog.math.Box(this.top, right, bottom, this.left);
};
/**
* Creates a new Rect object with the position and size given.
* @param {!goog.math.Coordinate} position The top-left coordinate of the Rect
* @param {!goog.math.Size} size The size of the Rect
* @return {!goog.math.Rect} A new Rect initialized with the given position and
* size.
*/
goog.math.Rect.createFromPositionAndSize = function(position, size) {
return new goog.math.Rect(position.x, position.y, size.width, size.height);
};
/**
* Creates a new Rect object with the same position and dimensions as a given
* Box. Note that this is only the inverse of toBox if left/top are defined.
* @param {goog.math.Box} box A box.
* @return {!goog.math.Rect} A new Rect initialized with the box's position
* and size.
*/
goog.math.Rect.createFromBox = function(box) {
return new goog.math.Rect(
box.left, box.top, box.right - box.left, box.bottom - box.top);
};
if (goog.DEBUG) {
/**
* Returns a nice string representing size and dimensions of rectangle.
* @return {string} In the form (50, 73 - 75w x 25h).
* @override
*/
goog.math.Rect.prototype.toString = function() {
return '(' + this.left + ', ' + this.top + ' - ' + this.width + 'w x ' +
this.height + 'h)';
};
}
/**
* Compares rectangles for equality.
* @param {goog.math.IRect} a A Rectangle.
* @param {goog.math.IRect} b A Rectangle.
* @return {boolean} True iff the rectangles have the same left, top, width,
* and height, or if both are null.
*/
goog.math.Rect.equals = function(a, b) {
if (a == b) {
return true;
}
if (!a || !b) {
return false;
}
return a.left == b.left && a.width == b.width && a.top == b.top &&
a.height == b.height;
};
/**
* Computes the intersection of this rectangle and the rectangle parameter. If
* there is no intersection, returns false and leaves this rectangle as is.
* @param {goog.math.IRect} rect A Rectangle.
* @return {boolean} True iff this rectangle intersects with the parameter.
*/
goog.math.Rect.prototype.intersection = function(rect) {
var x0 = Math.max(this.left, rect.left);
var x1 = Math.min(this.left + this.width, rect.left + rect.width);
if (x0 <= x1) {
var y0 = Math.max(this.top, rect.top);
var y1 = Math.min(this.top + this.height, rect.top + rect.height);
if (y0 <= y1) {
this.left = x0;
this.top = y0;
this.width = x1 - x0;
this.height = y1 - y0;
return true;
}
}
return false;
};
/**
* Returns the intersection of two rectangles. Two rectangles intersect if they
* touch at all, for example, two zero width and height rectangles would
* intersect if they had the same top and left.
* @param {goog.math.IRect} a A Rectangle.
* @param {goog.math.IRect} b A Rectangle.
* @return {goog.math.Rect} A new intersection rect (even if width and height
* are 0), or null if there is no intersection.
*/
goog.math.Rect.intersection = function(a, b) {
// There is no nice way to do intersection via a clone, because any such
// clone might be unnecessary if this function returns null. So, we duplicate
// code from above.
var x0 = Math.max(a.left, b.left);
var x1 = Math.min(a.left + a.width, b.left + b.width);
if (x0 <= x1) {
var y0 = Math.max(a.top, b.top);
var y1 = Math.min(a.top + a.height, b.top + b.height);
if (y0 <= y1) {
return new goog.math.Rect(x0, y0, x1 - x0, y1 - y0);
}
}
return null;
};
/**
* Returns whether two rectangles intersect. Two rectangles intersect if they
* touch at all, for example, two zero width and height rectangles would
* intersect if they had the same top and left.
* @param {goog.math.IRect} a A Rectangle.
* @param {goog.math.IRect} b A Rectangle.
* @return {boolean} Whether a and b intersect.
*/
goog.math.Rect.intersects = function(a, b) {
return (
a.left <= b.left + b.width && b.left <= a.left + a.width &&
a.top <= b.top + b.height && b.top <= a.top + a.height);
};
/**
* Returns whether a rectangle intersects this rectangle.
* @param {goog.math.IRect} rect A rectangle.
* @return {boolean} Whether rect intersects this rectangle.
*/
goog.math.Rect.prototype.intersects = function(rect) {
return goog.math.Rect.intersects(this, rect);
};
/**
* Computes the difference regions between two rectangles. The return value is
* an array of 0 to 4 rectangles defining the remaining regions of the first
* rectangle after the second has been subtracted.
* @param {goog.math.Rect} a A Rectangle.
* @param {goog.math.IRect} b A Rectangle.
* @return {!Array<!goog.math.Rect>} An array with 0 to 4 rectangles which
* together define the difference area of rectangle a minus rectangle b.
*/
goog.math.Rect.difference = function(a, b) {
var intersection = goog.math.Rect.intersection(a, b);
if (!intersection || !intersection.height || !intersection.width) {
return [a.clone()];
}
var result = [];
var top = a.top;
var height = a.height;
var ar = a.left + a.width;
var ab = a.top + a.height;
var br = b.left + b.width;
var bb = b.top + b.height;
// Subtract off any area on top where A extends past B
if (b.top > a.top) {
result.push(new goog.math.Rect(a.left, a.top, a.width, b.top - a.top));
top = b.top;
// If we're moving the top down, we also need to subtract the height diff.
height -= b.top - a.top;
}
// Subtract off any area on bottom where A extends past B
if (bb < ab) {
result.push(new goog.math.Rect(a.left, bb, a.width, ab - bb));
height = bb - top;
}
// Subtract any area on left where A extends past B
if (b.left > a.left) {
result.push(new goog.math.Rect(a.left, top, b.left - a.left, height));
}
// Subtract any area on right where A extends past B
if (br < ar) {
result.push(new goog.math.Rect(br, top, ar - br, height));
}
return result;
};
/**
* Computes the difference regions between this rectangle and {@code rect}. The
* return value is an array of 0 to 4 rectangles defining the remaining regions
* of this rectangle after the other has been subtracted.
* @param {goog.math.IRect} rect A Rectangle.
* @return {!Array<!goog.math.Rect>} An array with 0 to 4 rectangles which
* together define the difference area of rectangle a minus rectangle b.
*/
goog.math.Rect.prototype.difference = function(rect) {
return goog.math.Rect.difference(this, rect);
};
/**
* Expand this rectangle to also include the area of the given rectangle.
* @param {goog.math.IRect} rect The other rectangle.
*/
goog.math.Rect.prototype.boundingRect = function(rect) {
// We compute right and bottom before we change left and top below.
var right = Math.max(this.left + this.width, rect.left + rect.width);
var bottom = Math.max(this.top + this.height, rect.top + rect.height);
this.left = Math.min(this.left, rect.left);
this.top = Math.min(this.top, rect.top);
this.width = right - this.left;
this.height = bottom - this.top;
};
/**
* Returns a new rectangle which completely contains both input rectangles.
* @param {goog.math.IRect} a A rectangle.
* @param {goog.math.IRect} b A rectangle.
* @return {goog.math.Rect} A new bounding rect, or null if either rect is
* null.
*/
goog.math.Rect.boundingRect = function(a, b) {
if (!a || !b) {
return null;
}
var newRect = new goog.math.Rect(a.left, a.top, a.width, a.height);
newRect.boundingRect(b);
return newRect;
};
/**
* Tests whether this rectangle entirely contains another rectangle or
* coordinate.
*
* @param {goog.math.IRect|goog.math.Coordinate} another The rectangle or
* coordinate to test for containment.
* @return {boolean} Whether this rectangle contains given rectangle or
* coordinate.
*/
goog.math.Rect.prototype.contains = function(another) {
if (another instanceof goog.math.Coordinate) {
return another.x >= this.left && another.x <= this.left + this.width &&
another.y >= this.top && another.y <= this.top + this.height;
} else { // (another instanceof goog.math.IRect)
return this.left <= another.left &&
this.left + this.width >= another.left + another.width &&
this.top <= another.top &&
this.top + this.height >= another.top + another.height;
}
};
/**
* @param {!goog.math.Coordinate} point A coordinate.
* @return {number} The squared distance between the point and the closest
* point inside the rectangle. Returns 0 if the point is inside the
* rectangle.
*/
goog.math.Rect.prototype.squaredDistance = function(point) {
var dx = point.x < this.left ?
this.left - point.x :
Math.max(point.x - (this.left + this.width), 0);
var dy = point.y < this.top ? this.top - point.y :
Math.max(point.y - (this.top + this.height), 0);
return dx * dx + dy * dy;
};
/**
* @param {!goog.math.Coordinate} point A coordinate.
* @return {number} The distance between the point and the closest point
* inside the rectangle. Returns 0 if the point is inside the rectangle.
*/
goog.math.Rect.prototype.distance = function(point) {
return Math.sqrt(this.squaredDistance(point));
};
/**
* @return {!goog.math.Size} The size of this rectangle.
*/
goog.math.Rect.prototype.getSize = function() {
return new goog.math.Size(this.width, this.height);
};
/**
* @return {!goog.math.Coordinate} A new coordinate for the top-left corner of
* the rectangle.
*/
goog.math.Rect.prototype.getTopLeft = function() {
return new goog.math.Coordinate(this.left, this.top);
};
/**
* @return {!goog.math.Coordinate} A new coordinate for the center of the
* rectangle.
*/
goog.math.Rect.prototype.getCenter = function() {
return new goog.math.Coordinate(
this.left + this.width / 2, this.top + this.height / 2);
};
/**
* @return {!goog.math.Coordinate} A new coordinate for the bottom-right corner
* of the rectangle.
*/
goog.math.Rect.prototype.getBottomRight = function() {
return new goog.math.Coordinate(
this.left + this.width, this.top + this.height);
};
/**
* Rounds the fields to the next larger integer values.
* @return {!goog.math.Rect} This rectangle with ceil'd fields.
*/
goog.math.Rect.prototype.ceil = function() {
this.left = Math.ceil(this.left);
this.top = Math.ceil(this.top);
this.width = Math.ceil(this.width);
this.height = Math.ceil(this.height);
return this;
};
/**
* Rounds the fields to the next smaller integer values.
* @return {!goog.math.Rect} This rectangle with floored fields.
*/
goog.math.Rect.prototype.floor = function() {
this.left = Math.floor(this.left);
this.top = Math.floor(this.top);
this.width = Math.floor(this.width);
this.height = Math.floor(this.height);
return this;
};
/**
* Rounds the fields to nearest integer values.
* @return {!goog.math.Rect} This rectangle with rounded fields.
*/
goog.math.Rect.prototype.round = function() {
this.left = Math.round(this.left);
this.top = Math.round(this.top);
this.width = Math.round(this.width);
this.height = Math.round(this.height);
return this;
};
/**
* Translates this rectangle by the given offsets. If a
* {@code goog.math.Coordinate} is given, then the left and top values are
* translated by the coordinate's x and y values. Otherwise, top and left are
* translated by {@code tx} and {@code opt_ty} respectively.
* @param {number|goog.math.Coordinate} tx The value to translate left by or the
* the coordinate to translate this rect by.
* @param {number=} opt_ty The value to translate top by.
* @return {!goog.math.Rect} This rectangle after translating.
*/
goog.math.Rect.prototype.translate = function(tx, opt_ty) {
if (tx instanceof goog.math.Coordinate) {
this.left += tx.x;
this.top += tx.y;
} else {
this.left += goog.asserts.assertNumber(tx);
if (goog.isNumber(opt_ty)) {
this.top += opt_ty;
}
}
return this;
};
/**
* Scales this rectangle by the given scale factors. The left and width values
* are scaled by {@code sx} and the top and height values are scaled by
* {@code opt_sy}. If {@code opt_sy} is not given, then all fields are scaled
* by {@code sx}.
* @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.Rect} This rectangle after scaling.
*/
goog.math.Rect.prototype.scale = function(sx, opt_sy) {
var sy = goog.isNumber(opt_sy) ? opt_sy : sx;
this.left *= sx;
this.width *= sx;
this.top *= sy;
this.height *= sy;
return this;
};
|